这是用于发送电子邮件的 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 安装的应用程序进行版本控制:
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'
随着 Django 在生产debug = False
中,Celery 不直接通过运行来工作: celery worker -A myapp --loglevel=info
,还需要启动 Celery 脚本守护进程
bin/celery worker --help
嵌入式节拍选项: -B, --beat 同时运行芹菜节拍定期任务调度程序。 请注意,只能有一个实例 这项服务。..注意:: -B 用于 开发目的。对于生产环境,您 需要单独开始芹菜节拍。
重要参数:-B celery worker -A myapp --loglevel=info -B
你在哪里调用enviar_documentos_async
函数?可能是您提供客户端参数(客户端而不是客户端(的拼写错误。