kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Repeating tasks disappear from the task manager once a run finishes

Open rtibbles opened this issue 4 months ago • 6 comments

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

Overview

  • Repeating tasks disappear from the task manager the instant a run ends, so a manual KDP sync never shows a "Finished" panel.
  • Record each run's outcome on the job row and keep the task listed, reading as completed and scheduled again.
  • Applies to every repeating job, so a scheduled sync that fails and retries becomes visible too.

Complexity: Medium Target branch: develop

Context

Originally reported by @pcenov in https://github.com/learningequality/kolibri/issues/15052#issuecomment-5090270523: syncing to KDP completes correctly, but the task is removed from the task manager the moment it finishes, so the 'Finished' task is never displayed. Syncing to a peer device still shows it.

missing finished task

https://github.com/user-attachments/assets/df01c599-bd7e-48ee-a6f2-5ebd4bdaf8ba

logs.zip

Three things compose to produce this:

  1. enqueue_automatic_kdp_sync gives every KDP-registered facility a daily recurring sync with repeat=None (kolibri/core/auth/tasks.py:369).
  2. #15052 gave the manual KDP sync the same deterministic job_id, so "Sync" now runs on that recurring row instead of a fresh one-off.
  3. A finished recurring job is re-scheduled immediately: reschedule_finished_job_if_needed → schedule() sets state=QUEUED with a future scheduled_time (kolibri/core/tasks/storage.py:639, :979). The row never sits in COMPLETED, and the task manager hides repeat === null rows unless they are RUNNING (facilityTasksQueue.js:16, deliberate since a7bf6db03006942af423b177ab5b2978593fa14b).

Peer syncs behave identically once a schedule to that peer exists — the reported peer device had none, which is why it still showed 'Finished'.

The same cause produces two unreported effects:

  • A scheduled sync that fails re-schedules onto retry_interval (hourly for KDP), so a persistently failing automatic sync is invisible in the UI.
  • progress, exception, traceback and result live in saved_job and are cleared neither at reschedule nor at the start of the next run, so a run inherits the previous run's progress bar and exception.

The Change

A repeating task stays listed after each run, reading as completed and scheduled again:

Sync 'Facility name (a1b2)' — Kolibri Data Portal
Finished 2 minutes ago · next sync in 24 hours
1.2 MB sent • 340 kB received

A failed run reads Failed 5 minutes ago · retrying in 1 hour. This survives a page reload — no client-held state. A schedule that has never run stays out of the task manager; it belongs on Manage sync schedule.

Storage

  • Job gains last_finished_state (char 20, nullable) and last_finished_time (datetime, nullable), with a migration in kolibri/core/tasks/migrations/.
  • reschedule_finished_job_if_needed writes the finishing state and the current time to those columns in the same transaction as the reschedule, covering all three exits: recurrence, failure retry, and a task-requested retry_in delay.
  • mark_job_as_running clears progress, total_progress, exception, traceback and result so a new run does not inherit the last one's.
  • Those fields are not cleared at reschedule time: the queued row still describes the run that just finished.
  • extra_metadata is untouched: sync_state, bytes_sent and bytes_received are what the panel renders for the finished run.
  • restart_job clears and re-enqueues a fresh row, so it drops the snapshot.

API

  • TasksViewSet._job_to_response gains last_finished_status and last_finished_datetime, mirroring the existing state → status and scheduled_time → scheduled_datetime naming.
  • clearable stays state-derived, so a re-scheduled recurring row is not clearable.
  • "Clear completed" therefore cannot delete a schedule.

Frontend

  • facilityTasksQueue.isActiveTask shows a repeat === null row when it is RUNNING or has a last_finished_datetime.
  • facilityTasksQueue.facilityIsSyncing derives "is syncing" from !task.clearable over the active list.
  • With recurring rows always present, that pins every registered facility to a permanent "Syncing" spinner.
  • Re-base it on status === RUNNING.
  • syncFacilityTaskDisplayInfo drives the status line and byte counts off last_finished_status/last_finished_datetime for a non-running row.
  • It adds the next-run clause from scheduled_datetime.
  • Its isRunning must require status === RUNNING.
  • isRunning currently infers from extra_metadata.sync_state, which is stale (COMPLETED) on a re-queued row.
  • FacilitiesTasksPage.vue:19 tests facilityTasks.length === 0 for its empty state.
  • A device whose only task is a hidden schedule therefore renders neither tasks nor the "no tasks" message.
  • Test activeFacilityTasks there instead.
  • index.vue's isRepeatingTaskCompleted infers completion from a RUNNING → QUEUED transition.
  • Use a change in last_finished_datetime instead.
  • Manage sync schedule gains a last-run column from the same fields.
  • New translator strings are needed for the relative finished/failed status and the next-run clause.
  • FacilityNameAndSyncStatus carries an unused nextSync string — reuse or delete it.

How to Get There

  • Register a facility to the Data Portal (Device > Facilities > Register facility). This is what creates the daily recurring KDP sync.
  • Press Sync on that facility, choose Kolibri Data Portal, and watch Device > Facilities > Task manager as the sync finishes.
  • For the peer equivalent, first create a sync schedule to a peer device via Manage sync schedule, then use Sync.
  • For the failure path, make KDP unreachable (offline device) so the scheduled sync fails and re-schedules onto its hourly retry.

Out of Scope

  • The deterministic-id dedup from #15052 stays. A manual sync landing on the existing schedule is the intended behaviour.
  • Facility-level last_successful_sync / last_failed_sync (morango transfer-session subqueries in kolibri/core/auth/viewsets/facility.py) remain the facility list's source; they are per-facility, not per-schedule.
  • No UI for repeating tasks outside the facility queue (vacuum, soud). They gain the data; nothing renders it yet.

Acceptance Criteria

  • [ ] After a manual KDP sync completes, the task stays in Device > Facilities > Task manager showing the finished status, a relative finish time, the bytes sent and received, and the next sync time.
  • [ ] The entry is still present with the same information after a page reload.
  • [ ] The entry offers neither Clear nor Retry, and "Clear completed" neither removes it nor deletes the schedule.
  • [ ] A facility row shows "Syncing" only while a sync for it is actually running.
  • [ ] A scheduled sync that fails shows the failed status and the time of its next retry.
  • [ ] A second run of a repeating task starts with no progress or exception carried over from the previous run.
  • [ ] A peer device with a sync schedule behaves as above; a peer without one still shows a one-off finished entry that is clearable.
  • [ ] A schedule that has never run does not appear in the task manager.
  • [ ] The task manager shows its "no tasks" message when a never-run schedule is the only task.

Testing

  • kolibri/core/tasks/test/test_job_storage.py:
    • snapshot written on recurrence, on failure retry and on retry_in
    • no snapshot when a finished job is not re-scheduled
    • per-run fields cleared at mark_job_as_running
    • clear(force=False) leaves a re-scheduled recurring row in place
  • kolibri/core/tasks/test/test_api.py:
    • both new keys present in the response
    • clearable false for a re-scheduled recurring row
  • packages/kolibri-common/utils/__tests__/syncTaskUtils.spec.js: display info for a re-scheduled recurring row — finished, failed, and with a stale sync_state.
  • Jest coverage of the isActiveTask and facilityIsSyncing filters.

AI usage

I used Claude Code to trace the regression through the task storage and facility task UI, and to draft this issue. I confirmed each code reference and the root-cause chain myself, and decided the field-level design and scope.

rtibbles avatar Jul 27 '26 15:07 rtibbles

Plan: A repeating task (KDP/peer sync schedule) stays in the task manager after each run, reading as finished-and-scheduled-again, instead of vanishing the instant the run ends.

  • [x] Storage records the run that just ended
  • [x] A new run starts clean
  • [x] The tasks API exposes the snapshot
  • [x] Shared display logic reads the last run
  • [x] Device > Facilities keeps the row and drops the permanent spinner
  • [x] Facility > Data page stops spinning forever
  • [x] Manage sync schedule shows the last run

Implemented in #15093.


@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

How was this generated?
  • Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
  • Ran the project's full test suite and linter before committing
  • For UI changes: captured live screenshots to verify rendering
  • For backend changes: verified with example API/CLI output

rtibblesbot avatar Jul 27 '26 18:07 rtibblesbot

Acceptance criteria — all verified. Walked end to end on two live Kolibri servers: device A (:8451) owning "QA Facility", device B (:8452) having imported it, with a real peer sync schedule between them.

No Data Portal account is reachable from this environment, and registering one in dev would push real data to the live portal — so the KDP-worded criteria were exercised against an equivalent peer schedule. That is the same code path: both syncdataportal and peerfacilitysync are repeating rows re-queued by reschedule_finished_job_if_needed, and the issue notes peer syncs behave identically once a schedule to that peer exists.

This account cannot edit issue bodies (UpdateIssue is not permitted for it), so the checklist below — verbatim from the issue — is the record of the ticks.

  • [x] After a manual KDP sync completes, the task stays in Device > Facilities > Task manager showing the finished status, a relative finish time, the bytes sent and received, and the next sync time. → Finished 30 seconds ago · next sync in 1 hour / 10 KB sent • 23 KB received
  • [x] The entry is still present with the same information after a page reload. → Unchanged; the snapshot is server-side, no client-held state.
  • [x] The entry offers neither Clear nor Retry, and "Clear completed" neither removes it nor deletes the schedule. → No buttons rendered; POST /api/tasks/tasks/clearall/ returned 200 and left both row and schedule intact.
  • [x] A facility row shows "Syncing" only while a sync for it is actually running. → Sampled QUEUED → RUNNING → QUEUED; the row reads "Syncing" only across the RUNNING window.
  • [x] A scheduled sync that fails shows the failed status and the time of its next retry. → Failed 20 seconds ago · retrying in 5 minutes, counting down to the 5-minute retry_interval.
  • [x] A second run of a repeating task starts with no progress or exception carried over from the previous run. → At the transition into RUNNING: exception: null, percentage: 0; the panel reads "Waiting", not "Finished".
  • [x] A peer device with a sync schedule behaves as above; a peer without one still shows a one-off finished entry that is clearable. → Peer without a schedule produced a one-off repeat: 0 Finished entry with CLEAR.
  • [x] A schedule that has never run does not appear in the task manager. → Absent.
  • [x] The task manager shows its "no tasks" message when a never-run schedule is the only task. → Shown.

Plan items, for completeness:

  • [x] Storage records the run that just ended
  • [x] A new run starts clean
  • [x] The tasks API exposes the snapshot
  • [x] Shared display logic reads the last run
  • [x] Device > Facilities keeps the row and drops the permanent spinner
  • [x] Facility > Data page stops spinning forever
  • [x] Manage sync schedule shows the last run
Finished, on the schedule Finished
Failed, retrying Failed
Never-run schedule only Empty
Peer without a schedule — clearable One-off
Manage sync schedule Schedule
Row during a run Syncing
Row after the run Done

No code changes came out of this pass; #15093 is unchanged.


@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly

rtibblesbot avatar Jul 27 '26 22:07 rtibblesbot

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

rtibblesbot avatar Jul 28 '26 01:07 rtibblesbot

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

rtibblesbot avatar Jul 30 '26 00:07 rtibblesbot

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

rtibblesbot avatar Jul 31 '26 01:07 rtibblesbot

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

rtibblesbot avatar Jul 31 '26 10:07 rtibblesbot