Issue #3586550: Add automatic cleanup / retention for monitoring_sensor_result records
Closes #3586550.
Adds an optional retention policy for the monitoring_sensor_result entity so the table no longer grows unbounded when sensor_call_logging is set to all (or to on_request on a site that changes status a lot).
Changes
- Setting:
monitoring.settings:result_retention_days(integer, default0= keep forever). Exposed on the monitoring settings form as a number field alongside the existing logging toggles. - Cron prune:
monitoring_cron()deletes anymonitoring_sensor_resultwithtimestamp < request_time - retention_days * 86400. Done via the entity storage (not raw SQL), in 500-entity chunks, only whenretention_days > 0. - Index: new storage-schema handler
SensorResultEntityStorageSchemaadds an index ontimestamp, so the prune (and the existing "recent results" queries) don't scan the full table. - Upgrade path:
monitoring_update_8104()seeds the default value and applies the timestamp index to existing installs viaEntityDefinitionUpdateManager::updateEntityType(). - Test:
MonitoringRetentionTestcovers both0 = keep allandretention > 0paths. The boundary case (row exactly atnow - N days) is kept because the cutoff condition is strict<.
Backwards compatibility
No behavioural change when left at the default 0. Existing sites continue to accumulate logs as before until an admin opts in.
Manual test
- Enable monitoring, set Monitoring event logging to Log all events, enable Run sensors during cron.
- Let cron run until
SELECT COUNT(*) FROM monitoring_sensor_resulthas a few hundred rows. - On the settings form, set Result retention (days) to
1and save. - Backdate a row:
UPDATE monitoring_sensor_result SET timestamp = UNIX_TIMESTAMP() - 2*86400 WHERE record_id = 1. - Run cron (
drush cron). Row 1 is gone, recent rows are kept.
Edited by Frank Mably