2024-03-13|閱讀時間 ‧ 約 23 分鐘

[Laravel] 使用Supervisor執行Laravel的Queue Worker

此文章同步刊登於我的部落格


在使用laravel中的Queue job的時候,除了程式本身寫好之外

伺服器環境還會需要確保Laravel的queue:work指令可以持續進行

如果中斷還可以重新啟動這個時候就會需要用到Supervisor這個工具了


一、安裝Supervisor

apt update
apt install supervisor


二、啟動Supervisor

# supervisord -c <設定檔路徑> (預設/etc/supervisor/supervisord.conf)
supervisord -c /etc/supervisor/supervisord.conf


三、建立queue worker設定檔

cd /etc/supervisor/conf.d
vim laravel-worker.conf

設定檔內容(註解要刪除)

[program:laravel-worker-my-project]
process_name=%(program_name)s_%(process_num)02d
# command=php <專案資料夾路徑>/artisan queue:work --sleep=<每次嘗試間隔> --tries=<最多嘗試次數>
command=php /var/www/my-project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=2
startsecs=0
redirect_stderr=true
# stdout_logfile=<log檔案路徑>
stdout_logfile=/var/log/supervisor/laravel-worker-my-project.log
# logfile_maxbytes=<LOG檔案上限>
logfile_maxbytes=104857600
stopwaitsecs=3600


四、載入設定檔並啟用

supervisorctl reread
supervisorctl update
supervisorctl restart laravel-worker-my-project:*


五、查看服務是否啟用

supervisorctl status


環境

  • OS: Ubuntu 18.04
分享至
成為作者繼續創作的動力吧!
主要分享PHP相關的技術文章,包含Laravel與Codeigniter等,也許也會有其他範疇的內容。 歡迎交流~
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言