Search code examples
pythoncelery

What's the best way to stop celery app if worker initializing failed


I have some logic in worker_process_init signal:

@worker_process_init.connect
def init_per_worker_publisher(**kwargs):
    # some init logic...

If there appears unhandled exception, setup function is failed and no init happens but worker app keeps running like nothing happened.

I solved this problem like this:

@worker_process_init.connect
def init_per_worker_publisher(**kwargs):
    try:
        # some init logic...
    except Exception:
        celery_app.control.shutdown()

Maybe there is a better solution?


Solution

  • You can raise a WorkerShutdown exception.

    from celery.exceptions import WorkerShutdown
    
    @worker_process_init.connect
    def init_per_worker_publisher(**kwargs):
        try:
            # some init logic...
        except Exception:
            raise WorkerShutdown()