flexmeasures.data.services.automation_failure_hooks
Hooks that a host can implement to act on the failure of a critical automation.
FlexMeasures decides nothing about what a critical failure means for an organisation: one pages the engineer on duty, another opens a ticket, a third writes to a status page. So it only reports the failure, and leaves the business logic to the host.
A hook is a callable taking one CriticalAutomationFailure. Register it either
from a plugin, by exposing callables in the module’s
__automation_failure_hooks__, orfrom server settings, by naming dotted import paths in
FLEXMEASURES_CRITICAL_AUTOMATION_FAILURE_HOOKS,
which is for a host that wants to point at a function without writing a plugin.
Hooks run inside the dispatcher or the worker that recorded the failure, straight after it was recorded and committed. They are expected to be quick and to do their own error handling; a hook that raises is logged and skipped, and never fails the run it was told about, which has failed already. Anything slow belongs on a queue of the host’s own.
Functions
- flexmeasures.data.services.automation_failure_hooks.describe_critical_failure(run: AutomationRun, stage: str) CriticalAutomationFailure
Describe a failed run the way a hook is told about it.
- flexmeasures.data.services.automation_failure_hooks.get_automation_failure_hooks() list[Callable]
Return every registered hook: the ones plugins added, then the ones settings name.
- flexmeasures.data.services.automation_failure_hooks.initialize_automation_failure_hooks(app) None
Install an empty hook registry on this application.
Called before plugins are registered, as they add to it.
- flexmeasures.data.services.automation_failure_hooks.notify_critical_automation_failure(run: AutomationRun, stage: str) bool
Hand a failed run to the critical-failure hooks, once.
Does nothing unless the automation is flagged critical. A run notifies once, however many of its jobs fail and however often its dispatch is retried, which is recorded on the run itself, so that a second dispatcher or worker does not page again.
Never raises: this is called while recording a failure, and a hook that misbehaves must not cost us the record of what failed.
- Returns:
whether the hooks were called for this run.
- flexmeasures.data.services.automation_failure_hooks.register_automation_failure_hook(app, hook: Callable) None
Register a hook to call when a run of a critical automation fails.
Classes
- class flexmeasures.data.services.automation_failure_hooks.CriticalAutomationFailure(automation_id: int, automation_name: str, automation_type: str, asset_id: int, asset_name: str, run_id: int, scheduled_at: datetime, stage: str, error_type: str | None, error_message: str | None, dispatch_state: str, execution_state: str, attempt_count: int, intended_job_count: int, queued_job_count: int)
What a hook is told about a failed run of an automation flagged as critical.
Plain values rather than ORM objects, so a hook can hand this to a pager, a webhook or a queue without holding on to a database session. Use automation_id and run_id to look the records up where more is needed.
- __init__(automation_id: int, automation_name: str, automation_type: str, asset_id: int, asset_name: str, run_id: int, scheduled_at: datetime, stage: str, error_type: str | None, error_message: str | None, dispatch_state: str, execution_state: str, attempt_count: int, intended_job_count: int, queued_job_count: int) None