How to get the status of celery broker and backend?

Question:

Is there a clean way in celery for knowing whether its broker and/or the result backend are down or not?

I am using celery with RabbitMQ broker and Redis backend.

Currently, the easiest way I found is submitting a dummy task which would raise kombu.exceptions.OperationalError when the broker is down, and redis.exceptions.ConnectionError when the backend is down.

This feels hacky, however. Is there a better way?

Asked By: Quantum-Collapse

||

Answers:

After digging into the source files of Celery I ended up using the following

import celery
import kombu
import redis

try:
    with celery.current_app.connection_for_write() as conn:
        conn.connect()
        conn.release()
        print("Broker is working")
except(ConnectionError, kombu.exceptions.OperationalError):
    print("Broker is down")

try:
    celery.current_app.backend.get('Whatever')
    print("Backend is working")
except(ConnectionError, redis.exceptions.ConnectionError):
    print("Backend is down")
Answered By: Quantum-Collapse

it’s not handle check rabbitmq connection

Answered By: Jaffar Esmaili
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.