Repository navigation
fix(inmemory): move next_run on when fetch_schedule picks a schedule - #998
Merged
janbjorge merged 1 commit intoOct 22, 2026
Merged
Conversation
PostgreSQL sets next_run to now + delay when it picks a schedule. The in-memory adapter left it in the past, so the schedule was due again on every poll and a slower schedule ran each time the scheduler woke for a faster one.
Owner
|
Thanks once more 🧞♂️ @ericdelorefice ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Agentic
Summary
InMemoryQueries.fetch_schedule()picks a due schedule but leaves itsnext_runwhere it was. PostgreSQL'sfetch_schedulesetsnext_run = date_trunc('seconds', NOW() + delay)in the same UPDATE, on both the due branch and the stale-heartbeat branch. In memory,set_schedule_queued()then puts the schedule back withnext_runstill in the past, so the next poll picks it again.SchedulerManager.run()sleeps until the soonest schedule is due, so with two schedules on different cadences the slower one runs every time the scheduler wakes for the faster one. With* * * * *and*/2 * * * *registered on a real clock for 250 seconds:At the query level, against PyPI 1.6.0 and main (a4e26ee): insert a schedule due now,
fetch_scheduleit with a one-hour delay,set_schedule_queued, and fetch again. PostgreSQL (asyncpg and psycopg, PostgreSQL 16.2) movesnext_runby one hour and returns nothing on the second fetch; in memorynext_rundoes not move and the second fetch picks it again. Nothing was mocked or injected.The in-memory reference page says
SchedulerManagerworks unchanged against this adapter, so this looked like a gap rather than a choice. I may be missing a reason it was left out.Change
Both pick branches in
InMemoryQueries.fetch_schedule()now setnext_runto now plus the delay passed in, truncated to whole seconds as the SQL does withdate_trunc(+2 lines, one file). Release note added under v1.7.0, Fixed.Intended behaviour change: under the in-memory adapter a picked schedule is not due again until its delay has passed, as in PostgreSQL.
Testing
uv sync --frozen && uv run ruff check . && uv run ruff format . --check && uv run lint-imports && uv run mypy . && uv run pytestpassed (PostgreSQL 16.2 throughEXTERNAL_POSTGRES_DSN, Python 3.12; mypy run asmypy . --no-incremental): 1483 passed, 10 skipped; 1479 passed, 10 skipped on main.next_runmoves to now + delay and an immediate second fetch returns nothing, and reclaiming a stale picked schedule also movesnext_run. Two new controls pass on both: an in-memory schedule is picked again once itsnext_runcomes due, and a PostgreSQL test pins the samenext_runcontract forQueries.Checklist