diff --git a/backend/app/jobs/service.py b/backend/app/jobs/service.py index a09527b3..21587ad7 100644 --- a/backend/app/jobs/service.py +++ b/backend/app/jobs/service.py @@ -145,14 +145,58 @@ class JobService: def cleanup_old(cls, max_age_seconds=86400): """Delete terminal jobs whose completion is older than the window.""" cutoff = datetime.utcnow() - timedelta(seconds=max_age_seconds) - deleted = (Job.query - .filter(Job.status.in_(Job.TERMINAL_STATUSES)) - .filter(Job.completed_at.isnot(None)) - .filter(Job.completed_at < cutoff) - .delete(synchronize_session=False)) + ids = [row[0] for row in ( + db.session.query(Job.id) + .filter(Job.status.in_(Job.TERMINAL_STATUSES)) + .filter(Job.completed_at.isnot(None)) + .filter(Job.completed_at < cutoff) + .all())] + deleted = cls._delete_with_logs(ids) db.session.commit() return deleted + @staticmethod + def _delete_with_logs(ids): + """Delete jobs and their run log lines together. A job's lines live in + ``run_log_entries`` keyed by its id; deleting only the job strands + them there forever, since nothing else prunes that table.""" + if not ids: + return 0 + from app.models.run_log import RunLogEntry + (RunLogEntry.query + .filter(RunLogEntry.run_kind == 'job', RunLogEntry.run_id.in_(ids)) + .delete(synchronize_session=False)) + return (Job.query + .filter(Job.id.in_(ids)) + .delete(synchronize_session=False)) + + @classmethod + def prune_orphan_logs(cls, older_than, batch_size=5000): + """Delete job log lines whose job no longer exists. Boxes that pruned + jobs before their lines were deleted with them carry these orphans; + only lines older than ``older_than`` are considered, so a line written + just before its job row commits is never mistaken for one.""" + from sqlalchemy import exists + from app.models.run_log import RunLogEntry + deleted = 0 + while True: + ids = [row[0] for row in ( + db.session.query(RunLogEntry.id) + .filter(RunLogEntry.run_kind == 'job') + .filter(RunLogEntry.created_at < older_than) + .filter(~exists().where(Job.id == RunLogEntry.run_id)) + .limit(batch_size) + .all())] + if not ids: + break + deleted += (RunLogEntry.query + .filter(RunLogEntry.id.in_(ids)) + .delete(synchronize_session=False)) + db.session.commit() + if len(ids) < batch_size: + break + return deleted + @classmethod def prune_terminal(cls, retention_days=14, batch_size=5000): """Prune accumulated terminal jobs so scheduler-tick rows don't grow @@ -181,12 +225,11 @@ class JobService: .all())] if not ids: break - deleted += (Job.query - .filter(Job.id.in_(ids)) - .delete(synchronize_session=False)) + deleted += cls._delete_with_logs(ids) db.session.commit() if len(ids) < batch_size: break + cls.prune_orphan_logs(now - timedelta(days=retention_days), batch_size) return deleted diff --git a/backend/tests/test_jobs.py b/backend/tests/test_jobs.py index 6bdcb1c5..db1cf6b1 100644 --- a/backend/tests/test_jobs.py +++ b/backend/tests/test_jobs.py @@ -322,6 +322,37 @@ class TestJobRetention: assert ids['old_cancel'] not in survivors assert ids['ancient_failed'] not in survivors + def test_prune_deletes_the_jobs_log_lines_and_old_orphans(self, app): + # A pruned job's run log lines used to stay behind forever: a 25 GB + # box carried 540k orphaned rows (~160 MB with the index). + from datetime import datetime, timedelta + from app.models.run_log import RunLogEntry + + now = datetime.utcnow() + old = Job(kind='t.ok', status=Job.STATUS_SUCCEEDED, + completed_at=now - timedelta(days=30), created_at=now - timedelta(days=30)) + fresh = Job(kind='t.ok', status=Job.STATUS_SUCCEEDED, + completed_at=now - timedelta(days=1), created_at=now - timedelta(days=1)) + db.session.add_all([old, fresh]) + db.session.commit() + old_id, fresh_id = old.id, fresh.id + + def _line(run_id, days, kind='job'): + db.session.add(RunLogEntry(run_kind=kind, run_id=run_id, message='x', + created_at=now - timedelta(days=days))) + _line(old_id, 30) + _line(fresh_id, 1) + _line('gone-long-ago', 40) # orphan from an earlier prune + _line('not-committed-yet', 0) # too new to call an orphan + _line('gone-long-ago', 40, kind='sandbox') # another kind's lines + db.session.commit() + + JobService.prune_terminal(retention_days=14) + + left = {(r.run_kind, r.run_id) for r in RunLogEntry.query.all()} + assert left == {('job', fresh_id), ('job', 'not-committed-yet'), + ('sandbox', 'gone-long-ago')} + def test_retention_handler_respects_zero_disable(self, app, monkeypatch): from app.jobs import builtin_handlers from app.services.settings_service import SettingsService