Repository navigation
feat(worker): PLTF-3689 optional scheduled jobs behind OH_PROCRASTINATE_SCHEDULED_JOBS_ENABLED - #699
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||
Mutation reviewMethod: hand-written mutants were applied one at a time to the code this PR adds ( Controls (the PR's central claims)
Other mutants caught: invalid flag value treated as false; case-sensitive parsing; The suite works because the startup tests run the real Survivors
M11, M12, M15: the settled limits never reach the child runner under test
A fake @pytest.mark.parametrize('superseded, message', [
(False, 'scheduled_jobs.succeeded'),
(True, 'scheduled_jobs.superseded'),
])
async def test_the_task_hands_the_jobs_settings_to_the_child_runner(
monkeypatch, caplog, superseded, message
):
calls = []
async def fake_run_child_job(module, **kwargs):
calls.append((module, kwargs))
return SimpleNamespace(superseded=superseded, attempts=2)
monkeypatch.setattr(scheduled_jobs_module, 'run_child_job', fake_run_child_job)
logger: logging.Logger | None = scheduled_jobs_module._logger
while logger is not None:
monkeypatch.setattr(logger, 'propagate', True)
logger = logger.parent
[item] = configure(
{
'OH_JOB_RESEND_SYNC__ENABLED': 'true',
'OH_JOB_RESEND_SYNC__ACTIVE_DEADLINE_SECONDS': '120',
'OH_JOB_RESEND_SYNC__BACKOFF_LIMIT': '5',
}
)
app = App(connector=InMemoryConnector())
register_scheduled_jobs(app, [item])
context = SimpleNamespace(job=SimpleNamespace(id=7), app=None)
with caplog.at_level(logging.INFO):
await app.tasks['scheduled_jobs:resend_sync'].func(context, timestamp=0)
[(module, kwargs)] = calls
assert module == 'sync.resend_keycloak'
assert (kwargs['deadline_seconds'], kwargs['backoff_limit']) == (120, 5)
assert kwargs['env'] == item.env
[record] = [
r for r in caplog.records if r.getMessage().startswith('scheduled_jobs.')
]
assert (record.getMessage(), record.attempts) == (message, 2)Verified: passes on this branch unmodified; fails with M11, M12 and M15 each applied. M18: reserved settings leak into the child environment
def test_the_reserved_job_settings_never_reach_the_child():
[item] = configure(
{
'OH_JOB_RESEND_SYNC__ENABLED': 'true',
'OH_JOB_RESEND_SYNC__SCHEDULE': '0 4 * * *',
'OH_JOB_RESEND_SYNC__BACKOFF_LIMIT': '1',
'OH_JOB_RESEND_SYNC__ACTIVE_DEADLINE_SECONDS': '60',
}
)
assert not {'SCHEDULE', 'BACKOFF_LIMIT', 'ACTIVE_DEADLINE_SECONDS'} & set(item.env)Verified: passes on this branch unmodified; fails with M18 applied. M19, M20: blank values
def test_blank_values_fall_back_to_the_defaults():
assert scheduled_jobs_enabled({FLAG: ' '}) is False
[item] = configure(
{
'OH_JOB_MAINTENANCE_TASKS__ENABLED': 'true',
'OH_JOB_MAINTENANCE_TASKS__BACKOFF_LIMIT': '',
'OH_JOB_MAINTENANCE_TASKS__ACTIVE_DEADLINE_SECONDS': ' ',
}
)
assert (item.job.deadline_seconds, item.job.backoff_limit) == (1800, 3)Verified: passes on this branch unmodified; fails with M19 and with M20 applied. No hangs and no equivalent mutants. This comment was generated by an AI assistant on behalf of the user. |
|
The killing tests from the mutation review were added in a648a33 (test file only, no source changes).
The unmodified branch passes: This comment was generated by an AI assistant on behalf of the user. |
aivong-openhands
left a comment
There was a problem hiding this comment.
Taste rating: good. The change is small and opt-in, and it reuses the existing child runner, housekeeping and procrastinate's periodic and queueing-lock machinery. The tests check real behavior.
Material finding
A malformed cron schedule passes validation and then stops the running worker (inline comment on scheduled_jobs.py). This contradicts the PR's claim that invalid settings stop the worker at startup.
Checked, no issue
- With the flag off, no tasks are registered and only
defaultis consumed. - Each child gets the worker's base env plus only its own
OH_JOB_<JOB>__*settings. TheOH_JOB_prefixes cannot overlap, because each ends in__. lockandqueueing_lockgive at most one run waiting and one running. Procrastinate'sdefer_jobsskips onAlreadyEnqueued, and housekeeping fails stalledscheduled_jobsjobs.MAINTENANCE_TASK_CLAIM_LEASE_SECONDSis not read onmainyet. The PR notes say #691 and #692 must land before those jobs are enabled, so I treat this as deployment ordering and not a defect.- CI: build, lint, title and version checks pass on this head. Python tests and budget regressions were still pending when I looked.
- I could not run the tests myself because there is no Docker daemon for the test Postgres.
[RISK ASSESSMENT]
[Overall PR]
Verdict: COMMENT. This would be an approve once the cron validation is tightened, which is a one-line fix plus a test. It is not blocking.
Key insight: the config layer validates its own settings, but procrastinate parses cron expressions later, inside the running worker. Any setting procrastinate checks lazily needs the same validation up front.
Improve this review? If feedback seems incorrect or irrelevant, update the repository's
.agents/skills/custom-codereview-guide.md(with the/codereviewtrigger), then re-request review. The reviewer reads the guide from the PR head.Resolve with AI? Install the iterate skill and run
/iterate.Was this review helpful? React with 👍 or 👎.
This review was generated by an AI agent (OpenHands) on behalf of the user.
|
Addressed the review feedback in 482e9f2.
The inline thread is resolved. This comment was created by an AI agent (OpenHands) on behalf of the user. |
Local validation — real worker running scheduled jobsWhat this checks: with Setup: the real worker (
|
…TE_SCHEDULED_JOBS_ENABLED With the flag true (default false), the worker registers each CronJob job enabled with OH_JOB_<JOB>__ENABLED as a periodic task scheduled_jobs:<job> on the scheduled_jobs queue, with lock and queueing_lock scheduled_jobs:<job>, and also consumes that queue. Each task runs the job's module through run_child_job. OH_JOB_<JOB>__<NAME> settings reach only that job's child; the maintenance claim lease follows the longer budget deadline. worker.started logs the flag, queues and jobs.
…(mutation review)
…roniter Procrastinate parses cron lazily inside the running worker, where a bad expression stops the whole worker. Reject it at configuration time. Co-authored-by: openhands <openhands@all-hands.dev>
1f542f8 to
7eb7480
Compare
Co-authored-by: openhands <openhands@all-hands.dev>
…IGINT procrastinate handles the first signal and then restores the handler that was installed before it started. With none, the second signal killed the worker at once and left each running scheduled job's child process running. The worker now installs a handler that calls stop_all_children, so those jobs end as failed once their children are gone.
7eb7480 to
7ae186c
Compare
|
I am closing this PR together with #698. Procrastinate already gives us what this stack builds. I will open a new, smaller PR that uses This comment was created by an AI agent (OpenHands) on behalf of @aivong-openhands. |
…#874) * feat(worker): PLTF-3689 schedule the CronJob jobs with procrastinate periodic tasks Each job is a periodic task that calls its existing code in the worker, off unless OH_WORKER_SCHEDULED_JOBS_<JOB> is true. Replaces the child-process approach of #698 and #699. Co-authored-by: openhands <openhands@all-hands.dev> * chore(worker): tighten scheduled-jobs comments Co-authored-by: openhands <openhands@all-hands.dev> * fix(worker): PLTF-3689 don't retry scheduled jobs A retry returns the failed run to todo, which collides with the next run already waiting under the same queueing lock. The UniqueViolation left the failed run doing and holding its lock, so the job stopped until the worker restarted. The next scheduled run is now the retry. Co-authored-by: openhands <openhands@all-hands.dev> * test(worker): PLTF-3689 pin scheduled job wiring per mutation review - Run every job on a real worker with its CronJob entrypoint recorded, so a wrong or missing entrypoint call, a bad lazy import or resend_sync on the event loop fails (M1-M5). - Cover run_budget_maintenance.main() still running the job (M6). - Cover the worker skipping scheduled_jobs when no job is enabled (M7). - Time out each test at 60 s so a lost deadline fails instead of hanging (M8). - Give each worker test only the jobs it runs, so other jobs' periodic runs don't depend on the clock. Co-authored-by: openhands <openhands@all-hands.dev> * docs(worker): PLTF-3689 note stalled-run overlap in scheduled job comment Co-authored-by: openhands <openhands@all-hands.dev> --------- Co-authored-by: openhands <openhands@all-hands.dev>
HUMAN:
AGENT:
Why
Docker Compose installs need the seven CronJob jobs scheduled by the worker, while SaaS, Replicated and Helm keep their Kubernetes CronJobs. This PR makes worker scheduling opt-in with
OH_PROCRASTINATE_SCHEDULED_JOBS_ENABLED(default false), and then only for the jobs enabled one by one, so nothing changes for any install that doesn't set it. With the flag off, the worker registers no scheduled task and still takes jobs only fromdefault.With the flag on, the worker also takes jobs from the
scheduled_jobsqueue, the queue whose stalled jobs housekeeping already ends as failed instead of requeueing (#694). A worker that cannot run these tasks therefore never fetches them (#690).Summary
OH_PROCRASTINATE_SCHEDULED_JOBS_ENABLEDacceptstrue/1/false/0in any case; unset or empty is false, anything else stops the worker at startup. Per job,OH_JOB_<JOB>__ENABLED(default false),__SCHEDULE,__BACKOFF_LIMITand__ACTIVE_DEADLINE_SECONDSdefault to the chart's schedules and limits. Any otherOH_JOB_<JOB>__<NAME>reaches only that job's child, as<NAME>; anOH_JOB_variable that names no job is a startup error.scheduled_jobs:<job>on thescheduled_jobsqueue, withlockandqueueing_lockscheduled_jobs:<job>: at most one run waiting and one running, as the CronJobs'concurrencyPolicy: Forbiddoes. The task runs the job's module withrun_child_job, checks before each attempt that housekeeping has not ended the job, and logsscheduled_jobs.failed(error),scheduled_jobs.superseded(warning) orscheduled_jobs.succeeded.budget_maintenanceandmaintenance_tasksgetMAINTENANCE_TASK_CLAIM_LEASE_SECONDS= the longer of their deadlines + 300 s, as the claim code expects.worker.startedlogs the flag, the queues and the enabled jobs.stop_all_children. procrastinate handles the first signal (stop taking jobs, wait for running ones) and then restores the handler installed before it started; without one, the second signal killed the worker at once and left running jobs' child processes behind. Those jobs now end as failed once their children are gone.Issue Number
PLTF-3689 (OHE-3417).
How to Test
test_worker_scheduled_jobs.pycovers flag parsing; no job running unless enabled; chart defaults and per-job overrides; invalid settings and unknown job names as errors; settings isolation between children; the lease with default and overridden deadlines; task names, queue, locks and crons; worker startup with the flag off (onlydefault, no scheduled tasks, even with a job enabled), on (both queues, logged) and invalid (stops before the worker starts). On this test's Postgres, a real worker runs a registered job's module with its settings and recordssucceeded, a second defer is refused by the queueing lock, and a failing module endsfailedwithscheduled_jobs.failed; a job already ended by housekeeping starts no child. 72 passed across the four files.Two tests were checked against deliberate breakages: passing every
OH_JOB_setting to every child fails the isolation test, and registering scheduled jobs with the flag off fails the flag-off startup test.Video/Screenshots
N/A. No UI change.
Type
Notes
Stacked on #698 (the child-process runner). Kubernetes deployment values keep the flag unset; this PR changes no chart. Enabling a job against a database whose CronJobs still run it makes two schedulers run the same job: stop the other scheduler first. The maintenance and GitLab claim fixes (#691, #692) and the Resend overlap fix (#693) should be deployed before those jobs are enabled. The Compose service itself is separate work.
This PR was drafted by an AI agent on behalf of the user.
Enterprise server image for this PR: