我正在尝试在 Ubuntu 20.04、pipenv pipenv、版本 2023.2.18、FastAPI 0.95.1 和 Celery 5.2.7 中将 Celery 作为服务运行。
如果我在测试目录下做一个pipenv shell,然后做celery -A app.worker worker -loglevel=info,celery会正常运行。
然而,当我尝试用 systemd 守护芹菜时,我得到一个错误,我不知道我做错了什么。
这是我的 FastAPI 的项目树。
test
├─ app
│ ├─ dir1
│ └─ worker
│ ├─ __init__.py
│ └─ celery.py
├─ main.py
├─ Pipfile
└─ Pipfile.lock
这里是celery.py
from celery import Celery
import os
app = Celery('tasks',
broker=os.getenv('RABBITMQ_BROKER', 'amqp://user:password@localhost:5672'),
backend='rpc://',
)
app.autodiscover_tasks(['app.dir1'], force=True)
这里是/etc/tmpfiles.d/celery.conf
d /run/celery 0755 celery celery -
d /var/log/celery 0755 celery celery -
这里是/etc/conf.d/celery
CELERYD_NODES="w1"
CELERY_BIN="/home/jhpark/.local/share/virtualenvs/test-iwv-TRuN/bin/celery"
CELERY_APP='app.worker.celery:app'
CELERYD_MULTI="multi"
CELERYD_OPTS="--time-limit=300 --concurrency=8"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
和/etc/systemd/system/celery.service
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=celery
Group=celery
EnvironmentFile=/etc/conf.d/celery
WorkingDirectory=/home/jhpark/test/app
ExecStart=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi start $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}"'
ExecReload=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi restart $CELERYD_NODES \
--pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
--loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
Restart=always
[Install]
WantedBy=multi-user.target
当我使用
systemctl start celery.service
运行 celery.service 时,出现以下错误:
May 10 18:27:35 Artience-0128 systemd[1]: Starting Celery Service...
May 10 18:27:35 Artience-0128 sh[6699]: Usage: celery [OPTIONS] COMMAND [ARGS]...
May 10 18:27:35 Artience-0128 sh[6699]: Try 'celery --help' for help.
May 10 18:27:35 Artience-0128 sh[6699]: Error: Invalid value for '-A' / '--app':
May 10 18:27:35 Artience-0128 sh[6699]: Unable to load celery application.
May 10 18:27:35 Artience-0128 sh[6699]: The module /home/jhpark/test/app/worker/celery was not found.