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,7 +4,9 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"ddc/internal/explain"
|
||||
"ddc/internal/extract"
|
||||
"ddc/internal/fetch"
|
||||
)
|
||||
@@ -17,6 +19,7 @@ func (manager *Manager) Pause(id string) error {
|
||||
if !job.pause() {
|
||||
return ErrJobBusy
|
||||
}
|
||||
job.logger().Info("pause requested")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -28,6 +31,7 @@ func (manager *Manager) Resume(id string) error {
|
||||
if !job.begin(StatusQueued, StatusPaused, StatusFailed) {
|
||||
return ErrJobBusy
|
||||
}
|
||||
job.logger().Info("download resumed", "has_saved_progress", job.currentDownloadState() != nil)
|
||||
job.saveRecord()
|
||||
manager.launchDownload(job)
|
||||
return nil
|
||||
@@ -44,17 +48,17 @@ func (manager *Manager) Reorder(id, direction string) error {
|
||||
if job.currentStatus() != StatusQueued || !manager.slots.reorder(id, direction) {
|
||||
return ErrJobBusy
|
||||
}
|
||||
job.logger().Debug("queue position changed", "direction", direction, "position", manager.slots.position(id))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *Manager) launchDownload(job *Job) {
|
||||
runContext, cancelRun := job.startRun()
|
||||
manager.slots.enqueue(job.id)
|
||||
go func() {
|
||||
defer job.workers.Done()
|
||||
manager.runInBackground(job, "download", func() {
|
||||
defer cancelRun(nil)
|
||||
manager.download(job, runContext)
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func (manager *Manager) download(job *Job, runContext context.Context) {
|
||||
@@ -65,18 +69,25 @@ func (manager *Manager) download(job *Job, runContext context.Context) {
|
||||
defer manager.slots.release()
|
||||
|
||||
job.setStatus(StatusDownloading, "")
|
||||
started := time.Now()
|
||||
job.logger().Info("download started", "url", job.displayURL)
|
||||
filePath, err := manager.downloader.Download(runContext, manager.fetchRequest(job))
|
||||
if err != nil {
|
||||
manager.handleDownloadError(job, runContext, err)
|
||||
return
|
||||
}
|
||||
job.markDownloaded(filepath.Base(filePath))
|
||||
job.logger().Info("download finished",
|
||||
"file", filepath.Base(filePath),
|
||||
"bytes", job.bytesDone.Load(),
|
||||
"duration", time.Since(started).Round(time.Second).String(),
|
||||
)
|
||||
|
||||
if job.autoExtract && extract.IsArchive(filePath) {
|
||||
job.setStatus(StatusExtracting, "")
|
||||
destination, err := manager.extractArchive(job, filePath)
|
||||
if err != nil {
|
||||
manager.finishWithError(job, StatusReady, "Extraction failed: ", err)
|
||||
manager.failExtraction(job, err)
|
||||
job.saveRecord()
|
||||
return
|
||||
}
|
||||
@@ -98,6 +109,7 @@ func (manager *Manager) fetchRequest(job *Job) fetch.Request {
|
||||
Connections: current.ConnectionsPerDownload,
|
||||
State: job.currentDownloadState(),
|
||||
Progress: job,
|
||||
Logger: job.logger(),
|
||||
Checkpoint: func(state fetch.State) {
|
||||
job.setDownloadState(&state)
|
||||
job.saveRecord()
|
||||
@@ -109,14 +121,22 @@ func (manager *Manager) handleDownloadError(job *Job, runContext context.Context
|
||||
switch {
|
||||
case errors.Is(context.Cause(runContext), errPaused):
|
||||
job.setStatus(StatusPaused, "")
|
||||
job.logger().Info("download paused", "bytes_done", job.bytesDone.Load())
|
||||
case job.jobContext.Err() != nil:
|
||||
job.setStatus(StatusCancelled, "")
|
||||
job.logger().Info("download stopped", "bytes_done", job.bytesDone.Load())
|
||||
return
|
||||
default:
|
||||
if errors.Is(err, fetch.ErrSourceChanged) || errors.Is(err, fetch.ErrResumeUnsupported) {
|
||||
job.setDownloadState(nil)
|
||||
}
|
||||
job.setStatus(StatusFailed, err.Error())
|
||||
job.setStatus(StatusFailed, explain.Error(err))
|
||||
job.logger().Error("download failed",
|
||||
"url", job.displayURL,
|
||||
"bytes_done", job.bytesDone.Load(),
|
||||
"bytes_total", job.bytesTotal.Load(),
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
job.saveRecord()
|
||||
}
|
||||
Reference in new issue
Block a user