def create_celery_app(app=None, config="worker"): """ adapted from http://flask.pocoo.org/docs/0.10/patterns/celery/ """ app = app or create_app(config=config) celery.main = app.import_name celery.conf["BROKER_URL"] = app.config["CELERY_BROKER_URL"] celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask if not app.config["TESTING"]: connect_failure_handler() bugsnag.configure(ignore_classes=[ "webhookdb.exceptions.StaleData", "webhookdb.exceptions.NothingToDo", "webhookdb.exceptions.RateLimited", ]) return celery
def celery_failure_handler(): """ The bugsnag celery integration works by listening to the celery task_failure signal and sending an event when the signal is received. This context manages the signal connection and ensures that error handling does not occur across separate tests. """ connect_failure_handler() try: yield finally: task_failure.disconnect(failure_handler)
def configure_bugsnag(): """ Hooks up Bugsnag to Celery. You should do this early in the startup process to make sure we are setup correctly from the beginning. """ if not API_KEY: _logger.warning( "Bugsnag API key is not set, cannot configure exception tracking") return bugsnag.configure( api_key=API_KEY, project_root=os.path.abspath( os.path.join(os.path.dirname(__file__), '..')), app_version=BUILD_ID, release_stage=ENVIRONMENT, ) # register a handler to capture celery errors connect_failure_handler()
Change the API key below to your API key, Then, in one terminal, run: celery -A example.tasks And, in another, run: python -m example.tasks You will see a zero-division error appear in Bugsnag. """ from celery import Celery import bugsnag from bugsnag.celery import connect_failure_handler celery = Celery('tasks', broker='redis://localhost', backend='redis') bugsnag.configure(api_key="066f5ad3590596f9aa8d601ea89af845") connect_failure_handler() @celery.task(name='tasks.divide') def divide(x, y): return x / y if __name__ == "__main__": divide.delay(1, 0)
import bugsnag from bugsnag.celery import connect_failure_handler from celery import Celery bugsnag.configure(api_key="066f5ad3590596f9aa8d601ea89af845") connect_failure_handler() app = Celery('tasks', broker='redis://localhost:6379/0') @app.task def crash(x, y): return x + y @app.task def other_crash(): raise Exception('spam', 'eggs')