Repository navigation
Allow flagging automations as critical, with a failure hook - #2856
Merged
Merged
Conversation
Some automations matter more to an organisation than others. Flag one as critical, and a failed run of it is handed to hooks the host implements, where its own business logic decides what happens next - the issue's example being a hook that pages the engineer on duty. Criticality changes nothing about how an automation runs or how its failures are retried. It decides only who is told. The hooks are called when FlexMeasures has stopped trying: when a job of a run failed on a worker, which is not retried, or when a run's last dispatch attempt has been spent, so that a dispatch the next retry would have fixed pages nobody. A run notifies once, however many of its jobs fail, which is recorded on the run itself so that a second dispatcher or worker does not page again. A hook that raises is logged and skipped, and never costs us the record of what failed. Register a hook from a plugin, through __automation_failure_hooks__, or name it by dotted path in FLEXMEASURES_CRITICAL_AUTOMATION_FAILURE_HOOKS, for a host that would rather not write a plugin. The flag is exposed in the CLI (--critical, --critical/--not-critical), in the API (the critical field) and in the UI (a checkbox in both forms, and a badge on the automation's row). Also: a plugin which registers no Blueprint was dropped before any of its registrations ran, so a plugin existing only to register hooks could not register at all. The same already applied to __automation_types__. Refs #2844 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Documentation build overview
56 files changed ·
|
2 tasks done
Claim the notification with a conditional UPDATE instead of checking the loaded run, so two workers failing two jobs of one critical run cannot both call the hooks. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Only forecasts and schedules are dispatched again, so for any other type the first failed dispatch is already the last one. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…tring As FLEXMEASURES_PLUGINS does; a plain string was read one character at a time, so no hook was called. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Hooks share the caller's session, and a hook whose query failed left it in an aborted transaction, so recording the outcome of the run's next job failed too. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…tomations` Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…er punctuation Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
BelhsanHmida
approved these changes
Nov 18, 2026
BelhsanHmida
left a comment
Contributor
There was a problem hiding this comment.
looks good! tested locally with a hook that logs every page, a dispatcher on cron and two forecasting workers. CLI, API, UI badge and edit form all work, and a failed forecast pages once. I found a few cases that paged wrong and pushed fixes for them:
- Two jobs of one run failing at the same moment paged twice, every time. The notified check is now a conditional
UPDATE. - A critical report or plugin automation never paged on a failed dispatch, since those types aren't retried and never reach attempt 5. They now page on the first failure.
- The hooks setting as a plain string was read one character at a time, so no hook ran. It now splits on commas like
FLEXMEASURES_PLUGINS. - A hook whose query failed left the session aborted, which broke recording the run's next job. It now rolls back after each hook.
Also added Critical to flexmeasures show automations, updated the PATCH docs, a test for the no-Blueprint plugin path.
Keep both of the run's new timestamps, alerted_at from the monitor digest and critical_failure_notified_at, and both sides' changelog entries, under one v3.0-35 section in the API change log. Put the critical automations migration after the monitor's, as both started from bf22b21d0990. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
14 tasks done
Flix6x
added a commit
that referenced
this pull request
Nov 25, 2026
Brings in #2856 (critical automations). Both PRs add fields next to each other: - `critical` sits next to the edit fields and `--run-as` in the CLI, the update service, the schema and the edit form; creating an automation passes `critical` through the PR's split of preparing and creating. - Criticality and monitoring changes move into `_set_criticality` and `_set_monitoring`, which leave the automation as it is for None, keeping `update_automation` within the complexity limit. - #2856's test reads the run's and the automation's `revision`, the column this PR renamed from `schedule_revision`. - A merge migration (37aeaf745caa) joins this PR's 3a259005fb95 and #2856's d4a17c6b3e92 into one head. - The OpenAPI specs are regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <felix@seita.nl>
3 tasks done
BelhsanHmida
added a commit
that referenced
this pull request
Nov 25, 2026
Resolved conflicts with critical automations (#2856): an automation takes depends-on and critical together, in the API, the CLI and the page. When a run ends, the critical-failure hooks are told first, and then the runs waiting for it are released. A dispatch counts as given up by the same rule that releases those runs (see _dispatch_was_given_up), so a run on demand, which is never dispatched again, reaches the hooks, too. The dependencies migration now follows d4a17c6b3e92. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
4 tasks done
Flix6x
added a commit
that referenced
this pull request
Nov 30, 2026
- Changelogs: v1.1.3 and API v3.0-37 have been released since this PR's entries were placed, so main's versions are kept, with this PR's entries in the unreleased sections (v1.2.0 Bugfixes, API v3.0-38). - test_asset_schedules_fresh_db.py: both sides appended tests; both are kept, with main's datetime import. - test_automations_fresh_db.py: `revision=automation.revision`, after the rename in #2779. - Since #2920, a soc-usage beyond what a device can deliver is left out at a price while SoC relaxation is on, so the infeasible device in this PR's tests was no longer infeasible; they now switch SoC relaxation off. - Add a test that a critical sequential schedule automation with an infeasible device calls the failure hooks once, with the device's error (#2856 met this PR's recording of every job in the chain as failed). 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>
This was referenced Dec 1, 2026
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.
Closes #2844
What this does
Some automations matter more to an organisation than others. This lets you flag an automation as critical, and hands a failed run of a critical automation to hooks the host implements, where its own business logic decides what happens next, such as paging the engineer on duty, as in the issue's example.
Criticality changes nothing about how an automation runs or how its failures are retried. It only decides who is told.
When the hooks are called
When FlexMeasures has stopped trying:
A run notifies once, however many of its jobs fail. That is recorded on the run (
automation_run.critical_failure_notified_at), so a second dispatcher or worker does not notify again. A hook that raises is logged and skipped, and never costs us the record of what failed.Registering a hook
A hook is a callable taking one
CriticalAutomationFailure: plain values naming the automation, its asset, the run, the stage and the error, plus a one-linesummary.__automation_types__:Where the flag shows up
flexmeasures add automation --critical,flexmeasures edit automation --critical/--not-criticalcriticalfield on create, update and in responses;critical-failure-notified-atper run inrun-statscriticalbadge on the automation's rowfeatures/automations.rst, and entries in all three changelogsFlagging an automation critical does not reset its cursor or count up its schedule revision, as it does not change which runs are due.
Also in here: a plugin loader fix
register_pluginsdropped a plugin that has no Blueprint before running any of its registrations. A plugin that exists only to register hooks, or only to register automation types through__automation_types__, was silently ignored. This now logs that the plugin registers no routes and carries on. It can be split into its own PR if preferred.Testing
flexmeasures/data/tests/test_critical_automations_fresh_db.py, covering: default off, the CLI flag, editing without disturbing the recurrence, execution failures notifying once and only for critical automations, dispatch failures notifying only once the attempts are spent, a raising hook not breaking the failure record, and hooks named by dotted path (including a mistyped one being logged and skipped).test_automations_fresh_db.pysegfault inside the HiGHS solver. They crash the same way on a cleanmain, so I skipped them locally; CI will run them.Open questions
flexmeasures jobs run-automation, Run now) create noautomation_runrecord, so a failure there reaches no hook. Should they?🤖 Generated with Claude Code