强制停止芹菜工作者作为系统服务运行

时间:2017-07-27 16:15:01

标签: python ubuntu celery systemd

当我重新启动服务器并获得与以下语句相同的效果时,如何终止工作程序:

pkill -9 -f 'celery worker'

来自芹菜文件:

  

如果工人在经过考虑的时间后不会关闭,因为被困在无限循环或类似环境中,你可以使用KILL信号强制终止工人:

但是我作为systemd服务启动并使用以下配置来使用以下systemd单元文件启动它:

[Unit]
Description=Celery Service
After=network.target

[Service]
Type=forking
User=dsangvikar
Group=www-data
EnvironmentFile=-/etc/default/celery
WorkingDirectory=/home/dsangvikar/apps/msbot/
ExecStart=/home/dsangvikar/apps/msbot/msbotenv/bin/celery multi start \
-A microsoftbotframework.runcelery.celery chatbotworker --concurrency=4 \ 
--workdir=/home/dsangvikar/apps/msbot/ --logfile=/var/log/celery/%n.log 
--pidfile=/var/run/celery/%n.pid
ExecStop=/home/dsangvikar/apps/msbot/msbotenv/bin/celery multi stopwait
RuntimeDirectory=celery

[Install]
WantedBy=multi-user.target

当我sudo systemctl status celery时,我获得状态和pid。我用它杀了它。但是工作进程不会退出。我想强行杀死他们。我的程序逻辑在每次重启服务器时重新创建它们。但是,即使在系统重启时它们也没有被杀死,所以各种不同的问题都会出现。

1 个答案:

答案 0 :(得分:0)

Celery multi不应该用于生产。

这就是我正在使用的:

它启动10个主要流程,每个流程包含2个工作人员。总共有20个工人流程。

[program:celery_worker]
numprocs=10
process_name=%(program_name)s-%(process_num)s
directory=/opt/worker/main
environment=PATH="/opt/worker/main/bin:%(ENV_PATH)s"
command=/opt/worker/main/bin/celery worker -n worker%(process_num)s.%%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E
stdout_logfile=/var/log/celery/%(program_name)s-%(process_num)s.log
user=username
autostart=true
autorestart=true
startretries=99999
startsecs=10
stopsignal=TERM
stopasgroup=false
stopwaitsecs=7200
killasgroup=true

如果您正在运行作业,则不希望将TERM信号发送到PoolWorker进程,因为它会导致作业提前中止。你真正想要做的是将TERM信号发送到MainProcess,它将等待作业结束然后关闭。

所以你想要停止主要进程,如果它归结为杀死那么你想要作为一个群体杀死。

使用此命令启动工作线程关闭。如果工作人员未能在超级用户配置中stopwaitsecs时间退出,那么将发送一个终止信号,这将导致killasgroup被设置为真后的所有内容。

sudo supervisorctl stop celery_worker:*

上面的supervisord配置开始的示例。

