Python cron jobs with Sentry's @monitor, locally
Set the Sentry DSN in your Python job to http://sentry@127.0.0.1:8000/1 and keep the @monitor decorator from sentry_sdk.crons. Each run then shows up in Buggregator under Sentry → Crons: started, finished, failed. Give the monitor a schedule and Buggregator also marks runs that never started and runs that never finished.
Steps
1. Start Buggregator with a database file
The default database lives in memory, so jobs and their runs are gone after a restart. Keep them in a file.
docker run --pull always \
-p 127.0.0.1:8000:8000 \
-e DATABASE_DSN=/data/buggregator.db \
-v buggregator-data:/data \
ghcr.io/buggregator/server:latest2. Point the SDK at Buggregator
Any key before @ works. The number at the end puts the data into the default project.
import sentry_sdk
sentry_sdk.init(dsn="http://sentry@127.0.0.1:8000/1")In Docker Compose, use the service name: http://sentry@buggregator:8000/1.
3. Keep your decorator, add a schedule
The schedule lets Buggregator tell when a run is due. checkin_margin and max_runtime are minutes.
from sentry_sdk.crons import monitor
@monitor(monitor_slug='nightly-report', monitor_config={
'schedule': {'type': 'crontab', 'value': '0 3 * * *'},
'timezone': 'Europe/Berlin',
'checkin_margin': 5,
'max_runtime': 30,
})
def nightly_report():
...The first run creates the job in Buggregator. The settings the SDK sends update it on every run. monitor_config needs sentry-sdk 1.45.0 or newer.
4. Open Sentry, then Crons
Each job shows its status, schedule, last and next run, and a strip of the last 20 runs. Jobs that failed, hung or missed a run come first, under Needs attention. Open a job for its run history with start time and duration.
Why it works
The decorator sends a check-in when the function starts and another when it ends: ok, or error if it raised. Buggregator stores them as runs. Every 30 seconds it checks the schedule: a run that was due and never came is missed, and a run that started but did not end within max_runtime is timeout. Nobody is notified; you see it on the page.
Docs: docs.buggregator.dev/config/crons.html#sentry-crons
Requires Buggregator v2.13.0+
Related tips
- Crons
Know when a cron script did not run: one curl line
Add a curl to your crontab line. Buggregator shows each run, marks missed and hung runs, and keeps the last 10 KB of the job's output.
- Crons
Know when a Laravel scheduled task did not run
Add ->sentryMonitor() to a scheduled task and point the Sentry DSN at Buggregator. Each run shows up, and missed or hung runs are marked.
- Sentry
Django errors, traces and logs on your machine
Point sentry-sdk at Buggregator with traces and logs on. Django errors, request traces and log records show up locally, with no Sentry account.
Checked on with Buggregator v2.13.0