芹菜实时监控和Django应用程序 [英] Celery real time monitoring and Django app

查看:143
本文介绍了芹菜实时监控和Django应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

芹菜 docs 解释如何设置事件的自定义处理。然而,这种方法似乎并不适用于我的Django项目和芹菜。

Celery docs explain how to setup custom handling of events. This approach doesn't quite seem to work with my Django project and celery however.

我的 celery.py 文件看起来像

from __future__ import absolute_import 
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')

# The custom monitor copied from celery docs
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('MY MON 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)


app = Celery('myproj')


app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
# Monitoring
my_monitor(app)

我刚从文档复制了示例监视器。

I have just copied the example monitor from the docs.

我没有收到错误,但是启动一个芹菜工作者/ beat或django的runserver似乎挂起,直到我注释掉 my_monitor(app)行。

I don't get an error, but launching either a celery worker/beat or django's runserver just seems to hang forver until I comment out the my_monitor(app) line.

什么是在Django proj中执行此操作的正确方法?

What's the correct way to do this in a Django proj?

推荐答案

recv.capture是一个阻塞调用。您是否从事件处理程序获取任何输出?

recv.capture is a blocking call. Did you get any output from your event handler?

如果您尝试将django服务器用作监视器和任务生产者/消费者,那么不会工作。

If you're trying to use your django server as both a monitor and a task producer/consumer, that's not going to work.

您的显示器应该是一个单独的独立应用程序。你也应该考虑消除django依赖,因为它是不必要的。

Your monitor should be a separate stand-alone application. You should also consider eliminating the django dependency as it's unnecessary.

这篇关于芹菜实时监控和Django应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