2016-03-20 119 views

回答

0

任務事件可用於根據任務的事件監視和觸發操作。任務發送,任務接收,任務啓動,任務成功,任務失敗,任務拒絕,任務撤銷,任務重試是芹菜中支持的任務事件 。詳情請參考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()