Add structured logging and better error handling
This commit is contained in:
1 parent
db4e3c9611
commit
68c7dd76f2
31 files changed
+640
-109
No files matched your search
+20
-5
@@ -5,7 +5,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"ddc/internal/explain"
|
||||
"ddc/internal/files"
|
||||
)
|
||||
|
||||
@@ -50,10 +52,9 @@ func (manager *Manager) Move(id string, request MoveRequest) error {
|
||||
if !job.begin(StatusMoving, StatusReady) {
|
||||
return ErrJobBusy
|
||||
}
|
||||
go func() {
|
||||
defer job.workers.Done()
|
||||
manager.runInBackground(job, "move", func() {
|
||||
manager.moveFiles(job, sources, destination, request.Cleanup)
|
||||
}()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -109,13 +110,26 @@ func (manager *Manager) moveFiles(job *Job, sources []string, destination string
|
||||
|
||||
movedNames := make([]string, 0, len(sources))
|
||||
defer func() { manager.recordHistory(job, destination, movedNames) }()
|
||||
started := time.Now()
|
||||
for _, source := range sources {
|
||||
if err := files.MoveInto(source, destination, manager.reservedBytes(), job.Add); err != nil {
|
||||
job.setStatus(StatusReady, "Move failed: "+err.Error())
|
||||
job.logger().Error("move failed",
|
||||
"item", filepath.Base(source),
|
||||
"destination", destination,
|
||||
"moved_before_failure", len(movedNames),
|
||||
"error", err,
|
||||
)
|
||||
job.setStatus(StatusReady, "Move failed: "+explain.Error(err))
|
||||
return
|
||||
}
|
||||
movedNames = append(movedNames, filepath.Base(source))
|
||||
}
|
||||
job.logger().Info("move finished",
|
||||
"items", len(movedNames),
|
||||
"bytes", totalBytes,
|
||||
"destination", destination,
|
||||
"duration", time.Since(started).Round(time.Second).String(),
|
||||
)
|
||||
|
||||
message := fmt.Sprintf("Moved %d item(s) to %s", len(sources), destination)
|
||||
if !cleanup {
|
||||
@@ -123,7 +137,8 @@ func (manager *Manager) moveFiles(job *Job, sources []string, destination string
|
||||
return
|
||||
}
|
||||
if err := os.RemoveAll(job.directory); err != nil {
|
||||
message += ", cleanup failed: " + err.Error()
|
||||
job.logger().Warn("could not remove temporary files after move", "directory", job.directory, "error", err)
|
||||
message += ", cleanup failed: " + explain.Error(err)
|
||||
}
|
||||
job.setStatus(StatusCompleted, message)
|
||||
}
|
||||
Reference in new issue
Block a user