Keep successful builtin ticks for a day

The panel's scheduled checks run ~12k times a day on one box, and
each success kept a job, its queue message, log lines and a system
event for 14-30 days: ~500 MB recording "checked, nothing to do".
Successful ticks now go after 24 h with their queue message and emit
no event. Failures keep full retention.
This commit is contained in:
Juan Denis committed 2026-09-27 16:18:48 -04:00
1 parent f122acce01
commit a25aa0e4ef
4 files changed
+79 -2

No files matched your search

+4 -1
View File
@@ -125,7 +125,10 @@ class JobConsumer:
QueueBusService.complete(GROUP_SLUG, QUEUE_SLUG, message['id'])
stream.log('info', f'Job succeeded: {job.kind}')
stream.close(Job.STATUS_SUCCEEDED)
self._emit(job, 'job.succeeded')
# A successful builtin tick is already on the Jobs page; a system
# event per tick only doubled the rows. Failures still emit.
if not job.is_builtin_tick:
self._emit(job, 'job.succeeded')
except Exception as e:
logger.error(f'Job {job_id} ({job.kind}) failed: {e}')
try:
+11
View File
@@ -32,6 +32,17 @@ class Job(db.Model):
TERMINAL_STATUSES = (STATUS_SUCCEEDED, STATUS_FAILED, STATUS_CANCELLED)
# The panel's own scheduled checks (monitor, backup scheduler, sync…) fire
# every minute or so, ~12k runs a day on one box. A successful one records
# "checked"; it is kept a day, not the full job retention.
BUILTIN_KIND_PREFIX = 'builtin.'
TICK_RETENTION_HOURS = 24
@property
def is_builtin_tick(self):
return (self.scheduled_job_id is not None
and (self.kind or '').startswith(self.BUILTIN_KIND_PREFIX))
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
kind = db.Column(db.String(80), nullable=False, index=True)
status = db.Column(db.String(20), default=STATUS_PENDING, nullable=False, index=True)
+20 -1
View File
@@ -163,9 +163,21 @@ class JobService:
if not ids:
return 0
from app.models.run_log import RunLogEntry
from app.queue_bus.models import QueueMessage
(RunLogEntry.query
.filter(RunLogEntry.run_kind == 'job', RunLogEntry.run_id.in_(ids))
.delete(synchronize_session=False))
# The job's completed queue message goes with it; left behind it would
# wait for the (longer) telemetry window.
message_ids = [row[0] for row in (
db.session.query(Job.queue_message_id)
.filter(Job.id.in_(ids), Job.queue_message_id.isnot(None))
.all())]
if message_ids:
(QueueMessage.query
.filter(QueueMessage.id.in_(message_ids),
QueueMessage.status == QueueMessage.STATUS_COMPLETED)
.delete(synchronize_session=False))
return (Job.query
.filter(Job.id.in_(ids))
.delete(synchronize_session=False))
@@ -214,13 +226,20 @@ class JobService:
((Job.STATUS_FAILED,),
now - timedelta(days=retention_days * 3)),
]
# Successful builtin ticks go after a day (see Job.is_builtin_tick).
tick = ((Job.STATUS_SUCCEEDED,),
now - timedelta(hours=Job.TICK_RETENTION_HOURS),
(Job.scheduled_job_id.isnot(None),
Job.kind.startswith(Job.BUILTIN_KIND_PREFIX)))
specs = [tick] + [(statuses, cutoff, ()) for statuses, cutoff in specs]
deleted = 0
for statuses, cutoff in specs:
for statuses, cutoff, extra in specs:
while True:
ids = [row[0] for row in (
db.session.query(Job.id)
.filter(Job.status.in_(statuses))
.filter(age < cutoff)
.filter(*extra)
.limit(batch_size)
.all())]
if not ids:
+44
View File
@@ -353,6 +353,50 @@ class TestJobRetention:
assert left == {('job', fresh_id), ('job', 'not-committed-yet'),
('sandbox', 'gone-long-ago')}
def test_successful_builtin_ticks_are_kept_a_day(self, app):
# ~12k scheduler ticks a day each wrote a job, a queue message, log
# lines and an event, kept 14-30 days: ~500 MB on one box.
from datetime import datetime, timedelta
now = datetime.utcnow()
def _job(kind, status, hours, scheduled=True):
j = Job(kind=kind, status=status, scheduled_job_id=1 if scheduled else None,
completed_at=now - timedelta(hours=hours),
created_at=now - timedelta(hours=hours))
db.session.add(j)
return j
old_tick = _job('builtin.monitor_check', Job.STATUS_SUCCEEDED, 30)
fresh_tick = _job('builtin.monitor_check', Job.STATUS_SUCCEEDED, 2)
failed_tick = _job('builtin.monitor_check', Job.STATUS_FAILED, 30)
policy_run = _job('backup.policy.run', Job.STATUS_SUCCEEDED, 30) # real work
manual_builtin = _job('builtin.health_check', Job.STATUS_SUCCEEDED, 30, scheduled=False)
db.session.commit()
ids = {n: j.id for n, j in {'old_tick': old_tick, 'fresh_tick': fresh_tick,
'failed_tick': failed_tick, 'policy_run': policy_run,
'manual_builtin': manual_builtin}.items()}
JobService.prune_terminal(retention_days=14)
survivors = {j.id for j in Job.query.all()}
assert ids['old_tick'] not in survivors
assert {ids['fresh_tick'], ids['failed_tick'], ids['policy_run'],
ids['manual_builtin']} <= survivors
def test_a_successful_tick_writes_no_system_event(self, app, monkeypatch):
emitted = []
monkeypatch.setattr(JobConsumer, '_emit',
staticmethod(lambda job, event_type: emitted.append((job.kind, event_type))))
registry.register('builtin.t_tick', lambda job: {'ok': True})
registry.register('t.work', lambda job: {'ok': True})
for kind, scheduled in (('builtin.t_tick', 1), ('t.work', None)):
job = JobService.enqueue(kind)
job.scheduled_job_id = scheduled
db.session.commit()
_drain_once()
assert emitted == [('t.work', 'job.succeeded')]
def test_retention_handler_respects_zero_disable(self, app, monkeypatch):
from app.jobs import builtin_handlers
from app.services.settings_service import SettingsService