Skip to content

feat: delete automation run records older than their time-to-live - #2908

Merged
nhoening merged 6 commits into
mainfrom
feat/2872-prune-automation-runs
Dec 3, 2026
Merged

nhoening merged 6 commits into
mainfrom
feat/2872-prune-automation-runs

Conversation

@BelhsanHmida

@BelhsanHmida BelhsanHmida commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

The automation_run, automation_run_attempt and automation_run_job tables grow with every run, most of all for ingestion automations that run every minute.

This PR deletes old runs once an hour, as part of flexmeasures jobs run-automations. For example, with the defaults, an ingestion run scheduled 15 days ago is deleted with its attempts and jobs, while the automation's latest run is kept however old it is.

  • New setting FLEXMEASURES_AUTOMATION_DBLOGS_TTL, per automation type, with a "default" for plugin types, like FLEXMEASURES_AUTOMATION_MIN_INTERVAL.
    None keeps runs forever, for one type or, as the whole setting, for all of them.
  • Runs older than their TTL are kept when they are the automation's latest run, when a runner holds a live claim on them,
    or when they are a failed or stuck run of a monitored automation that flexmeasures monitor automations has not reported yet (the monitor's criterion, now shared).
  • A run that never finished is deleted once it expires, since nothing will finish it by then.
  • Pruning runs after dispatching, so it never delays a run. A Redis key makes several runners prune once an hour between them.
    It deletes in batches of 1,000 and starts no new batch after 30 seconds; whatever is left waits for the next hour. A failed pruning prints a warning and does not fail the command.
  • Docs: the setting, and a note in the automation runs section.
  • Added changelog item in documentation/changelog.rst

Decisions worth a look:

  • Pruning is on by default with the values from the issue, so the first run after upgrading deletes older history. Making it opt-in is a one-line change.
  • Keyed by automation type rather than queue: a run records its type, and the type names are the queue names from the issue.

Side effects:

  • The run counts the automation endpoints return (total, and the counts per state) now cover only the runs kept.
  • Postgres reuses the space of deleted rows but does not hand it back to the disk, so on a server with a large backlog the tables stop growing rather than shrink. A VACUUM FULL reclaims the space.

Look & Feel

FLEXMEASURES_AUTOMATION_DBLOGS_TTL = {
    "default": timedelta(days=60),
    "scheduling": timedelta(days=90),
    "forecasting": timedelta(days=60),
    "reporting": timedelta(days=60),
    "ingestion": timedelta(days=14),
}
$ flexmeasures jobs run-automations
Automation 1 ('Battery schedule') run 9 queued 1 scheduling job(s) for asset 6, scheduled for 2026-11-24 18:16:00+00:00.
Automation 2 ('Solar forecast') run 10 queued 3 forecasting job(s) for asset 7, scheduled for 2026-11-24 18:16:00+00:00.
Deleted the records of 4 automation run(s) older than their time-to-live.

How to test

pytest flexmeasures/data/tests/test_automation_pruning_fresh_db.py

Or by hand: let a few automations run, age their runs with UPDATE automation_run SET scheduled_at = scheduled_at - interval '100 days';, delete the Redis key flexmeasures:automation-run-pruning, and run flexmeasures jobs run-automations.
Old successful runs are deleted, unreported failures and each automation's latest run stay. After flexmeasures monitor automations, the next pruning deletes the reported failures too.

Further Improvements

  • A setting for the number of runs to keep per automation, as the issue suggests for later.

Related Items

Closes #2872


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

Pruning old runs needs the same rule, so that it never deletes a failure before the monitor reported it.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
FLEXMEASURES_AUTOMATION_DBLOGS_TTL sets a time-to-live per automation type.
Once an hour, flexmeasures jobs run-automations deletes older runs, with their attempts and jobs,
keeping each automation's latest run, runs under a live claim and failures the monitor did not report yet.

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 Nov 24, 2026 •

Copy link
Copy Markdown

BelhsanHmida and others added 2 commits November 30, 2026 10:21
Keep pruning on every run-automations tick, also when nothing is due, next to main's stale-skip and run reports.
Follow the schedule_revision to revision rename in the pruning tests.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…sed v1.1.3 (#2908)

Signed-off-by: Vibe Nuage Agent <vibe@mistral.ai>

@nhoening nhoening left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nhoening nhoening added this to the 1.2.0 milestone Dec 3, 2026
@nhoening
nhoening merged commit 1e620a1 into main Dec 3, 2026
11 checks passed
@nhoening
nhoening deleted the feat/2872-prune-automation-runs branch December 3, 2026 15:34
Flix6x added a commit that referenced this pull request Dec 5, 2026
Resolve the conflict with #2908 in automation_monitoring.py: main moved the criterion for an unreported failure into
unalerted_failure_criterion, which pruning uses too, to keep failures until the monitor reported them. The exclusion
of direct runs moves into that criterion, so a failed or stuck direct run, which is never reported, is pruned like any other run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Limit amount of logs of automation runs in the db

3 participants