在审查了许多关于同一问题的线程后,我还没有找到答案。
我正在运行一个Django应用程序,其中包含以下内容
Python:3.6
Django:3.0.5
芹菜:4.0.2
Kombu:4.2.0
我用docker compose运行所有堆栈,芹菜在不同的容器中运行。显然,我的任务是在芹菜中注册,因为如果我检查应用程序的注册任务,我会得到一个包含单个元素的列表,即任务本身:
$ celery -A apps.meals.tasks inspect registered
-> celery@7de3143ddcb2: OK
* apps.meals.tasks.send_slack_notification
myproj/settings.py
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps',
'apps.meals',
]
myproj/celery.py
:
from __future__ import absolute_import, unicode_literals
from celery import Celery
import os
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproj.settings')
app = Celery('myproj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
app.conf.update(
BROKER_URL=settings.BROKER_URL,
)
我的任务在meals
应用程序的另一个地方,位于一个名为tasks.py
的文件中。
apps/meals/tasks.py
from django.conf import settings
from slackclient import SlackClient
from celery.utils.log import get_task_logger
from myproj import celery_app
from json import loads, dumps
logger = get_task_logger(__name__)
slack_markdown_text = "Hola!n El menu de hoy es:n {content}n Pueden enviar su pedido aca: {link}n Saludos!"
@celery_app.task(name="apps.meals.tasks.send_slack_notification")
def send_slack_notification(serial_menu, serial_options):
...
我的文件结构如下:
technical-tests/
|
|--apps/
|----*snap
|----meals/
|------*snap
|------tasks.py
|--myproj
|----*snap
|----celery.py
|----settings.py
最后,docker-compose.yml是这样的:
version: '3.5'
services:
backend: ....
celery:
build:
context: ..
dockerfile: ./deploy/Dockerfile
volumes:
- ../code/:/opt/somelocation
environment:
- SECRET_KEY
- SLACK_TOKEN
- SLACK_CHANNEL
- SLACK_URL_PATTERN
- BROKER_URL
command: celery -A apps.meals.tasks worker -l info
depends_on:
- backend
- redis
芹菜工作控制台:
Celery控制台在启动时不会在注册任务出现时显示出漂亮的彩色屏幕,所以这很可疑。它只显示:
celery_1_aa9c50e916ae | /usr/local/lib/python3.6/site-packages/celery/platforms.py:796: RuntimeWarning: You're running the worker with superuser privileges: this is
celery_1_aa9c50e916ae | absolutely not recommended!
celery_1_aa9c50e916ae |
celery_1_aa9c50e916ae | Please specify a different user using the --uid option.
celery_1_aa9c50e916ae |
celery_1_aa9c50e916ae | User information: uid=0 euid=0 gid=0 egid=0
celery_1_aa9c50e916ae |
celery_1_aa9c50e916ae | uid=uid, euid=euid, gid=gid, egid=egid,
celery_1_aa9c50e916ae | [2020-04-08 14:05:34,024: INFO/MainProcess] Connected to redis://redis:6379/0
celery_1_aa9c50e916ae | [2020-04-08 14:05:34,034: INFO/MainProcess] mingle: searching for neighbors
celery_1_aa9c50e916ae | [2020-04-08 14:05:35,053: INFO/MainProcess] mingle: all alone
celery_1_aa9c50e916ae | [2020-04-08 14:05:35,068: WARNING/MainProcess] /usr/local/lib/python3.6/site-packages/celery/fixups/django.py:200: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
celery_1_aa9c50e916ae | warnings.warn('Using settings.DEBUG leads to a memory leak, never '
celery_1_aa9c50e916ae | [2020-04-08 14:05:35,069: INFO/MainProcess] celery@7de3143ddcb2 ready.
我知道该任务没有运行,因为在python shell中,当我在没有delay
的情况下调用send_slack_notifications
时,它会立即运行,但当我使用delay
时,命令将永远挂起,芹菜控制台不会移动,就像它没有接收到任何信息一样。
任何见解都将不胜感激!
更新
delay
调用正在apps/meals/views.py
中进行
from apps.meals.tasks import send_slack_notification
@login_required
def notify_menu(request, uuid):
if is_staff(request.user):
menu = Menu.objects.filter(uuid=uuid)
send_slack_notification.delay(
serialize(menu),
serialize(menu.first().list_options())
)
- 在芹菜配置中,添加值为
1
的环境变量C_FORCE_ROOT
- 使用django设置中的
CELERY_BROKER_URL
从设置中正确设置celeni broker,而不要从环境中传入BROKER_URL
(celeni会感到困惑,因为环境变量可能会覆盖设置变量 - 不要使用
app.conf.update
设置BROKER_URL
,请使用如上所述的设置CELERY_BROKER_URL