kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Adopt Kolibri's supervisor ownership and reconciliation API for WorkManager-dispatched jobs

Open rtibbles opened this issue 6 months ago • 4 comments

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

Kolibri is gaining a supervisor ownership and reconciliation mechanism for the task runner (supervisor registry, supervisor_id ownership on jobs, and reconcile_stalled_jobs). On Android, jobs are dispatched by WorkManager rather than Kolibri's in-process worker pool, so a Kolibri job row can be left stuck in RUNNING when Android kills the worker process. Kolibri now exposes an API for an external dispatcher to take ownership of jobs and to reconcile against an externally supplied live set; this task adopts it in the Android task layer.

Complexity: Low Target branch: main

Context

Kolibri-side API (in the kolibri repo, PR link to follow):

  • execute_job(job_id, ..., supervisor_id=None) — when given, the job is marked RUNNING and owned by supervisor_id before execution, with ownership preserved through the bare re-mark inside Job.execute(). If the job is already owned by a different supervisor the claim fails and execute_job returns without running it, so a job must be reconciled (its dead owner cleared) before it can be re-dispatched. A job with no owner (NULL) is claimable, so rows left by the pre-update taskworker still run.
  • Storage.reconcile_stalled_jobs(supervisor_stale_threshold=None, live_supervisor_ids=None) — exactly one kwarg. With live_supervisor_ids, SELECTED and RUNNING jobs whose owner is not in the set (or that have no owner) are requeued to QUEUED, and such CANCELING jobs are finalized to CANCELED. The registry is not used as a liveness source in this mode; reconcile clears any registry rows it finds, and Android's is always empty.
  • Requeues and finalizations run through registered job hooks' update(state=...); initial scheduling runs schedule. The Android hook keys off update(state=QUEUED) to re-enqueue (see The Change).

Reconciliation is at-least-once: a job wrongly judged dead may still finish in parallel with its requeued run, so tasks are expected to be idempotent.

The Change

  • taskworker.execute_job takes ownership of the job using the WorkManager request id as the supervisor identity. The request id survives WorkManager's own retries, so a retried request keeps owning its job rather than triggering a false requeue.
  • AndroidJobHook enqueues to WorkManager on the transition into QUEUED, not only on schedule. schedule()'s Task.enqueueOnce body moves into a shared helper that both schedule() and update() call; update() invokes it when state == State.QUEUED. This fires only on a genuine transition — core's identical-re-mark guard suppresses the hook for a no-op QUEUED→QUEUED update, so an already-queued row is not re-enqueued. A reconcile requeue therefore re-enters WorkManager immediately, exactly as a freshly scheduled job does.
  • task_reconciler runs reconcile_stalled_jobs as the first step of _do_reconciliation, passing the active WorkManager request ids as the live set. Clearing the dead owner first is required, or the fenced execute_job would refuse to re-run the job; the requeue then re-enqueues via the hook above.
  • The existing WorkManager↔queue sync is retained as the queue-membership reconciler — a different axis from reconcile_stalled_jobs, which only handles ownership/liveness of supervised states. Kolibri's DB and WorkManager are two stores with no shared transaction, so they drift: it re-enqueues QUEUED jobs that never reached WorkManager (a failed or lost enqueueOnce, or WM eviction) and cancels WorkManager work for jobs Kolibri no longer has. On Android this drift is routine (force-stop, app update, doze, WM dropping work), so the sync stays load-bearing, not optional.

Out of Scope

  • Narrowing the membership sync's "missing" check to QUEUED now that reconcile_stalled_jobs handles the supervised states first — an optional tidy, not done here.
  • Supervisor registry rows and heartbeats — Android never uses the registry as a liveness source (WorkManager is the authority); reconcile simply clears any rows it finds.

Acceptance Criteria

  • [ ] A job executed via WorkManager is owned by its request id for the duration of execution (supervisor_id set while RUNNING, cleared on completion).
  • [ ] A RUNNING job whose WorkManager work no longer exists is requeued by reconciliation and re-enqueued into WorkManager via the job hook's QUEUED handling.
  • [ ] A CANCELING job whose WorkManager work no longer exists is finalized to CANCELED by reconciliation rather than left stuck.
  • [ ] A job whose WorkManager request is retried by WorkManager is not requeued while the retry is pending.
  • [ ] RUNNING rows with no owner (left by the pre-update taskworker) are requeued on first reconciliation.
  • [ ] Verified on-device: kill the app mid-task and confirm the task resumes after reconciliation runs.

References

  • kolibri/core/tasks/worker.py and kolibri/core/tasks/storage.py in the kolibri repo (supervisor ownership branch; link the PR once open).

AI usage

Drafted with Claude Code from a design discussion during review of the Kolibri-side changes, and revised once that review settled the final API; the API description and integration points were verified against both codebases and edited by hand.

rtibbles avatar Jun 04 '26 22:06 rtibbles

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jul 23 '26 05:07 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jul 23 '26 23:07 rtibblesbot

The bot has been waiting for a response for 2 hour(s). It will resume automatically when someone replies.

rtibblesbot avatar Jul 24 '26 07:07 rtibblesbot

This was fixed in #15051

rtibbles avatar Aug 11 '26 22:08 rtibbles