Repository navigation
Conversation
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
This reverts commit a9e5c3b. Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: F.N. Claessen <felix@seita.nl>
📖 26 files changed · 2 added · 24 modified
Preview · build overview · flexmeasures · built Dec. 5, 2026, 11:27 a.m. UTC |
| ) | ||
| def run_job(job_id: str): | ||
| @click.option( | ||
| "--dry-run", |
There was a problem hiding this comment.
Maybe better: --direct-run
"dry" for me means nothing actually happens.
|
@Flix6x is this still valuable? |
Since January, main has gained what this branch did besides the dry run: `flexmeasures jobs run-job` performs the job with a worker on its own queue (registries and exception handler included), `work_on_rq` lives in job_utils, a custom scheduler can be named by its registered name, and the aggregate-power schedule is stored. So the conflicts are resolved to main throughout; the direct run follows in its own commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DWv6v3SfN2GwxVNn1ws2ep Signed-off-by: F.N. Claessen <claessen@seita.nl>
…on directly for debugging A worker now performs the job by default (with the queue's exception handler and registries), which changes the job's state. --direct-run calls the job's function in the CLI process instead: whatever the function does (such as saving a schedule) still happens, but the job's status, result and registries are left as they were, and exceptions surface with their traceback. Named --direct-run rather than --dry-run, as a dry run suggests nothing happens (review comment). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DWv6v3SfN2GwxVNn1ws2ep Signed-off-by: F.N. Claessen <claessen@seita.nl>
|
@nhoening yes, though smaller than it was. Most of this branch is on main by now: What that adds is debugging a failed job without side effects. Rerunning it through One caveat: a job function that looks up its own RQ job gets none in a direct run. The option's help could say so. |
|
A correction to my comment above: rerunning a critical automation's failed job with The rest stands: a One more caveat, besides the job not being found by a function that looks up its own RQ job: for an automation's report or schedule job, that lookup is what limits which sensors it may record on, so a direct run skips that check. And forecast jobs record on their automation run from their own arguments, so a direct run of one still updates that run. The option's help should say both, or |
Flix6x
left a comment
There was a problem hiding this comment.
A self-review, from a demo that ran the same jobs through flexmeasures jobs run-job with a worker (as on main) and with --direct-run, at 07c5000.
--direct-run is worth keeping, for debugging a failed job without the side effects of a rerun, once the caveats below are dealt with:
- A failing schedule job rerun by a worker is failed again, gets its exception stored, and its on-failure callback fails the jobs that depend on it (as #2409 does for a sequential schedule). A direct run leaves the job and its dependents as they were, and raises the error in the terminal.
- RQ's SimpleWorker enforces the job's timeout in-process, so a pause at a breakpoint can end in a
JobTimeoutException. A direct run has no timeout.
The caveats (see my comment above), which this PR should handle before it leaves draft:
- Job functions that look up their own RQ job get none in a direct run:
get_current_job()only returns a job whileJob.perform()runs it, and the option callsjob.func(*job.args, **job.kwargs)directly. For an automation's job, that lookup is what holds it to the sensors it was cleared for (sensors_automation_job_may_record_on,check_generator_results_of_job, and a report'srecheck_access_of_triggering_user): outside a job, nothing is checked. So a direct run of an automation's report or schedule job can write where the real job would be refused. - Forecast cycle jobs record on their run from their own arguments, so a direct rerun of a forecast automation's job still updates its run, while schedule and report jobs don't: the two behave differently.
We'll fix this ourselves on this branch:
- Call
job.perform()instead ofjob.func(...). That performs the job without a worker: the function can find its own job (get_current_job()), so the access checks of automation jobs (plugin jobs included) apply, while there are still no registries, no failure callback and no timeout.perform()persists the job's key, so the job loses its TTL; the help will say so. - Give a direct run of an automation's job its own automation run, so the beliefs it saves have a run that shows it ran, while the original run stays as it failed.
run-job --direct-runopens a new on-demand run of that automation, marked as a direct run of the original run and job in itsplan(no migration needed). It then performs the job with recording redirected from the original run to the new one:record_automation_job_started,record_automation_job_succeededandrecord_automation_job_failedmap the run ID while the direct run lasts. This covers schedule, report and plugin jobs, and forecast jobs, which record from their own arguments. - That new run is created as already dispatched, so the dispatcher doesn't queue it again. A failing direct run of a critical automation doesn't page.
- Say in the option's help what a direct run does and doesn't do.
We'll also add tests: a failing job (the error is raised, the job and its dependents stay as they were), a direct run in which the job function finds its own job, and a direct run of an automation's job that leaves the original run as it was and records its outcome on a new run.
This PR stays a draft until that's in.
Side finding, not for this PR: run-job exits 0 when the job it performs fails.
…on finds its own job Calling the job's function directly left `get_current_job()` without a job, so the access checks of an automation's job (which look up the job) checked nothing. `Job.perform` still uses no worker, registries, failure callback or timeout. It drops the job's expiry, which is put back afterwards. Tests: a failing job is raised and leaves itself and its dependent as they were, the function finds its own job, and the job keeps expiring. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…aving the original run as it was Performed directly, an automation's job reports its start and outcome to the run that queued it, which would rewrite a failed run's failure and release what waits for it. `run-job --direct-run` now opens a new run on demand of that automation, marked in its plan as a direct run of the original run and job (`direct-run-of`, no migration), and redirects the job's reports to it while the direct run lasts. This covers schedule, report and plugin jobs, and forecast jobs, which report from their own arguments. The new run is recorded as already dispatched, so no runner queues it; it pages nobody, is left out of `flexmeasures monitor automations` and out of the reconciliation of unfinished jobs, and releases nothing. An interruption the job does not record, such as quitting at a breakpoint, still ends it as failed. A run's status shows `direct-run-of`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…f to the API changelog Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
|
Follow-ups from the self-review, pushed:
Choices the follow-ups did not settle:
Not in this PR (as noted): |
Resolve the conflict with #2908 in automation_monitoring.py: main moved the criterion for an unreported failure into unalerted_failure_criterion, which pruning uses too, to keep failures until the monitor reported them. The exclusion of direct runs moves into that criterion, so a failed or stuck direct run, which is never reported, is pruned like any other run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
Flix6x
left a comment
There was a problem hiding this comment.
Re-checked after the follow-ups to my review (2adf8cf, 9b20bd5, f23126e), by running the review's probe jobs again:
--direct-runperforms the job withJob.perform(), so the job function finds its own job and an automation job's access checks apply, as on a worker. A failing job and the jobs depending on it are still left as they were, and there is still no timeout.- A direct run of an automation's job records its start and outcome on a new on-demand run, marked
direct-run-of, while the original run keeps its failure. The new run is not dispatched, pages nobody and is not reported byflexmeasures monitor automations. - The new tests pass, and so do the automation tests around them.
Since then, #2908 on main conflicts with this PR in automation_monitoring.py: it moved the criterion for an unreported failure into unalerted_failure_criterion, which pruning also uses to keep a failure until the monitor reported it. Keeping the direct-run exclusion outside that criterion would keep every failed direct run of a monitored automation forever. So we merged main with the exclusion moved into unalerted_failure_criterion (32c581a), and added a test that a failed or stuck direct run is pruned once it expired (1db0eb0). Both are pushed to this branch along with this comment.
With that pushed, I'll merge this once CI is green.
|
Follow-ups from the review:
|
Description
flexmeasures jobs run-jobnow has a worker perform the job on its own queue, so the queue's exception handler applies and the job's status, result and registries are updated. That is right for running a job for real, but not for debugging one.--direct-runcalls the job's function directly, in the CLI process:It's named
--direct-run, not--dry-run(as first proposed), because a dry run suggests nothing happens.Since this branch started, main has gained the rest of what it did: running the job with a worker on its own queue,
work_on_rqinflexmeasures.utils.job_utils, naming a custom scheduler by its registered name, and storing the aggregate-power schedule. Merging main therefore resolved to main throughout, and this PR now only adds the option.documentation/changelog.rstanddocumentation/cli/change_log.rstHow to test
test_run_job_with_direct_run_calls_the_function_and_leaves_the_job_as_it_wasinflexmeasures/cli/tests/test_jobs.py: the function runs once, and the job stays queued.Or by hand:
flexmeasures jobs run-job --job <uuid> --direct-run, thenflexmeasures jobs inspect-job --job <uuid>shows the job unchanged.🤖 Generated with Claude Code