[Django]-Sending Django-RQ exceptions to Sentry

6👍

You will need to develop a custom worker and register the sentry handler with python-rq.

I’am assuming you’re using rq.Worker as the worker class (the default on django-rq).

import rq
from raven import Client
from raven.transport import HTTPTransport
from rq.contrib.sentry import register_sentry

class SentryAwareWorker(rq.Worker):

    def __init__(self, *args, **kwargs):
        super(SentryAwareWorker, self).__init__(*args, **kwargs)
        dsn = settings.RAVEN_CONFIG['dsn']
        client = Client(dsn, transport=HTTPTransport)
        register_sentry(client, self)

I present an option to get sentry dsn and to initialise the sentry client but you can do it however you want. The important part is register_sentry(client, self).

Then run your worker with:

python manage.py rqworker queue_1 queue_2 queue_n --worker-class path.to.SentryAwareWorker

Leave a comment