Celery增加Systemd配置
Posted 小斌哥ge
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Celery增加Systemd配置相关的知识,希望对你有一定的参考价值。
Celery增加Systemd配置
一、设置python celery项目的配置
- 在/etc/conf.d/目录创建celery文件,编辑
# Name of nodes to start
# here we have a single node
CELERYD_NODES="work"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the celery command:
CELERY_BIN="/usr/local/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"
# App instance to use
# comment out this line if you dont use an app
CELERY_APP="celery_crontab"
# or fully qualified:
#CELERY_APP="main.tasks:app"
# How to call manage.py
CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
- 配置项说明
2.1 CELERYD_NODES=“work”,启动的celery进程的进程名为work
2.2 CELERY_BIN="/usr/local/bin/celery",celery安装目录,可使用which celery找到
2.3 CELERY_APP=“celery_crontab” ,在celery项目的配置文件config.py中,创建Celery实例对象APP时给APP定义的名字,一定要保持一致,如:
# config.py中创建celery对象时的命名
app = Celery(celery_crontab, broker=amqp://guest@localhost//)
二、设置systemd配置
- 在/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=/root/celery_crontab
ExecStart=/usr/local/bin/celery multi start work -A main -l info -B --
logfile=celerylog.log
ExecStop=/usr/local/bin/celery multi stop work -A main -l info -B --
logfile=celerylog.log
ExecReload=/usr/local/bin/celery multi restat work -A main -l info -B --
logfile=celerylog.log
[Install]
WantedBy=multi-user.target
- 配置项说明
[Unit]
Description:对当前服务的简单描述,如说明一下功能
After:表示celery.service应该在network.target后启动
[Service]
Type:定义启动类型,forking表示以fork()方式启动
User:指定启动任务的用户(提前创建好用户和所属组,设置好用户权限)
Group:指定用户的组
EnvironmentFile:指定celery项目的systemd配置文件:/etc/conf.d/celery
WorkingDirectory:指定celery项目的启动目录,项目启动文件main.py所在目录
ExecStart:在执行systemctl start celery.service命令时,会执行ExecStart
ExecStop:在执行systemctl stop celery.service命令时,会执行ExecStop
ExecReload:在执行systemctl restart celery.service命令时,会执行ExecReload
[Install]
WantedBy=multi-user.target:表示重启系统后自动启动celery.service
三、使用systemd运行celery.service
- 重载配置文件
每次修改celery.service配置后都要执行此命令,以便systemd确认该文件
systemctl daemon-reload - 启动命令
systemctl start celery.service - 停止命令
systemctl stop celery.service - 重启命令
systemctl restart celery.service - 查看celery.service的运行状态
systemctl status celery.service
以上是关于Celery增加Systemd配置的主要内容,如果未能解决你的问题,请参考以下文章
Celery,Tornado,Supervisor构建和谐的分布式系统