ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
Keep successful builtin ticks for a day
Juan Denisa25aa0e4ef1b
diff --git a/backend/app/jobs/consumer.py b/backend/app/jobs/consumer.pyindex bcccc5e9..0f022b3f 100644--- a/backend/app/jobs/consumer.py+++ b/backend/app/jobs/consumer.py@@ -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:diff --git a/backend/app/jobs/models.py b/backend/app/jobs/models.pyindex 9a7ac0c7..62e7c668 100644--- a/backend/app/jobs/models.py+++ b/backend/app/jobs/models.py@@ -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)diff --git a/backend/app/jobs/service.py b/backend/app/jobs/service.pyindex 21587ad7..3f3c66d3 100644--- a/backend/app/jobs/service.py+++ b/backend/app/jobs/service.py@@ -163,9 +163,21 @@ class JobService:if not ids:return 0from 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:diff --git a/backend/tests/test_jobs.py b/backend/tests/test_jobs.pyindex db1cf6b1..3e4d2359 100644--- a/backend/tests/test_jobs.py+++ b/backend/tests/test_jobs.py@@ -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_handlersfrom app.services.settings_service import SettingsService