Skip to content

Keep recurring tasks running after a worker dies mid-run - #555

Closed
evantahler wants to merge 1 commit into
mainfrom
claude-evan/keen-bell-dl3u31
Closed

evantahler wants to merge 1 commit into
mainfrom
claude-evan/keen-bell-dl3u31

Conversation

@evantahler

Copy link
Copy Markdown
Member

closes #554

Problem

Recurring tasks (task.frequency) used node-resque's JobLock with a 1-hour lock and reEnqueue: false. If a process died mid-run (a failed initializer start(), OOM, SIGKILL), its workerslock key stayed behind. On the next boot, enqueueAllRecurrent() enqueued the job, JobLock refused it and dropped it, and the perform wrapper's finally → enqueueRecurrent() never ran. The clock stayed silent until the lock expired, and after that it stayed silent until the next boot.

Changes

  • New RecurringTaskLock plugin (packages/keryx/util/recurringTaskLock.ts) replaces JobLock for recurring tasks:
    • It takes a short lease (config.tasks.recurringLockTimeout, default 15 s, env TASK_RECURRING_LOCK_TIMEOUT) with a per-run token. The lease is renewed every third of its length while the job runs and released (token-checked) in afterPerform. A dead worker's lock now lapses within one lease, not an hour.
    • When it refuses a run, it re-arms the chain with api.actions.enqueueRecurrent(action), so the task is retried every frequency instead of being dropped.
    • It keeps the same Redis key as JobLock, so a lock left by an older version still prevents an overlapping run during an upgrade.
  • Plugin order: QueueLock now runs before the run lock, so the queue lock is released as soon as the job is popped. Before, a refused run left a stale resque:lock:* key that would also have blocked the re-enqueue for an hour.
  • api.start() cleanup: when an initializer's start() throws, the initializers that already started (plus the one that failed) are stopped in reverse order, best-effort, before the SERVER_START error is thrown. A failed boot no longer leaves resque workers to die mid-job.
  • Docs: added recurringLockTimeout to the config table and a short note in the tasks guide.
  • Version bumped to 0.48.3.

Tests

  • resque.test.ts: a recurring task with an orphaned lock (simulating a dead worker) runs once the lease lapses. This test fails on main. A second test checks that the lock is renewed through a run several leases long and that no second run overlaps it, with 2 workers.
  • API.test.ts: a failed start stops the started initializers in reverse order, skips the ones that never started, and still surfaces the start error when a stop() also throws.
  • Locally, the package suite (1062 pass), resque-admin and docs tests pass. The one failure, oauthHandlers "unreachable document", also fails on main in my sandbox because of its network proxy.

Generated by Claude Code

Recurring tasks used node-resque's JobLock with an hour-long lock and
reEnqueue: false. If a process died mid-run, the orphaned lock made the
next boot's enqueue get refused and dropped, which ended the recurring
chain until the lock expired.

- Replace JobLock with RecurringTaskLock: a short lease
  (config.tasks.recurringLockTimeout, 15s) renewed while the job runs,
  which re-arms the chain when it refuses a run.
- Run QueueLock first so its lock is released on pop even when the run
  is refused.
- When an initializer fails during api.start(), stop the initializers
  that started (best-effort) before throwing.

closes #554

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015fc4xNZMVrBPdBEhZpgNCR
@evantahler evantahler closed this Oct 23, 2026
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.

A recurring task stops for an hour after a worker dies mid-run (orphaned JobLock)

2 participants