Sybren A. Stüvel 84f93e7502 Transition from ex-GORM structs to sqlc structs (2/5)
Replace old used-to-be-GORM datastructures (#104305) with sqlc-generated
structs. This also makes it possible to use more specific structs that
are more taylored to the specific queries, increasing efficiency.

This commit mostly deals with workers, including the sleep schedule and
task scheduler.

Functional changes are kept to a minimum, as the API still serves the
same data.

Because this work covers so much of Flamenco's code, it's been split up
into different commits. Each commit brings Flamenco to a state where it
compiles and unit tests pass. Only the result of the final commit has
actually been tested properly.

Ref: #104343
2024-12-04 14:00:13 +01:00

73 lines
2.1 KiB
Go

package persistence
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"context"
"database/sql"
"time"
"projects.blender.org/studio/flamenco/internal/manager/persistence/sqlc"
"projects.blender.org/studio/flamenco/pkg/api"
)
// This file contains functions for dealing with task/worker timeouts. Not database timeouts.
// workerStatusNoTimeout contains the worker statuses that are exempt from
// timeout checking. A worker in any other status will be subject to the timeout
// check.
var workerStatusNoTimeout = []api.WorkerStatus{
api.WorkerStatusError,
api.WorkerStatusOffline,
}
// FetchTimedOutTasks returns a slice of tasks that have timed out.
//
// In order to time out, a task must be in status `active` and not touched by a
// Worker since `untouchedSince`.
//
// The returned tasks also have their `Job` and `Worker` fields set.
func (db *DB) FetchTimedOutTasks(ctx context.Context, untouchedSince time.Time) ([]*Task, error) {
queries := db.queries()
sqlcTasks, err := queries.FetchTimedOutTasks(ctx, sqlc.FetchTimedOutTasksParams{
TaskStatus: api.TaskStatusActive,
UntouchedSince: sql.NullTime{Time: untouchedSince, Valid: true},
})
if err != nil {
return nil, taskError(err, "finding timed out tasks (untouched since %s)", untouchedSince.String())
}
result := make([]*Task, len(sqlcTasks))
for index, task := range sqlcTasks {
gormTask, err := convertSqlTaskWithJobAndWorker(ctx, queries, task)
if err != nil {
return nil, err
}
result[index] = gormTask
}
return result, nil
}
func (db *DB) FetchTimedOutWorkers(ctx context.Context, lastSeenBefore time.Time) ([]*Worker, error) {
queries := db.queries()
sqlcWorkers, err := queries.FetchTimedOutWorkers(ctx, sqlc.FetchTimedOutWorkersParams{
WorkerStatusesNoTimeout: workerStatusNoTimeout,
LastSeenBefore: sql.NullTime{
Time: lastSeenBefore.UTC(),
Valid: true},
})
if err != nil {
return nil, workerError(err, "finding timed out workers (last seen before %s)", lastSeenBefore.String())
}
result := make([]*Worker, len(sqlcWorkers))
for index := range sqlcWorkers {
result[index] = &sqlcWorkers[index]
}
return result, nil
}