我可以use signals记录任务执行时间,但我想记录队列上的时间。信号有可能吗?我应该使用哪些信号?
答案 0 :(得分:1)
任务事件可用于根据任务事件监视和触发操作。任务发送,任务接收,任务启动,任务成功,任务失败,任务拒绝,任务撤销,任务重试是任务事件 芹菜支持。有关详细信息,请参阅此link。要记录任务在队列中等待的时间,请使用相应的任务事件处理程序获取创建(或添加到作业队列)时间和任务开始时间的任务。它们的不同将给出队列中作业的等待时间。下面是一个关于如何实现它的示例python代码。
from celery import Celery
redis = Redis(host='workerdb', port=6379, db=0)
taskId_startTime = {}
taskId_createTime = {}
def my_monitor():
app = Celery('vwadaptor', broker='redis://workerdb:6379/0',backend='redis://workerdb:6379/0')
state = app.events.State()
def announce_task_received(event):
state.event(event)
task = state.tasks.get(event['uuid'])
taskId_createTime[task.uuid] = task.timestamp
def announce_task_started(event):
state.event(event)
task = state.tasks.get(event['uuid'])
taskId_startTime[task.uuid] = task.timestamp
def announce_task_succeeded(event):
state.event(event)
task = state.tasks.get(event['uuid'])
print "wait time in queue", taskId_startTime[task.uuid] - taskId_createTime[task.uuid]
with app.connection() as connection:
recv = app.events.Receiver(connection, handlers={
'task-received': announce_task_received,
'task-started': announce_task_started,
'task-succeeded': announce_task_succeeded,
})
recv.capture(limit=None, timeout=None, wakeup=True)
my_monitor()