Skip to content

feat: say whether an automation's runs can start, in one status field - #2943

Open
BelhsanHmida wants to merge 9 commits into
mainfrom
feat/2911-automation-status
Open

BelhsanHmida wants to merge 9 commits into
mainfrom
feat/2911-automation-status

Conversation

@BelhsanHmida

@BelhsanHmida BelhsanHmida commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

An automation can look fine while every run of it is refused, for instance when the user it runs as was deactivated, or a sensor it reads from was deleted, or while its runs start but every job fails, for instance when its asset lost its price.

This PR gives each automation a status that says whether its runs can start, and whether they work. For example, deactivate a colleague through the API, and every automation that runs as them now lists as:

"status": {"state": "runs-refused", "reason": "run-user-inactive", "message": "Automation 6 runs as former-colleague (user 12), who is missing or inactive.", "since": "2026-12-01T09:00:00+00:00"}
  • Automations get a stored status: ok, runs-refused or runs-failing (with a reason and a message), or unknown for existing automations until their next run or change. A new migration adds it.
  • The reasons are run-user-inactive, run-user-lacks-access, plugin-unavailable and definition-invalid (say, a deleted sensor), and new ones can be added later.
  • Runs that start, but whose jobs failed in the latest run that ended, make it runs-failing with the reason jobs-failed and the error, e.g. "Its latest run failed on ValueError: Missing consumption price for commodity 'electricity'." The next run that works, or an edit of the definition or recurrence, sets it back to ok. Refused runs come first.
  • Refusing a run on demand or an edit because the run user is missing or inactive now carries reason: run-user-inactive (was run-user-lacks-access, added in Feat: edit automation config #2779, unreleased), matching the status.
  • It is stored rather than worked out on every read, as discussed in the issue, so the list endpoint stays as cheap as it is.
  • It is updated after each run: a run that queued its jobs sets ok (unless it was planned before the latest edit, or runs are failing), a run whose jobs ended sets ok or runs-failing, and a failed dispatch works the status out anew, so a Redis outage does not count as a refusal.
  • It is also updated as soon as something it depends on changes, so refused runs show before the next run fails:
    creating, editing or copying an automation, deactivating a user or changing their roles (API), deleting a user or organisation (CLI), and deleting a sensor (API and CLI).
  • Automations set up through the CLI, which run as nobody, now also have their sensors and data generator checked, so a deleted sensor or a missing plugin shows for them too.
  • The API gives a status with each automation, flexmeasures show automations gets a Status column (with the message in the details), and the Automations page marks such an automation runs refused or runs failing, with the reason on hover.
  • Docs: a section on whether an automation can run.
  • Added changelog item in documentation/changelog.rst

Look & Feel

GET /api/v3_0/assets/(id)/automations:

{"id": 6, "name": "Battery forecasts", "status": {"state": "ok", "since": "2026-12-01T08:00:00+00:00"}, ...}
{"id": 7, "name": "Day-ahead report", "status": {"state": "runs-refused", "reason": "definition-invalid", "message": "Could not determine the sensors of automation 7: {'sensor': ['No sensor found with ID 12.']}", "since": "2026-12-01T09:00:00+00:00"}, ...}

flexmeasures show automations:

  ID  Asset          Name               ...  Runs as                    Status
----  -------------  -----------------  ...  -------------------------  ----------------------------------
   6  Battery (ID 2) Battery forecasts  ...  toy-user (user 2)          ok
   8  Battery (ID 2) Imbalance prices   ...  former-colleague (user 12) runs-refused (run-user-inactive)

How to test

pytest flexmeasures/data/tests/test_automation_status_fresh_db.py flexmeasures/api/v3_0/tests/test_automation_status_api_fresh_db.py
flask db upgrade && flask db downgrade d5b8f3a1c962 && flask db upgrade

Or by hand: deactivate the user an automation runs as (on their user page), then open the asset's Automations page. The automation is marked runs refused, and hovering the badge says why. Reactivate the user, and the badge is gone. Or point a schedule's asset at a price sensor, delete that sensor, and run the schedule: it is marked runs failing, saying the price is missing.

Further Improvements

  • Changes to an organisation's access (account roles, consultancy) and uninstalling a plugin only show after the automation's next run.
  • The digest of flexmeasures monitor automations could report automations whose runs are refused.
  • Deleting a sensor through the API now works out the sensors of every automation to find the ones involving it, which is fine for something this rare, but could use an index later.

Related Items

Closes #2911
Follow-up to #2779 and discussion #2780


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

Add a status to automations (ok, runs-refused with a reason and a message, or unknown until first checked),
stored rather than worked out on every read, so that listing automations stays cheap.
It is set when an automation is created, edited or copied, and after each run's dispatch:
a run that queued its jobs marks it ok (unless it was planned before the latest edit), and a failed dispatch works it out anew.
Refusals now carry a reason, and a missing plugin raises its own NotImplementedError subclass.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…lve changes

Deactivating a user, changing their roles or deleting them (API and CLI) refreshes the automations that run as them,
and deleting a sensor (API and CLI) refreshes the automations involving it,
so that refused runs show before the next run fails, not after.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
The API gives a status with each automation, `flexmeasures show automations` gets a Status column (and the message in its details),
and the Automations page marks an automation whose runs are refused, with the reason on hover.

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>
@read-the-docs-community

read-the-docs-community Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Refusing a run on demand, or an edit, because the user the automation runs as is missing or inactive now says run-user-inactive,
rather than run-user-lacks-access, which stays for a run user whose access falls short.
The Automations page offers to take over for both.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
The status said whether runs can start, so a schedule whose asset lost its price looked fine while every job failed.
Runs that start, but whose jobs failed in the latest run of the current revision that ended, now make it runs-failing,
with the reason jobs-failed and the error they failed on; the next run that works, or an edit starting a new revision, sets it back to ok.
Refused runs still come first, and the Automations page marks failing runs too.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Say whether an automation can run, in one extendable status field

1 participant