如何获取芹菜经纪人和后端的状态?

How to get the status of celery broker and backend?

celery 是否有一种干净的方法来了解其代理 and/or 结果后端是否已关闭?

我正在使用带有 RabbitMQ 代理和 Redis 后端的 celery。

目前,我发现最简单的方法是提交一个虚拟任务,该任务会在代理关闭时引发 kombu.exceptions.OperationalError,而在后端关闭时引发 redis.exceptions.ConnectionError

然而,这感觉很老套。有没有更好的方法?

深入研究 Celery 的源文件后,我最终使用了以下内容

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