【问题标题】:How to get the status of celery broker and backend?如何获取 celery broker 和后端的状态?
【发布时间】:2019-08-13 10:05:21
【问题描述】:
celery 中是否有一种干净的方法可以知道其代理和/或结果后端是否已关闭?
我正在使用带有 RabbitMQ 代理和 Redis 后端的 celery。
目前,我发现最简单的方法是提交一个虚拟任务,当代理关闭时会引发kombu.exceptions.OperationalError,当后端关闭时会引发redis.exceptions.ConnectionError。
但是,这感觉很老套。有没有更好的办法?
【问题讨论】:
标签:
python
redis
rabbitmq
celery
【解决方案1】:
深入研究 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")