Created
March 22, 2018 12:36
-
-
Save jerry-git/b26f2eb85710aeb009a837497fdbd567 to your computer and use it in GitHub Desktop.
A simple example about how to use error callbacks in celery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
celery -A celery_error_callback_example.celery worker --loglevel=info | |
python celery_error_callback_example.py | |
""" | |
import time | |
from celery import Celery | |
celery = Celery( | |
'celery_error_callback_example', broker='amqp://guest@localhost//') | |
@celery.task | |
def do_something_important(important_arg): | |
print('doing important stuff with arg {}'.format(important_arg)) | |
if 'error' in important_arg: | |
raise ValueError('Oh boi, something went wrong') | |
@celery.task | |
def error_callback(request, exception, traceback): | |
print('ERROR:') | |
print('request: {}'.format(request)) | |
print('exception: {}'.format(exception)) | |
print('traceback: {}'.format(traceback)) | |
def main(): | |
do_something_important.s('good argument').on_error( | |
error_callback.s()).delay() | |
time.sleep(1) | |
do_something_important.s('error argument').on_error( | |
error_callback.s()).delay() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment