VanDok_
VanDok_

Reputation: 23

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?

Upvotes: 2

Views: 442

Answers (1)

zion
zion

Reputation: 36

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()

Upvotes: 2

Related Questions