Opt-in retention for finished jobs in the SQL queues (`queue.retention`)
Feature Request
Is your feature request related to a problem? Please describe.
The Postgres and SQLite queues never delete finished jobs. Every completed, cancelled and failed job stays in pg_loco_queue / sqlt_loco_queue forever, so the table keeps growing, and every dequeue and status update gets slower. (#1851 adds indexes, which fixes the speed but not the growth.)
In my app, one nightly import adds ~18k jobs. After about six weeks, pg_loco_queue held ~196k rows, and almost all of them were completed.
Today the way to clean up is cargo loco jobs purge, run by hand or from the scheduler. Two things make that a poor default:
- It needs the scheduler (or cron) to be running, separate from the workers.
- Without
--status, it also deletes queued jobs older than--max-age(90 days by default). So a plain purge can delete jobs that haven't run yet. That may be on purpose, to clear out jobs that will never run, but it surprised me.
Describe the solution you'd like
An optional retention block under queue:, set up the same way as reaper:
queue:
kind: Postgres
uri: ...
reaper:
age_minutes: 10
# Optional: delete finished jobs after this many days. Each status is optional,
# and a status you leave out is kept forever. Queued and processing jobs are
# never deleted.
retention:
completed_days: 7
cancelled_days: 7
failed_days: 30
interval_seconds: 3600 # default- Off unless set, like
reaper, so upgrading never deletes anyone's jobs. The config thatloco newgenerates would turn it on, so new apps stay small from the start. - Runs like the reaper: a background task started by the queue's
run, which stops when the worker stops. It would reuse the delete logic behindjobs purge(clear_jobs_older_than). - Measures age from
updated_at, which is when the job finished, so "keep completed jobs 7 days" means 7 days after the job completed.jobs purgemeasures fromcreated_at, so a job scheduled far ahead, or one that ran for days, can be deleted soon after it finishes. This needs either a new version ofclear_jobs_older_thanor a parameter that picks the column. - Postgres and SQLite, the same drivers that have
reaper. Redis doesn't have this problem in the same way. - Queued and processing jobs have no setting, so retention can never delete a job that hasn't finished.
Describe alternatives you've considered
- Document the scheduler +
jobs purgeinstead of building this in. No new config, but it depends on the scheduler running, and on people remembering--statusso queued jobs aren't deleted. - One age plus a list of statuses (
max_age_days+statuses), likejobs purge. Simpler, but then failed jobs can't be kept longer than completed ones. That matters because failed jobs are the ones you look at and retry withjobs retry. - On by default. Every app would stay small automatically, but upgrading would quietly start deleting jobs people may still need.
If this approach works for you, I'm happy to open the PR.
Source: loco-rs/loco