2017-07-27 83 views
0

如何殺工人,當我重新啓動服務器,並獲得儘可能下面的語句相同的效果:運行的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。我用它來殺死它。但工作進程不會退出。我想強迫殺死他們。我的程序邏輯在每次服務器重啓時重新創建它們。但是,即使在系統重啓時它們也不會被殺死,所有各種不同的問題都在出現。

+0

我沒有與貫穿systemd芹菜經驗,但你可以用'須藤systemctl停止celery'停止? – randomir

+0

我可以。只是如果我做'sudo reboot',工作節點不會退出。重新啓動時,當芹菜試圖連接到redis併產生工人時,它會失敗。它還將啓動時間設置爲2 3分鐘。我想這是一個更系統的問題。 編輯:即我想在重新啓動之前自動殺死工人和芹菜進程,以便在服務器啓動時它具有乾淨的石板可用。 –

+0

這可能會更好[請問Ubuntu](https://www.askubuntu.com) – C8H10N4O2

回答

0

芹菜多不應該用於生產。

這是我使用的是什麼:

它從每個工人2 10個主要過程。所以共有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設置爲true,因此會終止一切。

sudo supervisorctl stop celery_worker:* 

以上supervisord配置開始的示例。

username  1659 1.1 0.2 119796 45632 ?  S 10:45 0:06 [celeryd: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]: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: [email protected]:PoolWorker-1] 
username  1797 0.0 0.2 118232 39548 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1798 0.0 0.2 118152 39532 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1799 0.0 0.2 118156 39652 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1800 0.0 0.2 118168 39748 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1801 0.0 0.2 118164 39608 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1802 0.0 0.2 118192 39768 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1803 0.0 0.2 118200 39728 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1804 0.0 0.2 118168 39756 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1805 0.0 0.2 118188 39692 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-1] 
username  1806 0.0 0.2 118152 39536 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-2] 
username  1807 0.0 0.2 118232 39544 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-2] 
username  1808 0.0 0.2 118164 39608 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-2] 
username  1809 0.0 0.2 118200 39732 ?  S 10:45 0:00 [celeryd: [email protected]:PoolWorker-2] 

如果你想停下來發生的瞬間,然後設置stopwaitsecs爲1

[email protected]:~$ 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 
[email protected]:~$ 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 
[email protected]:~$ 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