Manager: avoid "no record found" error in task scheduler

It's fine when there is no task for a worker, so having Gorm log an error
was just causing noise.
This commit is contained in:
Sybren A. Stüvel 2022-03-01 11:52:28 +01:00
parent 648a220098
commit 0235ffcb4a

View File

@ -49,13 +49,13 @@ func (db *DB) ScheduleTask(ctx context.Context, w *Worker) (*Task, error) {
txErr := db.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error { txErr := db.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var err error var err error
task, err = findTaskForWorker(tx, w) task, err = findTaskForWorker(tx, w)
if err == gorm.ErrRecordNotFound {
// Not finding a task is not an error.
return nil
}
if err != nil { if err != nil {
return fmt.Errorf("error finding task for worker: %w", err) return fmt.Errorf("error finding task for worker: %w", err)
} }
if task == nil {
// No task found, which is fine.
return nil
}
// Found a task, now assign it to the requesting worker. // Found a task, now assign it to the requesting worker.
// Without the Select() call, Gorm will try and also store task.Job in the jobs database, which is not what we want. // Without the Select() call, Gorm will try and also store task.Job in the jobs database, which is not what we want.
@ -104,11 +104,15 @@ func findTaskForWorker(tx *gorm.DB, w *Worker) (*Task, error) {
Order("priority desc"). // Highest task priority Order("priority desc"). // Highest task priority
Limit(1). Limit(1).
Preload("Job"). Preload("Job").
First(&task) Find(&task)
if findTaskResult.Error != nil { if findTaskResult.Error != nil {
return nil, findTaskResult.Error return nil, findTaskResult.Error
} }
if task.ID == 0 {
// No task fetched, which doesn't result in an error with Limt(1).Find(&task).
return nil, nil
}
return &task, nil return &task, nil
} }