无法在Debug = False中使用Django启动工作进程Celery任务(生产)

时间:2019-04-29 09:45:47

标签: django redis celery

这是发送邮件的Celery任务,有趣的是,一切都可以在DEBUG = True模式下正常运行,但在生产环境DEBUG = False中则无法正常工作。在发送操作期间并在激活的调试模式下运行celery worker -A invoice2 --loglevel = debug会显示以下内容:

 ERROR/ForkPoolWorker-5] Task enviar_documentos[xxx] raised unexpected: TypeError("enviar_documentos_async() got an unexpected keyword argument 'cliente'",)

首先使用延迟启动发送文件:

from django.conf import settings
from notifications.tasks import send_async_documents

def send_documents (company, client, documents, type, template = "invoice", context = None):
    send_documents = send_documents_async.delay
    send_documents (type = type,
                    company = company.pk,
                    client = client.pk,
                    template = template,
                    context = context)

第二,任务:

from celery_app import app
@app.task (name = "send_documents")

def enviar_documentos_async (company, client, documents, type = "FacturaVenta", template = "invoice", context = None):
     if not context:
       context = {}
       klass = KLASS.get (type)
       queryset = klass.objects.filter (pk__in = documents)
       em = Empresa.objects.get (pk = company)
       cl = Cliente.objects.get (pk = client)
       .
       .
       return send_email(
           mail_from = "\"{0.name}\"><{0.email}>".format(em),
           mail_to = cl.email,
           type = template,
           context = context,
           attachments = attachments)

第三,发送电子邮件:

from django.core.mail import EmailMessage
from django.template import Context, Template

def send_email(mail_from, mail_to, type, context = None, attachments = None):
   subject = Template(subject.type).render(mail_context) .replace ("\ n", "")
   message = Template(type.body).render(mail_context)
   ...
   msg = EmailMessage (
          subject,
          linebreaks (message),
          mail_from,
          mail_to)

pip在virtualenv中安装的版本应用程序:

   celery 4.3.0

   Django 2.1.1

   django-extensions 2.0.7

   django-filter 2.1.0

   django-oauth-toolkit 1.1.3

   django-redis 4.10.0

   django-redis-cache 2.0.0

   djangorestframework 3.8.2

   redis 3.2.1

   redis-cache 0.1.5

   redis-structures 0.1.7

   kombu 4.5.0

在服务器Linux-4.9.0-3-amd64-x86_64-with-debian-9.0 + nginx / 1.10.2 + uwsgi 2.0.17.1:

Redis server v = 5.0.4 sha = 00000000: 0 malloc = jemalloc-5.1.0 bits = 64
Celery 4.3.0 (rhubarb)

Settings.py的一部分

DJANGO_ROOT = dirname (abspath (__ file__))
SITE_HTDOCS = normpath (join (DJANGO_ROOT, '../htdocs'))
STATIC_ROOT = normpath (join (SITE_HTDOCS, 'static'))
STATIC_URL = '/static/'
MEDIA_ROOT = normpath (join (SITE_HTDOCS, 'media'))
MEDIA_URL = '/media/'
...
# CELERY
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

2 个答案:

答案 0 :(得分:0)

您在哪里调用enviar_documentos_async函数? 可能是您提供 client 参数(使用cliente而不是client)的错字。

答案 1 :(得分:0)

在Django生产debug = False的情况下,Celery无法通过运行celery worker -A myapp --loglevel=info直接工作,因此还必须启动Celery脚本daemonization

bin/celery worker --help
  

嵌入式节拍选项:     -B,--beat同时运行celery beat定期任务计划程序。                           请注意,只能有一个实例                           这项服务。 ..注意:: -B用于                           发展宗旨。对于生产环境,您                           需要单独开始芹菜拍打。

重要参数:-B celery worker -A myapp --loglevel=info -B