1

I want to know if there is a way to monitor whether or not a task completes or fails as soon as it does using python celery. I have an event I want to fire up based on the results of a certain task.

KudzieChase
  • 261
  • 3
  • 16

1 Answers1

1

You can run your task as a celery @shared_task with a try except block inside:

@shared_task
def my_task(input1, input2, ...):
    Setting up...
    try:
        Do stuff
        fire_success_event() <- Your success event
    except Exception:
        The above stuff failed
        fire_fail_event() <- your fail event
        return 1 <- fail
    return 0 <- success

Good luck :)

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • Thanks but I would want to know whether the task `my_task` has Succeeded or Failed. More precisely, I want to receive a notification kind of like having a listener that is just waiting for the task to succeed or fail, then act accordingly. – KudzieChase Apr 07 '17 at 10:23
  • Then instead of the events, use a return statement (typically 0: success, 1:fail). I will edit my answer – John Moutafis Apr 07 '17 at 10:24
  • When I do that I seem to keep getting a fail since the task might be pending at the point I check what it returns. Check back to my earlier comment – KudzieChase Apr 07 '17 at 10:27
  • That is why I was initially suggesting to fire your events from within the celery task! – John Moutafis Apr 07 '17 at 10:28
  • Good to know mate :) – John Moutafis Apr 07 '17 at 10:50