Skip to content

Allow flagging automations as critical, with a failure hook - #2856

Merged
nhoening merged 12 commits into
mainfrom
feat/2844-critical-automations
Nov 24, 2026
Merged

nhoening merged 12 commits into
mainfrom
feat/2844-critical-automations

Conversation

@Ahmad-Wahid

Copy link
Copy Markdown
Contributor

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:

  • Execution: a job of the run failed on a worker. Those are not retried, so the hooks are called at once.
  • Dispatch: the run's last dispatch attempt has been spent. A dispatch failure that the next retry might fix notifies nobody.

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-line summary.

  • From a plugin, next to __automation_types__:
    __automation_failure_hooks__ = [page_the_engineer_on_duty]
  • Or by dotted path in the server settings, for a host that would rather not write a plugin:
    FLEXMEASURES_CRITICAL_AUTOMATION_FAILURE_HOOKS = ["my_company.ops.page_the_engineer_on_duty"]

Where the flag shows up

  • CLI: flexmeasures add automation --critical, flexmeasures edit automation --critical/--not-critical
  • API: a critical field on create, update and in responses; critical-failure-notified-at per run in run-stats
  • UI: a Critical checkbox in the create and edit forms, and a critical badge on the automation's row
  • Copies: a copy of a critical automation is critical too
  • Docs: a "Critical automations" section in features/automations.rst, and entries in all three changelogs

Flagging 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_plugins dropped 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

  • 10 new tests in 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).
  • The existing automation, run and API suites pass. On my machine (macOS arm64), three scheduling tests in test_automations_fresh_db.py segfault inside the HiGHS solver. They crash the same way on a clean main, so I skipped them locally; CI will run them.
  • End to end against a local stack: an example plugin's hook paged once for a failing forecast job, and once for a run whose dispatch kept failing, only after the fifth attempt. Nine sibling runs of the same automation, still at attempt 4, notified nobody.

Open questions

  1. On-demand runs (flexmeasures jobs run-automation, Run now) create no automation_run record, so a failure there reaches no hook. Should they?
  2. Dispatch timing: with the current backoff, a dispatch failure notifies about 11 minutes after the first attempt. That keeps blips quiet, but may be slow for something critical.
  3. Hooks run in-process, inside the dispatcher or worker that recorded the failure. The docs ask for quick hooks and anything slow on a queue of the host's own; an async dispatch could come later.

🤖 Generated with Claude Code

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>
@Ahmad-Wahid Ahmad-Wahid self-assigned this Nov 10, 2026
@Flix6x Flix6x mentioned this pull request Nov 12, 2026
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 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! 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>
@nhoening nhoening added this to the 1.2.0 milestone Nov 24, 2026
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@nhoening
nhoening merged commit ed762a4 into main Nov 24, 2026
11 checks passed
@nhoening
nhoening deleted the feat/2844-critical-automations branch November 24, 2026 19:50
@Flix6x Flix6x mentioned this pull request Nov 25, 2026
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>
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>
@Flix6x Flix6x mentioned this pull request Nov 30, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allowing flagging Automations as critical

3 participants