If-else workflow with Celery Tasks


Sometimes we need to link tasks according to the outcomes, similar to an if/else flow. With Celery is possible to do it using link and link_error.

To give you an idea, let’s say that we need to send an important SMS to our customer. If this succeed, we should sync this SMS to our CRM tool; if something went wrong, we should contact IT right away.

We can do it by linking our send_sms task to other tasks. We use link for tasks that must be executed after our main task and link_error for tasks that should be executed if something goes wrong.

send_sms.apply_async(
  args=[body, customer.phone_number],
  link=sync_sms_to_crm.si(customer.pk, message),
  link_error=send_email_to_it.si(customer.pk, message)
)

You can combine linking with other Celery workflow structures making your workflow more consistent on writing less code.

Translations


comments powered by Disqus