username       1659  1.1  0.2 119796 45632 ?        S    10:45   0:06 [celeryd: celery@worker7.hostname:MainProcess] -active- (worker -n worker7.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1662  1.1  0.2 119804 45716 ?        S    10:45   0:06 [celeryd: celery@worker6.hostname:MainProcess] -active- (worker -n worker6.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1663  1.2  0.2 119724 45412 ?        S    10:45   0:06 [celeryd: celery@worker5.hostname:MainProcess] -active- (worker -n worker5.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1666  1.1  0.2 119732 45524 ?        S    10:45   0:05 [celeryd: celery@worker4.hostname:MainProcess] -active- (worker -n worker4.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1671  1.2  0.2 119792 45724 ?        S    10:45   0:06 [celeryd: celery@worker3.hostname:MainProcess] -active- (worker -n worker3.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1674  1.2  0.2 119792 45420 ?        S    10:45   0:06 [celeryd: celery@worker2.hostname:MainProcess] -active- (worker -n worker2.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1678  1.1  0.2 119712 45708 ?        S    10:45   0:05 [celeryd: celery@worker1.hostname:MainProcess] -active- (worker -n worker1.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1679  1.2  0.2 119808 45476 ?        S    10:45   0:06 [celeryd: celery@worker0.hostname:MainProcess] -active- (worker -n worker0.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1680  1.1  0.2 119796 45512 ?        S    10:45   0:05 [celeryd: celery@worker9.hostname:MainProcess] -active- (worker -n worker9.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1681  1.1  0.2 119720 45736 ?        S    10:45   0:06 [celeryd: celery@worker8.hostname:MainProcess] -active- (worker -n worker8.%h --app=python --time-limit=3600 -c 2 -Ofair -l debug --config=celery_config -E)
username       1796  0.0  0.2 118160 39660 ?        S    10:45   0:00 [celeryd: celery@worker9.hostname:PoolWorker-1]
username       1797  0.0  0.2 118232 39548 ?        S    10:45   0:00 [celeryd: celery@worker8.hostname:PoolWorker-1]
username       1798  0.0  0.2 118152 39532 ?        S    10:45   0:00 [celeryd: celery@worker3.hostname:PoolWorker-1]
username       1799  0.0  0.2 118156 39652 ?        S    10:45   0:00 [celeryd: celery@worker2.hostname:PoolWorker-1]
username       1800  0.0  0.2 118168 39748 ?        S    10:45   0:00 [celeryd: celery@worker7.hostname:PoolWorker-1]
username       1801  0.0  0.2 118164 39608 ?        S    10:45   0:00 [celeryd: celery@worker6.hostname:PoolWorker-1]
username       1802  0.0  0.2 118192 39768 ?        S    10:45   0:00 [celeryd: celery@worker1.hostname:PoolWorker-1]
username       1803  0.0  0.2 118200 39728 ?        S    10:45   0:00 [celeryd: celery@worker5.hostname:PoolWorker-1]
username       1804  0.0  0.2 118168 39756 ?        S    10:45   0:00 [celeryd: celery@worker0.hostname:PoolWorker-1]
username       1805  0.0  0.2 118188 39692 ?        S    10:45   0:00 [celeryd: celery@worker4.hostname:PoolWorker-1]
username       1806  0.0  0.2 118152 39536 ?        S    10:45   0:00 [celeryd: celery@worker3.hostname:PoolWorker-2]
username       1807  0.0  0.2 118232 39544 ?        S    10:45   0:00 [celeryd: celery@worker8.hostname:PoolWorker-2]
username       1808  0.0  0.2 118164 39608 ?        S    10:45   0:00 [celeryd: celery@worker6.hostname:PoolWorker-2]
username       1809  0.0  0.2 118200 39732 ?        S    10:45   0:00 [celeryd: celery@worker5.hostname:PoolWorker-2]

如果您希望立即停止,请将stopwaitsecs设置为1。

lpiner@hostname:~$ sudo supervisorctl status
celery_worker:celery_worker-0    RUNNING   pid 2488, uptime 0:00:48
celery_worker:celery_worker-1    RUNNING   pid 2487, uptime 0:00:48
celery_worker:celery_worker-2    RUNNING   pid 2486, uptime 0:00:48
celery_worker:celery_worker-3    RUNNING   pid 2485, uptime 0:00:48
celery_worker:celery_worker-4    RUNNING   pid 2484, uptime 0:00:48
celery_worker:celery_worker-5    RUNNING   pid 2483, uptime 0:00:48
celery_worker:celery_worker-6    RUNNING   pid 2482, uptime 0:00:48
celery_worker:celery_worker-7    RUNNING   pid 2481, uptime 0:00:48
celery_worker:celery_worker-8    RUNNING   pid 2490, uptime 0:00:48
celery_worker:celery_worker-9    RUNNING   pid 2489, uptime 0:00:48
lpiner@hostname:~$ sudo supervisorctl stop celery_worker:*
celery_worker:celery_worker-7: stopped
celery_worker:celery_worker-6: stopped
celery_worker:celery_worker-5: stopped
celery_worker:celery_worker-4: stopped
celery_worker:celery_worker-3: stopped
celery_worker:celery_worker-2: stopped
celery_worker:celery_worker-1: stopped
celery_worker:celery_worker-0: stopped
celery_worker:celery_worker-9: stopped
celery_worker:celery_worker-8: stopped
lpiner@hostname:~$ sudo supervisorctl status
celery_worker:celery_worker-0    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-1    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-2    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-3    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-4    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-5    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-6    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-7    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-8    STOPPED   Aug 02 11:17 AM
celery_worker:celery_worker-9    STOPPED   Aug 02 11:17 AM