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
@@ -4,8 +4,10 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"ddc/internal/diskspace"
|
||||
"ddc/internal/explain"
|
||||
"ddc/internal/extract"
|
||||
"ddc/internal/files"
|
||||
)
|
||||
@@ -32,14 +34,13 @@ func (manager *Manager) Extract(id, relativePath string) error {
|
||||
if !job.begin(StatusExtracting, StatusReady) {
|
||||
return ErrJobBusy
|
||||
}
|
||||
go func() {
|
||||
defer job.workers.Done()
|
||||
manager.runInBackground(job, "extraction", func() {
|
||||
if _, err := manager.extractArchive(job, archivePath); err != nil {
|
||||
manager.finishWithError(job, StatusReady, "Extraction failed: ", err)
|
||||
manager.failExtraction(job, err)
|
||||
return
|
||||
}
|
||||
job.setStatus(StatusReady, "")
|
||||
}()
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -58,16 +59,27 @@ func (manager *Manager) extractArchive(job *Job, archivePath string) (string, er
|
||||
limits := extract.Limits{MaxBytes: limit, MaxEntries: maxArchiveEntries}
|
||||
|
||||
job.resetProgress()
|
||||
started := time.Now()
|
||||
job.logger().Info("extraction started", "archive", filepath.Base(archivePath))
|
||||
if err := extract.Archive(job.jobContext, archivePath, destination, limits, job); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return destination, os.Remove(archivePath)
|
||||
job.logger().Info("extraction finished",
|
||||
"archive", filepath.Base(archivePath),
|
||||
"folder", filepath.Base(destination),
|
||||
"duration", time.Since(started).Round(time.Second).String(),
|
||||
)
|
||||
if err := os.Remove(archivePath); err != nil {
|
||||
job.logger().Warn("could not remove archive after extraction", "archive", archivePath, "error", err)
|
||||
}
|
||||
return destination, nil
|
||||
}
|
||||
|
||||
func (manager *Manager) finishWithError(job *Job, status Status, prefix string, err error) {
|
||||
func (manager *Manager) failExtraction(job *Job, err error) {
|
||||
if job.jobContext.Err() != nil {
|
||||
job.setStatus(StatusCancelled, "")
|
||||
return
|
||||
}
|
||||
job.setStatus(status, prefix+err.Error())
|
||||
job.logger().Error("extraction failed", "error", err)
|
||||
job.setStatus(StatusReady, "Extraction failed: "+explain.Error(err))
|
||||
}
|
||||
Reference in new issue
Block a user