
"Sentry is a great tool for monitoring celery tasks, and alerting when they fail or don't run on time. But it requires a bit of work to setup properly. Below is some sample code for setting up sentry monitoring of periodic tasks, followed by an explanation. import math import sentry_sdk from celery import signals from sentry_sdk import monitor from sentry_sdk.integrations.celery import CeleryIntegration @signals.beat_init.connect # if you use beats @signals.celeryd_init.connect def init_sentry(**kwargs): sentry_sdk.init( dsn=..., integrations=[ CeleryIntegration(monitor_beat_tasks=False) ] ) @signals.worker_shutdown.connect @signals.task_postrun.connect def flush_sentry(**kwargs): sentry_sdk.flush(timeout=5) def add_periodic_task(celery, schedule, task): max_runtime = math.ceil(schedule * 4 / 60) monitor_config = { "recovery_threshold": 1, "failure_issue_threshold": 10, "checkin_margin": max_runtime, "max_runtime": max_runtime, "schedule": { "type": "interval", "value": math.ceil(schedule / 60.0) "unit": "minute" } } name = task.__name__ task = monitor(monitor_slug=name, monitor_config=monitor_config)(task) celery.add_periodic_task(schedule, celery.task(task).s(), name=name)"
"The init_sentry function must be called before any tasks start executing. The sentry docs for celery recommend using the celeryd_init signal. And if you use celery beats for periodic task execution, then you also need to initialize on the beat_init signal. Monitoring Beats Tasks In this example, I'm setting monitor_beat_tasks=False to show how you can do manual monitoring. monitor_beat_tasks=True is much simpler, and doesn't require any code like in add_periodic_task. But in my experience, it's not reliable when using async celery functions. The automatic beats monitoring uses some celery signals that likely don't get executed correctly under async conditions. But manual monitorin"
Initialize Sentry before any Celery tasks run by connecting to celeryd_init and, if using beats, beat_init. Use CeleryIntegration and consider monitor_beat_tasks=False when async task execution can prevent automatic beat signals from firing. Flush Sentry on worker shutdown and after task postrun to ensure events are delivered. For periodic tasks, compute a max_runtime and create a monitor_config including recovery_threshold, failure_issue_threshold, checkin_margin, max_runtime, and schedule details. Wrap periodic task callables with sentry_sdk.monitor (monitor_slug and monitor_config) and register them with celery.add_periodic_task for manual monitoring.
Read at StreamHacker
Unable to calculate read time
Collection
[
|
...
]