1.在laravel項目目錄運行
php artisan queue:work
2.啟動隊列監聽服務
queue:work 命令有個缺陷,就是每次有新任務推送到隊列后需要手動登錄到服務器并運行該命令,任務才會被執行,這顯然是不合理的,對此我們可以使用一些自動化解決方案。
2.1一種方式是將 artisan queue:listen 命令加入到服務器啟動腳本中該命令會在新任務推送到隊列時自動調用 artisan queue:work。這種方案的問題是 queue:listen 命令會一直掛在那里,消耗CPU資源,而且一旦命令掛掉,新的任務還是無法執行,更好的解決方案是使用 Supervisor 來運行 queue:listen。
1.啟動隊列監聽服務
通過下面這條指令啟動隊列監聽服務,它會自動處理隊列任務:
php artisan queue:listen
在linux中,如果想讓它在后臺執行,可以這樣:
nohup php artisan queue:listen &
在crontab -e中加入命令輪詢處理
* * * * * php /home/wwwroot/fcgadmin/artisan queue:work
2.2 php artisan queue:listen 命令加入到服務器啟動腳本
3.最優解Supervisor流程
yum install supervisor
安裝好后在/etc/會生成一個supervisord.conf文件及一個supervisord.d文件目錄
supervisord.conf是一些默認配置,可自行修改
vim /etc/supervisord.conf
[include]默認配置是制定.ini,因個人習慣命名為.conf文件,因此修改配置如下:
[include]
files = supervisord.d/*.conf
supervisord.d目錄用來存放用戶自定義的進程配置
cd /etc/supervisord.d
touch fcg.conf
編輯其內容
[program:fcg-queue-listen]
command=php /home/wwwroot/fcgadmin/artisan queue:listen
user=root
process_name=%(program_name)s_%(process_num)d
directory=/home/wwwroot/fcgadmin
stdout_logfile=/var/log/supervisor/supervisord.log
redirect_stderr=true
numprocs=1
4.查看進程listen
ps -ef |grep listen
kill pid 殺死進程
5.supervisorctl 是 supervisord的命令行客戶端工具
0.supervisord -c /etc/supervisord.conf 啟動supervisord服務
1.supervisorctl status:查看所有進程的狀態
2.supervisorctl stop es:停止es
3.supervisorctl start es:啟動es
4.supervisorctl restart es: 重啟es
5.supervisorctl update :配置文件修改后可以使用該命令加載新的配置
6.supervisorctl reload: 重新啟動配置中的所有程序