开发者社区> 问答> 正文

在django中启动芹菜task_monitor

查看芹菜文档,我可以看到任务监视器是在一个脚本中启动的(见下面)。在django的实现中(据我所知),情况并非如此,因为我必须在线程中启动任务监视器。 当前,我在第一次运行作业时启动监视器,然后在以后每次运行作业时检查它的状态(参见下面的内容)。这似乎不是一个好方法。 在django项目中为芹菜实例化任务监视器的正确方法是什么? 我似乎错过了一些很明显的东西。

# docs example
from celery import Celery

def my_monitor(app):
    state = app.events.State()

    def announce_failed_tasks(event):
        state.event(event)
        # task name is sent only with -received event, and state
        # will keep track of this for us.
        task = state.tasks.get(event['uuid'])

        print('TASK FAILED: %s[%s] %s' % (
            task.name, task.uuid, task.info(),))

    with app.connection() as connection:
        recv = app.events.Receiver(connection, handlers={
                'task-failed': announce_failed_tasks,
        })
        recv.capture(limit=None, timeout=None, wakeup=True)

if __name__ == '__main__':
    app = Celery(broker='amqp://guest@localhost//')

    # LAUNCHED HERE
    my_monitor(app)
# my current implementation
# If the celery_monitor is not instantiated, set it up
app = Celery('scheduler',
             broker=rabbit_url,  # Rabbit-MQ
             backend=redis_url,  # Redis
             include=tasks
             )

celery_monitor = Thread(target=build_monitor, args=[app], name='monitor-global', daemon=True)

# import celery_monitor into another module
global celery_monitor

if not celery_monitor.is_alive():
    try:
        celery_monitor.start()
        logger.debug('Celery Monitor - Thread Started (monitor-retry) ')
    except RuntimeError as e:  # occurs if thread is dead
        # create new instance if thread is dead
        logger.debug('Celery Monitor - Error restarting thread (monitor-rety): {}'.format(e))
        celery_monitor = Thread(target=build_monitor, args=[app], name='monitor-retry', daemon=True)
        celery_monitor.start()  # start thread
        logger.debug('Celery Monitor - Thread Re-Started (monitor-retry) ')
else:
    logger.debug('Celery Monitor - Thread is already alive. Dont do anything.')

问题来源StackOverflow 地址:/questions/59379268/launching-celery-task-monitor-in-django

展开
收起
kun坤 2019-12-30 09:35:45 447 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Monitoring the Dynamic Resource Usage of Scala and Python Spark Jobs in Yarn 立即下载
《基于 Service Worker 实现在线代理》 立即下载
In-Flux Limiting for a Multi-Tenant Logging Service 立即下载