Skip to content

fix: recover orphaned job status when worker pod is killed mid-execution#102

Merged
cau-git merged 7 commits intomainfrom
cau/rq-worker-add-heartbeat
Mar 3, 2026
Merged

fix: recover orphaned job status when worker pod is killed mid-execution#102
cau-git merged 7 commits intomainfrom
cau/rq-worker-add-heartbeat

Conversation

@cau-git
Copy link
Member

@cau-git cau-git commented Feb 25, 2026

When a SimpleWorker process is killed (OOMKill, node eviction, etc.), the in-flight job is permanently frozen in STARTED state because the main thread is blocked during conversion and RQ's built-in heartbeat stops being refreshed. The job previously remained stuck for 4 hours (the job timeout), with no pub/sub notification ever reaching polling or WebSocket clients.

Two components fix this:

  1. Per-job heartbeat thread in CustomRQWorker.perform_job(): a daemon thread (_heartbeat_loop) is started when perform_job() begins and stopped in the finally block. Every 20 s it writes docling:job:alive:{job_id} with a 60 s TTL to Redis via a dedicated connection. If the process is killed, the thread dies with it and the key expires naturally.

  2. Watchdog asyncio task in RQOrchestrator.process_queue(): _watchdog_task() runs alongside the existing pub/sub listener. Every 30 s it scans all STARTED tasks older than the 90 s grace period and checks whether their liveness key still exists. A missing key means the worker is dead: a FAILURE _TaskUpdate is published to the docling:updates channel, which is received by _listen_for_updates(), persisted, and delivered to WebSocket subscribers, reducing the detection window from 4 hours to ~90 s.

With this fix, a worker that is killed mid-execution will still lose the task and no retry-mechanism is in place yet, so the conversion will track as failed instead of stale.

When a SimpleWorker process is killed (OOMKill, node eviction, etc.) the
in-flight job is permanently frozen in STARTED state because the main
thread is blocked during conversion and RQ's built-in heartbeat stops
being refreshed. The job previously remained stuck for 4 hours (the job
timeout), with no pub/sub notification ever reaching polling or WebSocket
clients.

Two components fix this:

1. Per-job heartbeat thread in CustomRQWorker.perform_job()
   A daemon thread (_heartbeat_loop) is started when perform_job() begins
   and stopped in the finally block. Every 20 s it writes
   docling:job:alive:{job_id} with a 60 s TTL to Redis via a dedicated
   connection. If the process is killed the thread dies with it and the
   key expires naturally.

2. Watchdog asyncio task in RQOrchestrator.process_queue()
   _watchdog_task() runs alongside the existing pub/sub listener. Every
   30 s it scans all STARTED tasks older than the 90 s grace period and
   checks whether their liveness key still exists. A missing key means
   the worker is dead: a FAILURE _TaskUpdate is published to the
   docling:updates channel, which is received by _listen_for_updates(),
   persisted, and delivered to WebSocket subscribers — reducing the
   detection window from 4 hours to ~90 s.

Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

DCO Check Passed

Thanks @cau-git, all your commits are properly signed off. 🎉

@mergify
Copy link

mergify bot commented Feb 25, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@cau-git cau-git changed the title Cau/rq worker add heartbeat fix: recover orphaned jobs when worker pod is killed mid-execution Feb 25, 2026
cau-git added 2 commits March 3, 2026 12:23
Watchdog now queries RQ's StartedJobRegistry instead of per-pod
self.tasks to detect orphaned jobs, closing the coverage gap when
the enqueue pod is recycled during rolling updates.

Also adds _on_task_status_changed() hook (no-op by default) called
after every pub/sub status update, allowing subclasses to persist
terminal states to durable storage.

Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
…hdog

- Make `task_result()` use deterministic fallback key (`{results_prefix}:{task_id}`) when in-memory result-key cache is missing.
- Return `None` when result blob is absent and cache resolved key when present.
- Run watchdog `StartedJobRegistry.get_job_ids()` with cleanup enabled to clear abandoned STARTED entries during scans.

Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 52.11268% with 34 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling_jobkit/orchestrators/rq/orchestrator.py 40.81% 29 Missing ⚠️
docling_jobkit/orchestrators/rq/worker.py 77.27% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@cau-git cau-git marked this pull request as ready for review March 3, 2026 12:28
@cau-git cau-git changed the title fix: recover orphaned jobs when worker pod is killed mid-execution fix: recover orphaned job status when worker pod is killed mid-execution Mar 3, 2026
Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
dolfim-ibm
dolfim-ibm previously approved these changes Mar 3, 2026
Copy link
Member

@dolfim-ibm dolfim-ibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
@cau-git cau-git merged commit 4738900 into main Mar 3, 2026
10 checks passed
@cau-git cau-git deleted the cau/rq-worker-add-heartbeat branch March 3, 2026 13:10
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.

2 participants