Skip to content

Handle each failed job with its own queue's exception handler in run-worker - #2850

Open
sankalpsthakur wants to merge 2 commits into
FlexMeasures:mainfrom
sankalpsthakur:fix/2526-run-worker-exception-handler
Open

sankalpsthakur wants to merge 2 commits into
FlexMeasures:mainfrom
sankalpsthakur:fix/2526-run-worker-exception-handler

Conversation

@sankalpsthakur

@sankalpsthakur sankalpsthakur commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

  • flexmeasures jobs run-worker looked up the exception handler with the whole --queue option. A worker started on several queues, as documentation/host/installation.rst sets it up (--queue "scheduling|forecasting|ingestion|reporting"), therefore got the generic fallback handler for every job: a failed scheduling job lost its traceback in the worker log, and a failed forecasting job was not counted in its meta.
  • The worker now gets one handler, handle_exception_by_queue, which passes each failed job on to the handler of the queue the job came from (job.origin), as run-job already does since Run a job once, on its own queue, in flexmeasures jobs run-job #2480. It returns the inner handler's result, because RQ stops calling further handlers when one returns False.
  • Added changelog item in documentation/changelog.rst

Look & Feel

No visible change, apart from what the worker logs for a failed job.

How to test

pytest flexmeasures/cli/tests/test_jobs.py
  • test_run_worker_handles_a_job_with_the_handler_of_its_own_queue runs run-worker --queue "scheduling|forecasting" with an in-process burst worker on a failing forecasting job, and checks that the forecasting handler counted the failure in the job's meta. On main the generic handler runs instead, which stores only the exception summary, so the assertion fails. The forecasting queue comes second in the option, so giving every job the first queue's handler would fail it too.
  • test_handle_exception_by_queue_falls_back_and_passes_on_the_result checks that a job goes to its own queue's handler, that a queue without one falls back to the generic handler, and that a False from the handler reaches RQ.

I could not run the tests myself: the suite needs a Postgres database, which I don't run locally, and Actions are disabled on my fork, so the CI of this PR is their first run. black 26.3.1 and mypy 2.3.1 (with the flags of the type-check task) pass on the two changed files, and flake8 7.1.1 with the repo's .flake8 reports nothing on the changed lines.

Further Improvements

None planned.

Related Items

Fixes #2526. The run-job part of the issue was already fixed by #2480, so this PR only changes run-worker.


AI/LLM disclosure

  • AI coding tools (Claude Code) helped write this change, its tests and this description.

Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

…worker

run-worker looked up the exception handler with the whole --queue option,
so a worker on "scheduling|forecasting", as the hosting docs set it up,
used the generic fallback handler for every job. Failed scheduling jobs
lost their traceback in the worker log, and failed forecasting jobs were
not counted in their meta.

Give the worker one handler that passes each job on to the handler of the
queue the job came from (job.origin), as run-job already does since FlexMeasures#2480.

Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 flexmeasures | 🛠️ Build #35758705 | 📁 Comparing 1e1e5a0 against latest (055d54d)

  🔍 Preview build  

4 files changed
± changelog.html
± genindex.html
± _autosummary/flexmeasures.cli.jobs.html
± api/v3_0.html

@nhoening
nhoening requested a review from BelhsanHmida November 9, 2026 16:48
@nhoening nhoening added this to the 1.2.0 milestone Nov 9, 2026

@BelhsanHmida BelhsanHmida left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks for the fix! Just the changelog wording.


* Hovering anywhere over a session in the Charge Points chart shows its tooltip again, listing all of the session's timestamps, as it did before the move to ECharts, without highlighting another charger's session; also, a chart's tooltip no longer shows a point from a neighbouring subplot [see `PR #2829 <https://www.github.com/FlexMeasures/flexmeasures/pull/2829>`_]

* Handle each failed job of a worker on several queues, such as one started with ``flexmeasures jobs run-worker --queue "scheduling|forecasting"``, with the exception handler of the job's own queue, so a failed scheduling job shows its traceback in the worker log [see `PR #2850 <https://www.github.com/FlexMeasures/flexmeasures/pull/2850>`_]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

RQ already logs the full traceback before any handler runs, so the traceback isn't what this fixes. The gain is on the forecasting side:

Suggested change
* Handle each failed job of a worker on several queues, such as one started with ``flexmeasures jobs run-worker --queue "scheduling|forecasting"``, with the exception handler of the job's own queue, so a failed scheduling job shows its traceback in the worker log [see `PR #2850 <https://www.github.com/FlexMeasures/flexmeasures/pull/2850>`_]
* Handle each failed job of a worker on several queues, such as one started with ``flexmeasures jobs run-worker --queue "scheduling|forecasting"``, with the exception handler of the job's own queue, so a failed forecasting job counts its failures in its meta and, if an automation created it, is recorded as failed in the automation run [see `PR #2850 <https://www.github.com/FlexMeasures/flexmeasures/pull/2850>`_]

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

run-worker with a multi-queue --queue string registers the wrong exception handler

3 participants