ductor icon indicating copy to clipboard operation
ductor copied to clipboard

Cron reschedule cancels currently executing jobs

Open alexeymorozua opened this issue 8 months ago • 1 comments

Bug

When cron_jobs.json is modified while a cron job is executing, the file-watcher triggers _reschedule_all() which cancels all scheduled tasks — including the currently running one.

Reproduction

  1. Have a cron job running (e.g. play-earnings-report at 20:00)
  2. Add or modify another cron job in cron_jobs.json (e.g. create a new one-time reminder)
  3. The file-watcher detects the change and calls _reschedule_all()
  4. _reschedule_all() cancels all tasks in self._tasks, including the executing one
  5. The running job is terminated mid-execution

Expected behavior

Currently executing jobs should not be cancelled when rescheduling. Only pending (not-yet-started) jobs should be cancelled and rescheduled.

Root cause

In ductor_bot/cron/observer.py, _reschedule_all() iterates over self._tasks and cancels every task unconditionally:

for task in self._tasks.values():
    task.cancel()
self._tasks.clear()

There is no distinction between a task that is waiting for its scheduled time vs. one that is currently executing _execute_job().

Suggested fix

Track currently executing job IDs in a set (e.g. self._executing). In _reschedule_all(), skip tasks whose job ID is in _executing. In _run_at(), add the job ID to _executing before calling _execute_job() and remove it after.

Logs

2026-03-12 20:18:26 INFO  Detected cron_jobs.json change, rescheduling
2026-03-12 20:18:26 INFO  CronObserver: cancelled 7 pending tasks

The play-earnings-report job that started at 20:00 was killed at 20:18 when a new cron was added.

alexeymorozua avatar Mar 12 '26 21:03 alexeymorozua

Fixed in v0.15.0. I added an _executing set to track running jobs. _reschedule_all() now skips executing jobs, and _run_at() checks job.enabled before rescheduling. Also added a pre-execution enabled check as double safety.

PleasePrompto avatar Mar 14 '26 13:03 PleasePrompto