Files
agent-zero/helpers/job_loop.py
Alessandro 9048880222 Catch up recurring scheduler tasks after downtime
Persist the schedule anchor and run overdue idle tasks once on the next tick, coalescing missed occurrences instead of relying on a one-minute lookback. Preserve pending work across ordinary edits, reset timing when schedules or states change, and carry the anchor through JSON, YAML and public serializers.

Recheck eligibility when claiming a run and keep one worker per task to prevent stale selections and repeated ticks from launching duplicates. Calculate cron delays in the task timezone across daylight-saving changes. Document disabled, failed and interrupted-task behavior in the scheduler and its skill.

Validate with 195 focused regression tests. In the live Docker stop/start test, the enabled marker-only task ran exactly once 93 seconds after its missed slot; the disabled task remained untouched, two extra ticks caused no replay, and existing tasks were unchanged.
2026-11-13 12:07:03 +01:00

58 lines
1.6 KiB
Python

import asyncio
from datetime import datetime
import time
from helpers.task_scheduler import TaskScheduler
from helpers.print_style import PrintStyle
from helpers import errors
from helpers import runtime
SLEEP_TIME = 60
keep_running = True
pause_time = 0
async def run_loop():
global pause_time, keep_running
while True:
if runtime.is_development():
# Signal to container that the job loop should be paused
# if we are runing a development instance to avoid duble-running the jobs
try:
await runtime.call_development_function(pause_loop)
except Exception as e:
PrintStyle().error("Failed to pause job loop by development instance: " + errors.error_text(e))
if not keep_running and (time.time() - pause_time) > (SLEEP_TIME * 2):
resume_loop()
if keep_running:
try:
await scheduler_tick()
except Exception as e:
PrintStyle().error(errors.format_error(e))
await asyncio.sleep(SLEEP_TIME)
async def scheduler_tick():
# Get the task scheduler instance and print detailed debug info
scheduler = TaskScheduler.get()
# Run the scheduler tick
await scheduler.tick()
# Run job_loop extensions (e.g. email polling)
from helpers.extension import call_extensions_async
await call_extensions_async("job_loop")
def pause_loop():
global keep_running, pause_time
keep_running = False
pause_time = time.time()
def resume_loop():
global keep_running, pause_time
keep_running = True
pause_time = 0