feat(job): implement robust cancel_job with cross-node NOTIFY and force-abort#52
Closed
bodymindarts wants to merge 3 commits intomainfrom
Closed
feat(job): implement robust cancel_job with cross-node NOTIFY and force-abort#52bodymindarts wants to merge 3 commits intomainfrom
bodymindarts wants to merge 3 commits intomainfrom
Conversation
…ce-abort
Adds a full cooperative cancellation system (Oban model) with:
Phase 1 — Schema: new migration adding cancelled_at column to
job_executions and a trigger that fires pg_notify('job_cancel') when
the column transitions from NULL to non-NULL. New ExecutionCancelled
and JobCancelled event variants. Poll query now filters out
cancelled pending rows.
Phase 2 — Local infrastructure: RunningJobRegistry (DashMap-backed)
tracks running jobs with CancellationToken per job. The monitor task
watches for cancel signals alongside shutdown, with configurable
cancel_timeout (default 5s) for cooperative exit before force-abort.
CurrentJob exposes cancellation_requested() / is_cancellation_requested()
for runners. JobCompletion::Cancelled variant for cooperative cancel.
Phase 3 — Cross-node signaling: PgListener subscribes to job_cancel
channel. On notification, looks up RunningJobRegistry and cancels the
token. Keep-alive handler polls for cancelled_at IS NOT NULL as
fallback for missed NOTIFY (connection drops).
Phase 4 — Public API: Jobs::cancel_job() with idempotent CancelResult
enum (CancelledWhilePending, CancelledWhileRunning, AlreadyCompleted,
AlreadyCancelled, NotFound). Pending jobs are immediately deleted and
events recorded. Running jobs get cancelled_at set + NOTIFY fired.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
642603e to
b21d128
Compare
Increase max_attempts from 50 to 100 (5s → 10s) to reduce CI flakiness. The bulk spawn test already uses 100 attempts; align this test similarly. Our robust cancel changes (RunningJobRegistry, CancellationToken, monitor task select! branches) do not affect the normal job dispatch path — the cancelled_at IS NULL filter is a no-op for new jobs, and the DashMap operations are lock-free with trivial overhead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5 tasks
* fix(entity): make cancel_job idempotent to prevent double events When a cancel signal fires, both the dispatcher (cooperative cancel) and the monitor task (force-abort timeout) can race to finalize the same job. Both paths call job.cancel_job() which unconditionally pushed ExecutionCancelled + JobCancelled events, leading to duplicate events. Now cancel_job() checks if the job is already cancelled before pushing events, making the operation safely idempotent. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(job): wrap pending cancel DELETE and events in single transaction Previously, the cancel_job() method for pending jobs performed the DELETE of the execution row on the raw pool, then opened a new transaction to record cancellation events. A crash between the DELETE and the commit would leave the execution row gone but the job entity never marked as cancelled. Now both the DELETE and event recording happen inside the same atomic operation, matching the pattern used by cancel_and_complete_job in the dispatcher. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test(job): add integration tests for cooperative cancel and force-abort Test A (cooperative cancel): Creates a runner that select!s on cancellation_requested() and returns JobCompletion::Cancelled when triggered. Verifies the job ends up cancelled and completed. Test B (force-abort): Creates a runner that blocks forever (ignores cancellation). Configures a short cancel_timeout (1 second) via JobPollerConfig. Verifies the monitor task force-aborts and records cancellation events. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(job): extract shared cancel finalization into repo helper The cancel_and_complete_job (dispatcher) and force_cancel_job (poller) functions were nearly identical: begin_op → find job → DELETE execution row → cancel_job() → update → commit. If one was updated the other would diverge. Extract finalize_cancelled_job() in repo.rs and call it from both sites. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(poller): restore compile-time query checking in poll_jobs The sqlx::query_as! macro was replaced with runtime sqlx::query_as when adding the cancelled_at IS NULL filter, losing compile-time schema validation. Restore query_as! with explicit column type overrides (id as "id: JobId") and pass job_type_strings with `as _` cast for the ANY($4) parameter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(job): rename RunningJobRegistry to CancellationTokens The struct is a map from JobId to CancellationToken — it doesn't track general running-job information (that's JobTracker's role). The new name better describes its narrow responsibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Member
Author
|
Superseded by PR #61, which has also been closed. Cancel approach is being handled at the command-center layer instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implementation (Oban model)
Generated with Claude Code