Set Up Crons

Sentry Crons allows you to monitor the uptime and performance of any scheduled, recurring job. Once implemented, it'll allow you to get alerts and metrics to help you solve errors, detect timeouts, and prevent disruptions to your service.

Requirements

Job Monitoring

Use the Celery Beat integration to monitor and notify you if your job is missed (or doesn't start when expected), if it fails due to a problem in the runtime (such as an error), or if it fails by exceeding its maximum runtime.

First, set up your Celery beat configuration:

Copied
from celery import Celery, signals
from celery.schedules import crontab

import sentry_sdk
from sentry_sdk.crons import monitor
from sentry_sdk.integrations.celery import CeleryIntegration

app = Celery('mytasks', broker='redis://localhost:6379/0')
app.conf.beat_schedule = {
    'set-in-beat-schedule': {
        'task': 'tasks.tell_the_world',
        'schedule': crontab(hour='10', minute='15'),
        'args': ("in beat_schedule set", ),
    },
}

Initialize Sentry either in celeryd_init or beat_init signal:

Copied
#@signals.celeryd_init.connect
@signals.beat_init.connect
def init_sentry(**kwargs):
    sentry_sdk.init(
        dsn='https://examplePublicKey@o0.ingest.sentry.io/0',
        integrations=[CeleryIntegration()],
        environment="local.dev.grace",
        release="v1.0.7-a1",
    )

Finally, link your Celery task to a Sentry Cron Monitor:

Copied
@app.task
@monitor(monitor_slug='<monitor-slug>')
def tell_the_world(msg):
    print(msg)

Connecting Errors to Cron Monitors

To link any exceptions captured during your job's lifecycle, use Sentry's context with your monitor slug.

Copied
sentry_sdk.set_context("monitor", {
    "slug": "<monitor_slug>",
})

Alerts

When your recurring job fails to check in (missed), runs beyond its configured maximum runtime (failed), or manually reports a failure, Sentry will create an error event with a tag to your monitor.

To receive alerts about these events:

  1. Navigate to Alerts in the sidebar.
  2. Create a new alert and select "Issues" under "Errors" as the alert type.
  3. Configure your alert and define a filter match to use: The event's tags match {key} {match} {value}.

Example: The event's tags match monitor.slug equals my-monitor-slug-here

Cron completed alert filter

Learn more in Issue Alert Configuration.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").