简体   繁体   English

django DEBUG设置为False时,Celery没有看到任务

[英]Celery doesn't see tasks when django DEBUG is set to False

I've got a Django application with Celery. 我在Celery上有一个Django应用程序。 Tasks are described in tasks.py files with @shared_task in different apps, each has its own AppConfig and is being installed (added to INSTALLED_APP) correctly. 在不同应用程序中带有@shared_task的task.py文件中描述了任务,每个文件都有自己的AppConfig,并且已正确安装(添加到INSTALLED_APP中)。

Everything worked fine until I turned DEBUG off. 一切正常,直到我关闭了DEBUG。 Celery started to fail due to tasks of unregistered type. 由于未注册类型的任务,芹菜开始失败。 With the DEBUG variable Celery can see all of my tasks and work correctly. 使用DEBUG变量,Celery可以查看我的所有任务并正常工作。

EDIT: My celery.py code is as following: 编辑:我的celery.py代码如下:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

app = Celery('APP')
app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Running manage.py check shows no errors, all config files and logs have correct rights. 运行manage.py check不会显示任何错误,所有配置文件和日志都具有正确的权限。

What can cause such a bug? 什么会导致这种错误?

The thing that was causing such a strange bug was my log files permissions. 导致这种奇怪错误的原因是我的日志文件权限。

I have the following in my settings.py: 我的settings.py中包含以下内容:

if not DEBUG:
    WORKER_HANDLERS = {}
    for app in PROJECT_APPS:
        WORKER_HANDLERS['file_{0}'.format(app)] = {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/var/log/app/{0}.log'.format(app),
        }

    LOGGING['handlers'].update(WORKER_HANDLERS)

While installing my DEBIAN package in postinst script, i was calling collectstatic command from root, which created all of my log files owned by root. 在postinst脚本中安装我的DEBIAN软件包时,我从root调用collectstatic命令,该命令创建了root拥有的所有日志文件。

That's why celery failed to run: actually there was an error starting my app. 这就是celery无法运行的原因:实际上,启动我的应用程序时出错。

Yet strange thing is that manage.py check was saying everything was ok. 然而奇怪的是, manage.py check表示一切正常。

Be careful with file permissions on log files. 注意日志文件的文件权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM