Skip to content

feat: flexmeasures jobs run-job --direct-run, to call a job's function directly for debugging - #1927

Open
Flix6x wants to merge 17 commits into
mainfrom
feat/dry-run-one-job
Open

Flix6x wants to merge 17 commits into
mainfrom
feat/dry-run-one-job

Conversation

@Flix6x

@Flix6x Flix6x commented Jan 26, 2026 •

Copy link
Copy Markdown
Member

Description

flexmeasures jobs run-job now 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-run calls the job's function directly, in the CLI process:

  • whatever the function itself does, such as saving a schedule, still happens;
  • the job's status, result and registries are left as they were, so a failed job stays failed and can be inspected again;
  • an exception surfaces in the terminal with its traceback, instead of being handed to the queue's exception handler.

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_rq in flexmeasures.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.

  • Added changelog items in documentation/changelog.rst and documentation/cli/change_log.rst

How to test

test_run_job_with_direct_run_calls_the_function_and_leaves_the_job_as_it_was in flexmeasures/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, then flexmeasures jobs inspect-job --job <uuid> shows the job unchanged.

🤖 Generated with Claude Code

Flix6x added 10 commits January 20, 2026 12:40
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>
@Flix6x
Flix6x changed the base branch from main to feat/custom-scheduler-by-name January 26, 2026 09:10
Comment thread flexmeasures/cli/jobs.py Outdated
)
def run_job(job_id: str):
@click.option(
"--dry-run",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better: --direct-run

"dry" for me means nothing actually happens.

Base automatically changed from feat/custom-scheduler-by-name to main January 27, 2026 23:17
@nhoening

nhoening commented Aug 6, 2026

Copy link
Copy Markdown
Member

@Flix6x is this still valuable?

Flix6x and others added 2 commits December 1, 2026 10:17
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>
@Flix6x Flix6x added CLI enhancement New feature or request labels Dec 1, 2026
@Flix6x Flix6x self-assigned this Dec 1, 2026
@Flix6x Flix6x added this to the 1.2.0 milestone Dec 1, 2026
@Flix6x Flix6x changed the title Feat/dry-run one job feat: flexmeasures jobs run-job --direct-run, to call a job's function directly for debugging Dec 1, 2026
@Flix6x

Flix6x commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@nhoening yes, though smaller than it was. Most of this branch is on main by now: flexmeasures jobs run-job performs a job with a worker on its own queue. This PR now only adds --direct-run (your naming) to run-job, which calls the job's function directly.

What that adds is debugging a failed job without side effects. Rerunning it through run-job hands a failure to the queue's exception handler again (for scheduling jobs, that also fails the jobs depending on it), updates the job's status and registries, and, for an automation's job, records the run's outcome again. For a critical automation, that means paging the engineer on duty on every try. A direct run does none of that: the job stays as it was, and can be inspected again, with the traceback in your terminal and no job timeout while you're at a breakpoint.

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.

@Flix6x

Flix6x commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

A correction to my comment above: rerunning a critical automation's failed job with run-job does not page again. Paging happens once per run (critical_failure_notified_at). What a rerun does do is rewrite the automation run's record: the failure's moment and message become the rerun's, and a rerun that succeeds after a fix marks the job, and the whole run, as succeeded, which buries the original failure. A direct run records nothing, so the run stays as it failed.

The rest stands: a run-job rerun hands the failure to the queue's exception handler again (for scheduling jobs, that fails the dependent jobs again) and is subject to the job's timeout, while a direct run is neither.

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 --direct-run should refuse an automation's job.

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 while Job.perform() runs it, and the option calls job.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's recheck_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 of job.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-run opens a new on-demand run of that automation, marked as a direct run of the original run and job in its plan (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_succeeded and record_automation_job_failed map 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.

Flix6x and others added 3 commits December 1, 2026 21:00
…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>
@Flix6x

Flix6x commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Follow-ups from the self-review, pushed:

  • 1a, Job.perform() (2adf8cf): --direct-run now performs the job with RQ's Job.perform(), so the job function finds its own job (get_current_job()) and the access checks of an automation's job apply; still no worker, registries, failure callback or timeout. perform() drops the job's expiry, so run-job puts the remaining TTL back afterwards, and the help does not need a caveat.

  • 1b, a direct run of an automation's job gets its own run (9b20bd5): the job's run is found from its trigger (schedule, report, plugin) or its automation_run_id/logical_job_key kwargs (forecast). A new on-demand run is opened with _new_on_demand_run, marked plan["direct-run-of"] = {"run": <original run>, "job": <job id>} (no migration), with one job record under the same logical key. While the job runs, a ContextVar maps the original run ID to the new one in record_automation_job_started/_succeeded/_failed (and _failed_without_running, via _record_automation_job_failure). The original run stays as it was and runs waiting for it are not released. The new run:

    • is created already dispatched (queued, dispatch_completed_at and first_enqueued_at set, with one finished attempt), so the dispatcher, skip_stale_automation_runs and release_waiting_automation_run don't touch it;
    • does not page (the critical-failure hook skips direct runs);
    • is left out of flexmeasures monitor automations (alerted_at is not faked), and nothing waits for it, so it releases nothing;
    • is shown as direct-run-of in the run status (serialize_automation_run).
  • 1c, help and docstring (2adf8cf, 9b20bd5): they now say what a direct run does and does not do.

  • 2, tests (2adf8cf, 9b20bd5): test_jobs.py covers a failing job with a deferred dependent (error raised, both left queued/deferred, no stored exception, not in a failed registry), the function finding its own job, and the job keeping its TTL. The new test_jobs_fresh_db.py (the tests write to the database, so they can't share test_jobs.py's module database) covers:

    • a failing schedule/report-style job: the original run is left as it was, the new run is marked and failed, pages nobody, isn't reported, and no runner picks it up;
    • a forecast wrap-up job recording success on the new run;
    • an interrupted direct run ending its run as failed;
    • a runner tick during a direct run not marking it as a lost job.

    Each new test fails with its change reverted. The existing direct-run test needed no change.

  • 3, changelogs (f23126e): the main and CLI entries now say that a new on-demand run records a direct run of an automation's job and the original run is left as it was. The API changelog adds direct-run-of in run-stats.

Choices the follow-ups did not settle:

  • The new run's job record can't reuse the job's RQ ID (automation_run_job.rq_job_id is unique), so it gets direct-run-<run id>-<job id>.
  • That ID isn't in Redis, so the runner's reconciliation of unfinished jobs skips direct runs. Otherwise a runner tick during a direct run would fail it as a lost job.
  • The new run takes the original run's revision, parameters and generator, since the job computes what that run planned.
  • An interruption that the job doesn't record itself (for example quitting at a breakpoint) is recorded as a failure, so the run doesn't stay running.
  • A CLI process killed outright (SIGKILL) still leaves its direct run running. That run is not reported as stuck.
  • Optional, not done: a test that an automation's job is refused under --direct-run on a sensor it isn't cleared for. With perform() the guard applies the same way as on a worker.

Not in this PR (as noted): run-job (worker path) exits 0 when the job fails.

@Flix6x
Flix6x marked this pull request as ready for review December 1, 2026 20:24
Flix6x and others added 2 commits December 3, 2026 17:10
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 Flix6x left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked after the follow-ups to my review (2adf8cf, 9b20bd5, f23126e), by running the review's probe jobs again:

  • --direct-run performs the job with Job.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 by flexmeasures 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.

@Flix6x

Flix6x commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

Follow-ups from the review:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLI enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants