maintenance: pure-logic ticker decides forget/prune/check fires
This commit is contained in:
@@ -193,6 +193,73 @@ func (s *Store) GetJob(ctx context.Context, id string) (*Job, error) {
|
||||
return &j, nil
|
||||
}
|
||||
|
||||
// LatestJobByKind returns the most recent terminal job (status in
|
||||
// 'succeeded','failed','cancelled' — UK spelling matches the wire/DB
|
||||
// literal, see api.JobCancelled) of the given kind for the host, or
|
||||
// (nil, ErrNotFound) if no such job exists. Used by the maintenance
|
||||
// ticker to compute "last fire" anchors for the cron-due check;
|
||||
// queued and running jobs are excluded so an in-flight run doesn't
|
||||
// suppress its own cron tick from firing. //nolint:misspell // wire format
|
||||
func (s *Store) LatestJobByKind(ctx context.Context, hostID, kind string) (*Job, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, host_id, kind, status, scheduled_id, actor_kind, actor_id,
|
||||
started_at, finished_at, exit_code, stats, error, created_at
|
||||
FROM jobs
|
||||
WHERE host_id = ? AND kind = ?
|
||||
AND status IN ('succeeded','failed','cancelled')
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1`, hostID, kind)
|
||||
var (
|
||||
j Job
|
||||
schedID sql.NullString
|
||||
actorID sql.NullString
|
||||
startedAt sql.NullString
|
||||
finishedAt sql.NullString
|
||||
exitCode sql.NullInt64
|
||||
stats sql.NullString
|
||||
errMsg sql.NullString
|
||||
createdAt string
|
||||
)
|
||||
if err := row.Scan(&j.ID, &j.HostID, &j.Kind, &j.Status, &schedID,
|
||||
&j.ActorKind, &actorID, &startedAt, &finishedAt,
|
||||
&exitCode, &stats, &errMsg, &createdAt); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("store: scan latest job by kind: %w", err)
|
||||
}
|
||||
if schedID.Valid {
|
||||
s := schedID.String
|
||||
j.ScheduledID = &s
|
||||
}
|
||||
if actorID.Valid {
|
||||
s := actorID.String
|
||||
j.ActorID = &s
|
||||
}
|
||||
if startedAt.Valid {
|
||||
t, _ := time.Parse(time.RFC3339Nano, startedAt.String)
|
||||
j.StartedAt = &t
|
||||
}
|
||||
if finishedAt.Valid {
|
||||
t, _ := time.Parse(time.RFC3339Nano, finishedAt.String)
|
||||
j.FinishedAt = &t
|
||||
}
|
||||
if exitCode.Valid {
|
||||
i := int(exitCode.Int64)
|
||||
j.ExitCode = &i
|
||||
}
|
||||
if stats.Valid && stats.String != "" {
|
||||
j.Stats = json.RawMessage(stats.String)
|
||||
}
|
||||
if errMsg.Valid {
|
||||
s := errMsg.String
|
||||
j.Error = &s
|
||||
}
|
||||
t, _ := time.Parse(time.RFC3339Nano, createdAt)
|
||||
j.CreatedAt = t
|
||||
return &j, nil
|
||||
}
|
||||
|
||||
// HasJobOfKind reports whether any job of the given kind exists for
|
||||
// this host, regardless of status. Used by the auto-init path on
|
||||
// agent hello to decide whether to dispatch a fresh `restic init` —
|
||||
|
||||
Reference in New Issue
Block a user