Force stop celery workers running as a systemd service

一曲冷凌霜 提交于 2019-12-12 04:21:32

问题


How do I kill the workers when I reboot the server and get the same effect as the following statement:

pkill -9 -f 'celery worker'

From the celery documentation:

If the worker won’t shut down after considerate time, for being stuck in an infinite-loop or similar, you can use the KILL signal to force terminate the worker:

But I am starting as a systemd service and have the following config to start it using the following systemd unit file:

[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

When I do sudo systemctl status celery, I get the status and pid. I use it to kill it. But the worker process doesn't exit. I want to force kill them. My program logic recreates them on every server restart. But since they are not killed even on a system reboot, all kinds of different problems are cropping up.


回答1:


Celery multi is not supposed to be used for production.

This is what I'm using:

It starts 10 main processes with 2 workers each. So a total of 20 worker processes.

[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

If you have jobs running you don't want to send TERM signals to the PoolWorker process as it will cause the job to abort early. What you really want to do is send the TERM signal to MainProcess which will wait for the job to end and then close.

So you want to stop the primary processes and if it comes down to kill then you want to kill as a group.

Use this command to start the worker shutdown. If the workers fail to exit by the stopwaitsecs time in the supervisor config then a kill signal will be sent and that will kill everything since killasgroup is set to true.

sudo supervisorctl stop celery_worker:*

Example of what the supervisord config above starts.

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]

If you want stops to happen instantly then set stopwaitsecs to 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


来源:https://stackoverflow.com/questions/45356059/force-stop-celery-workers-running-as-a-systemd-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!