flexmeasures.data.services.automation_status

Whether an automation’s runs can start and work, kept on the automation as its status.

An automation can look fine while every run of it is refused: its run user was deactivated, lacks access to one of its sensors, a sensor in its parameters was deleted, or the plugin it needs is not installed. It can also look fine while its runs start, but their jobs fail, say because a schedule’s asset no longer has a price to schedule against. Its status says so, in one field that also says when all is well, and that new kinds of trouble can extend.

Working the status out takes resolving the automation’s sensors and checking access to each, which is too much to do for every automation in a listing. So it is stored on the automation, and kept up to date:

  • after each run’s dispatch (see note_dispatch_queued and note_dispatch_failed),

  • when a run’s jobs end (see note_run_executed),

  • when the automation is created or edited,

  • when a user whom automations run as is changed or deleted (see automations_run_by),

  • and when a sensor an automation involves is deleted.

Refused runs come first, as runs that cannot start say nothing about whether their jobs would work. Failing runs come next: the latest run of the automation’s current revision whose jobs ended, failed. An edit that starts a new revision so starts with a clean slate, as its runs have yet to show whether the edit fixed things.

Functions

flexmeasures.data.services.automation_status.assess_automation_status(automation: Automation) → AutomationStatus

Work out whether the runs of an automation can start, doing the checks a run’s dispatch does, and whether they work.

Unlike a run, this also works out the sensors of an automation that runs as nobody (one set up through the CLI), as a sensor it names may have been deleted, or the plugin of its data generator be missing. Runs that can start are failing when the latest run of the current revision whose jobs ended, failed. It does not commit or change the automation (see refresh_automation_status).

flexmeasures.data.services.automation_status.automations_run_by(user_ids: list[int]) → list[Automation]

List the automations that run as any of these users.

flexmeasures.data.services.automation_status.commit_refreshed_automation_statuses(automations: list[Automation], after: str) → None

Refresh the status of several automations and commit them, after a change that was committed already.

A database error while doing so is logged and rolled back, rather than raised, so that keeping statuses up to date never fails the change that set it off, nor what comes after it.

Parameters:

after – what happened, for the log, such as “user 12 was deactivated”.

flexmeasures.data.services.automation_status.note_dispatch_failed(run_id: int) → None

Work out an automation’s status anew after a run of it failed to queue its jobs, and commit it.

The dispatch may have failed for a reason that says nothing about whether runs can start, such as Redis being down, so the status is worked out from the automation rather than read from the error. Call this once the failure itself was committed, as working out the status may roll back the session. A database error while doing so is logged rather than raised (see commit_refreshed_automation_statuses), so that it neither replaces the failure it follows nor keeps the critical-failure hooks from being called.

flexmeasures.data.services.automation_status.note_dispatch_queued(run: AutomationRun) → None

Mark an automation as able to run, now that a run of it queued its jobs, without committing.

The run passed the checks a status stands for, but only for what the automation was when the run was planned. A run planned before the latest edit says nothing about the automation as it is now, so it leaves the status alone. Failing runs stay failing, as jobs that were just queued have yet to show whether they work (see note_run_executed).

flexmeasures.data.services.automation_status.note_run_executed(run: AutomationRun) → None

Mark an automation’s runs as working or failing, now that the jobs of a run of it ended, without committing.

Only the latest run of the automation’s current revision counts, so that a run planned before the latest edit, or one that ends after a later run did, leaves the status alone. Neither does it lift refused runs, which only the checks of a dispatch or an event lift, as jobs that were queued before the runs were refused can still end after.

flexmeasures.data.services.automation_status.note_run_executed_and_commit(run_id: int) → None

Mark an automation’s runs as working or failing after a job of a run of it ended (see note_run_executed), and commit it.

Call this once the job’s outcome itself was committed. A database error while doing so is logged and rolled back rather than raised, so that it neither loses the job’s outcome nor keeps the critical-failure hooks from being called.

flexmeasures.data.services.automation_status.record_automation_status(automation: Automation, status: AutomationStatus) → bool

Store a status on an automation, without committing.

It is only written when it changed, so that a run that goes as the one before it leaves the automation’s row alone.

Returns:

whether the status changed.

flexmeasures.data.services.automation_status.refresh_automation_status(automation: Automation) → AutomationStatus

Work out an automation’s status anew and store it, without committing.

flexmeasures.data.services.automation_status.refresh_automation_statuses(automations: list[Automation]) → None

Refresh the status of several automations, without committing.

One automation whose status cannot be worked out (say, because its asset names a module that does not exist) keeps the status it had, rather than keeping the others, or the change that set this off, from going through.

flexmeasures.data.services.automation_status.serialize_automation_status(automation: Automation) → dict[str, str | None]

Render an automation’s status for the API: just its state when all is well, with a reason and a message when not.

Classes

class flexmeasures.data.services.automation_status.AutomationStatus(status: str, reason: str | None = None, message: str | None = None)

Whether an automation’s runs can start, and if not, why not.

__init__(status: str, reason: str | None = None, message: str | None = None) → None