Add structured logging and better error handling

This commit is contained in:
SirBlob committed 2026-11-01 13:50:26 -05:00
1 parent db4e3c9611
commit 68c7dd76f2
31 files changed
+640 -109

No files matched your search

+9 -4
View File
@@ -2,7 +2,7 @@ package jobs
import (
"encoding/json"
"log"
"log/slog"
"os"
"path/filepath"
"time"
@@ -69,23 +69,28 @@ func phaseFor(status Status, downloaded bool) string {
func (job *Job) saveRecord() {
data, err := json.MarshalIndent(job.record(), "", " ")
if err != nil {
log.Printf("could not encode download %s: %v", job.id, err)
job.logger().Error("could not encode download state", "error", err)
return
}
job.recordMutex.Lock()
defer job.recordMutex.Unlock()
if err := files.WriteAtomically(filepath.Join(job.directory, files.JobFileName), data); err != nil && !os.IsNotExist(err) {
log.Printf("could not save download %s: %v", job.id, err)
job.logger().Error("could not save download state", "error", err)
}
}
func readRecord(directory string) (jobRecord, bool) {
data, err := os.ReadFile(filepath.Join(directory, files.JobFileName))
recordPath := filepath.Join(directory, files.JobFileName)
data, err := os.ReadFile(recordPath)
if err != nil {
if !os.IsNotExist(err) {
slog.Warn("could not read saved download state", "path", recordPath, "error", err)
}
return jobRecord{}, false
}
var record jobRecord
if err := json.Unmarshal(data, &record); err != nil {
slog.Warn("saved download state is damaged, treating the download as finished", "path", recordPath, "error", err)
return jobRecord{}, false
}
return record, true