43 lines
854 B
Go
43 lines
854 B
Go
package jobs
|
|
|
|
import (
|
|
"log/slog"
|
|
"runtime/debug"
|
|
)
|
|
|
|
const shortIDLength = 8
|
|
|
|
func (job *Job) logger() *slog.Logger {
|
|
return slog.With("download", job.id[:min(len(job.id), shortIDLength)])
|
|
}
|
|
|
|
func (job *Job) isDownloaded() bool {
|
|
job.mutex.Lock()
|
|
defer job.mutex.Unlock()
|
|
return job.downloaded
|
|
}
|
|
|
|
func (manager *Manager) runInBackground(job *Job, task string, work func()) {
|
|
go func() {
|
|
defer job.workers.Done()
|
|
defer func() {
|
|
recovered := recover()
|
|
if recovered == nil {
|
|
return
|
|
}
|
|
job.logger().Error("background task crashed",
|
|
"task", task,
|
|
"panic", recovered,
|
|
"stack", string(debug.Stack()),
|
|
)
|
|
status := StatusFailed
|
|
if job.isDownloaded() {
|
|
status = StatusReady
|
|
}
|
|
job.setStatus(status, "Internal error during "+task+". The server log has the details.")
|
|
job.saveRecord()
|
|
}()
|
|
work()
|
|
}()
|
|
}
|