A recurring job is not the same as a long-running worker. The worker knows how to
execute work; the timer decides when a new unit of work should exist.
For a daily report, a nightly cleanup, or a weekly digest, the pattern is:
systemd timer `-- run a small enqueue script `-- insert one job per tenant/period `-- long-running workers execute The period is part of the job id, so running the enqueue script twice does not
produce two reports.
Enqueue script
# /opt/nonodb/jobs/enqueue_reports.py from datetime import datetime, timezone from nonodb import NonoDB from nonodb_worker import JobQueue db = NonoDB() jobs = JobQueue(db.collection("_jobs")) period = datetime.now(timezone.utc).strftime("%Y-%m-%d") for tenant in active_tenants(): jobs.enqueue( "daily_report", {"tenant": tenant, "period": period}, job_id=f"daily_report:{tenant}:{period}", max_attempts=3, ) If the timer fires twice, or the script is retried, the deterministic job_id
turns the second insert into a no-op. If a report fails, the worker retries it
with backoff; the enqueue script does not need to know about retries.
The systemd timer
# /etc/systemd/system/nonodb-reports.service [Unit] Description=Enqueue NonoDB daily reports [Service] Type=oneshot User=nonodb Group=nonodb Environment=NONODB_SOCK=/run/nonodb/nonodb.sock Environment=PYTHONPATH=/opt/nonodb/client:/opt/nonodb/workers/python WorkingDirectory=/opt/nonodb ExecStart=/usr/bin/python3 /opt/nonodb/jobs/enqueue_reports.py # /etc/systemd/system/nonodb-reports.timer [Unit] Description=Run the NonoDB report scheduler daily [Timer] OnCalendar=*-*-* 06:00:00 Persistent=true [Install] WantedBy=timers.target Enable it:
systemctl daemon-reload systemctl enable --now nonodb-reports.timer systemctl list-timers nonodb-reports.timer Persistent=true runs a missed timer after downtime. That is useful for a daily
report: if the machine was off at 06:00, the scheduler still enqueues the report
when it starts.
The report itself runs in the background worker, not in the timer service. The
timer should finish quickly after enqueueing.
The worker side
The worker does not need to know that the job is recurring:
from nonodb_worker import Job def daily_report(job: Job): tenant = job.payload["tenant"] period = job.payload["period"] report = build_report(tenant, period) upload_report(tenant, period, report) return {"tenant": tenant, "period": period} def register(runner): runner.register("daily_report", daily_report) Run the workers as a long-running service, as described in the offload article.
Multiple workers can process different tenants in parallel; each job is claimed
by one worker.
The --once alternative
For a small deployment with one scheduled task, a timer can start a worker with
--once instead of an enqueue script:
[Service] Type=oneshot ExecStart=/usr/bin/python3 -m nonodb_worker \ --module jobs_handlers \ --collection _jobs \ --once This claims and runs at most one due job, then exits. It is simple, but it is not
a general scheduler: if ten reports are due, the timer runs one and exits. Use a
long-running worker if the timer can enqueue more than one job.
Missed runs and duplicates
A recurring job has two independent failure modes:
- The timer did not run.
Persistent=truehandles downtime. If the timer was disabled, decide whether to backfill missed periods or skip them. - The timer ran twice. The period key in
job_idmakes enqueue idempotent.
Do not rely on the timer itself for exactly-once execution. The queue is
at-least-once, and the report handler should be idempotent: overwrite the report
for tenant + period rather than appending a second copy.
Checklist
- [ ] The timer runs an enqueue script, not the long report itself.
- [ ] The job id contains the tenant and the period.
- [ ]
Persistent=trueis set if missed runs should be caught up. - [ ] The report handler is idempotent for the same period.
- [ ] Long-running workers execute the queued jobs.
- [ ]
max_attemptsreflects how important the report is. - [ ] Timer and worker logs are monitored separately.
