Skip to content

fix(scheduler): fire-and-forget dispatch to prevent APScheduler skips (#132) - #328

Merged
vybe merged 1 commit into
mainfrom
feature/132-apscheduler-skip-fix
Apr 14, 2026
Merged

vybe merged 1 commit into
mainfrom
feature/132-apscheduler-skip-fix

Conversation

@vybe

@vybe vybe commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Scheduler job function now returns immediately after backend accepts dispatch
  • Polling moved to background asyncio.create_task() — no more blocking
  • last_run_at updated on dispatch (not completion) for missed-schedule detection
  • Background tasks tracked for graceful shutdown

Problem

APScheduler's max_instances=1 caused triggers to skip when the job function blocked on DB polling (up to 1hr). Even though the agent had available slots, the scheduler's Python job was still running (waiting for completion), so APScheduler refused to fire the next trigger.

Solution

True fire-and-forget: after backend accepts the dispatch, spawn a background task to poll and publish WebSocket events. The job function returns in ~30s (dispatch timeout) instead of blocking for the full execution timeout.

Changes

File Changes
src/scheduler/service.py _poll_and_finalize() background task, _active_poll_tasks tracking, immediate last_run_at update
tests/scheduler_tests/test_async_dispatch.py 4 new tests for fire-and-forget behavior
docs/memory/feature-flows/scheduler-service.md Updated flow diagram
docs/planning/ORCHESTRATION_RELIABILITY_2026-04.md Marked #132 as shipped

Test Plan

  • All 127 scheduler tests pass
  • New tests verify immediate return, background task creation, last_run_at update, graceful shutdown
  • Manual: create hourly schedule, simulate stuck execution, verify next trigger fires

Closes #132

🤖 Generated with Claude Code

…#132)

The scheduler's job function was blocking on DB polling after dispatch,
causing APScheduler to skip subsequent triggers when max_instances=1.

Changes:
- _call_backend_execute_task() now spawns asyncio.create_task(_poll_and_finalize())
  and returns immediately with "dispatched" status
- _execute_schedule_with_lock() updates last_run_at immediately on dispatch
  (for missed-schedule detection on restart)
- Background tasks tracked in _active_poll_tasks set for graceful shutdown
- 4 new tests for fire-and-forget behavior

The job function now returns in ~30s (dispatch timeout) instead of
blocking for the full execution timeout (up to 1hr), so APScheduler
never hits max_instances and never skips triggers.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@vybe
vybe merged commit ffb0afe into main Apr 14, 2026
vybe added a commit that referenced this pull request Apr 14, 2026
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.

bug: APScheduler max_instances=1 causes skipped executions despite agent having parallel slots

1 participant