diff --git a/internal/store/jobs.go b/internal/store/jobs.go index 1cec113..633fcc6 100644 --- a/internal/store/jobs.go +++ b/internal/store/jobs.go @@ -193,20 +193,18 @@ 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 +// LatestJobByKind returns the most recent job (any status, including +// queued and running) 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 +// in-flight jobs MUST be considered or a long-running prune (>60s) +// would re-fire on the next tick while the first is still running. 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 ( diff --git a/internal/store/jobs_test.go b/internal/store/jobs_test.go index 46af279..9339a66 100644 --- a/internal/store/jobs_test.go +++ b/internal/store/jobs_test.go @@ -49,20 +49,41 @@ func TestLatestJobByKind(t *testing.T) { t.Errorf("want j-new, got %q", got.ID) } - // A queued job should be ignored — terminal-status filter. - queuedAt := time.Now().UTC() + // An in-flight running job must be returned — long-prune-suppresses-tick + // scenario: if a prune runs >60s the next tick must not re-fire it. + runningAt := time.Now().UTC() + if err := s.CreateJob(ctx, Job{ + ID: "j-running", HostID: hostID, Kind: "forget", + ActorKind: "system", CreatedAt: runningAt, + }); err != nil { + t.Fatalf("create running: %v", err) + } + if err := s.MarkJobStarted(ctx, "j-running", runningAt); err != nil { + t.Fatalf("mark started: %v", err) + } + got2, err := s.LatestJobByKind(ctx, hostID, "forget") + if err != nil { + t.Fatalf("LatestJobByKind 2: %v", err) + } + if got2.ID != "j-running" { + t.Errorf("in-flight running job must be returned; want j-running, got %q", got2.ID) + } + + // A queued (not-yet-started) job is also returned (it is newer than + // j-running because CreatedAt is later). + queuedAt := runningAt.Add(time.Millisecond) if err := s.CreateJob(ctx, Job{ ID: "j-queued", HostID: hostID, Kind: "forget", ActorKind: "system", CreatedAt: queuedAt, }); err != nil { t.Fatalf("create queued: %v", err) } - got2, err := s.LatestJobByKind(ctx, hostID, "forget") + got3, err := s.LatestJobByKind(ctx, hostID, "forget") if err != nil { - t.Fatalf("LatestJobByKind 2: %v", err) + t.Fatalf("LatestJobByKind 3: %v", err) } - if got2.ID != "j-new" { - t.Errorf("queued job should be ignored; want j-new, got %q", got2.ID) + if got3.ID != "j-queued" { + t.Errorf("queued job must be returned as newest; want j-queued, got %q", got3.ID) } // Different kind → ErrNotFound.