Skip to content

scheduler: unclaimed schedule jobs expire at their claim deadline - #184

Merged
teddashh merged 1 commit into
mainfrom
feat/schedule-job-claim-deadline
Nov 21, 2026
Merged

teddashh merged 1 commit into
mainfrom
feat/schedule-job-claim-deadline

Conversation

@teddashh

Copy link
Copy Markdown
Collaborator

What

Unclaimed scheduled jobs now expire on their own at a claim deadline, instead of #183's special case that only stopped them from blocking overlap.

  • Claim deadline: one cron interval of the job's schedule plus the existing 2-minute grace (scheduleStaleNotStartedCutoff), measured from jobs.created_at. It is derived, not stored, so there is no migration.
  • Sweep: Store.ExpireScheduleJobs(now) runs at the start of every TickSchedules call (every 30 s in scheduleLoop).
    • It finds not_started jobs with no lease that belong to schedule_jobs and are past the deadline. The read query is driven by the existing jobs(state, created_at…) index.
    • It moves each one to rejected with a guarded UPDATE … WHERE state='not_started' AND lease_token IS NULL. This is the same transition the manual cancel uses.
    • It writes one schedule-job-cancel audit row per job: auth_subject=scheduler:<name>, reason=claim_deadline_expired, plus the machine id.
    • A write transaction opens only when something is due.
  • Scope: only schedule_jobs. Profile-assignment and other jobs are never touched, and neither are claimed or leased jobs.
  • Existing stuck jobs: jobs already stuck on dropped or retired targets, or under disabled schedules, are cleared on the first tick after deploy.

Removed (#183 special case)

  • the stale NOT (...) clause in the overlap count
  • scheduleStaleTargetsTx and the per-run "left out N target(s)" exclusion
  • the "every target still holds a stale unclaimed job" skip

The manual POST /v1/operator/schedule-jobs/{id}/cancel stays; it is now only needed to clear a job sooner.

Behaviour is unchanged for overlap timing. An unclaimed job still blocks exactly the slot right after its own and is gone before the slot after that. The difference is that the offline machine gets a fresh job in that run instead of being left out.

The morning report's reason text for a rejected scheduled job now reads "rejected the check or never picked it up".

Tests

  • TestScheduleJobClaimDeadline covers:
    • the job is still overlap before its deadline
    • a leased job never expires
    • an expired job is rejected and the run goes ahead on both targets
    • a non-schedule (profile) job is untouched
    • exactly one audit row with claim_deadline_expired
  • TestScheduleJobClaimDeadlineClearsExistingStuckJobs: a job on a dropped target under a disabled schedule is cleared on the first sweep, and a second sweep is a no-op.
  • The existing cancel tests and cutoff test still pass.

Deploy / rollback

  • No schema change and no migration; it is safe on the 2.8 GB DB.
  • The first tick after start rejects the existing old unclaimed scheduled jobs, with audit rows.
  • Rollback: redeploy the previous image (68ef582). Rejected jobs stay rejected, which is the same end state as a manual cancel; nothing else to undo.

A scheduled job no agent has claimed within one cron interval plus the
two-minute grace is rejected by the scheduler tick, with a
schedule-job-cancel audit row (reason claim_deadline_expired). This
replaces the #183 stale special case in runSchedule (stale-overlap
filter, left-out targets, all-stale skip); the manual cancel API stays.
@teddashh
teddashh merged commit 73916ff into main Nov 21, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant