flexmeasures.data.services.automation_pruning
Delete the records of automation runs that are older than their time-to-live, so that the run tables stop growing.
Deleting a run deletes its attempts and its jobs, too (their foreign keys cascade).
Functions
- flexmeasures.data.services.automation_pruning.prunable_automation_runs_criterion(ttl_setting: dict[str, timedelta | None] | timedelta | None, stuck_after: timedelta, now: datetime)
Return the criterion for a run whose record may be deleted.
That is a run scheduled longer ago than the time-to-live of its automation type, except:
a run that a runner holds a live claim on, as it is being dispatched;
the latest run of its automation, so that every automation that ever ran still shows how it last went;
a failure of a monitored automation that flexmeasures monitor automations did not report yet.
A run that never finished, because the worker running its job died for instance, is deleted once it expired, too: by then nothing will finish it any more.
- flexmeasures.data.services.automation_pruning.prune_automation_runs(now: datetime | None = None, ttl_setting: dict[str, ~datetime.timedelta | None] | ~datetime.timedelta | None | object=<object object>, stuck_after: timedelta | None = None, batch_size: int = 1000, time_budget: timedelta = datetime.timedelta(seconds=30)) int
Delete the records of the runs that outlived their time-to-live, and return how many were deleted.
Runs are deleted in batches, each in a transaction of its own, so that no table is locked for long. Once the time budget is spent, no new batch starts, and whatever is left waits for the next pruning.
- Parameters:
now – The moment to judge by, which defaults to the server’s current time.
ttl_setting – FLEXMEASURES_AUTOMATION_DBLOGS_TTL (read from the app config if not given), where None keeps all runs.
stuck_after – FLEXMEASURES_MONITOR_AUTOMATIONS_STUCK_AFTER (read from the app config if not given).
batch_size – How many runs to delete per transaction.
time_budget – How long to keep starting new batches.
- flexmeasures.data.services.automation_pruning.prune_automation_runs_if_due(now: datetime | None = None) int | None
Prune the automation runs, unless they were pruned less than an hour ago.
Redis keeps track of the last pruning, so that runners on several machines prune once an hour between them. Return how many runs were deleted, or None if pruning was not due.