Restarting background callbacks in application code

My background callbacks make a request to a backend that may not have data, and will respond with HTTP 503 - temporary server error, with a ‘Retry-After’ header to communicate that the data may be available a bit later.

I’d like to have the background callback throw an exception that causes it to be retried later, freeing-up a Celery worker thread for other tasks in the queue. I could have a blocking loop that waits to check the API again after some amount of time, but that is leading to worker-pool exhaustion.

Celery tasks have a “bind” configuration parameter that lets the application code programmatically retry tasks by passing a ‘self’ argument to the task function. That’d be an ideal solution for background callbacks.

A reasonable alternative would be to use conditional retries. e.g. a defined “DashRetryException” that, when thrown, is caught by the Dash background callback decorator context and gracefully retries the callback logic later.

These are proposed solutions, but I’d love any alternative suggestions from the community.

1 Like