配置Supervisor管理Laravel隊列

安裝

可以用Python的pip工具進行安裝

sudo pip install supervisor

也可以使用Linux的包管理命令安裝

sudo apt-get install supervisor

配置

運行如下命令可以查看默認的配置

echo_supervisord_conf

如果需要修改默認的配置,可以運行下面命令生成一個默認的配置文件

echo_supervisord_conf > /etc/supervisord.conf

修改默認配置

默認配置文件中的supervisord.sock躯畴、supervisord.log以及supervisord.pid是放在/tmp目錄下影涉,這個目錄存放的是Linux中的臨時文件痪寻,一旦被系統(tǒng)刪除硝桩,就會提示unix:///tmp/supervisor.sock no such file笙僚,所以我們要把這三個文件放到其他目錄中保存成福。

此處注意中文注釋的幾個需要修改的地方

[unix_http_server]
;此處修改為/var/run目錄碾局,避免被系統(tǒng)刪除
file=/var/run/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

[supervisord]
;此處修改為/var/log/supervisor目錄
logfile=/var/log/supervisor/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
;此處修改為/var/run目錄
pidfile=/var/run/supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=chrism                 ; default is current user, required if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp              ; default is not to cd during start
;nocleanup=true              ; don't clean up tempfiles at start; default false
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false            ; strip ansi escape codes in logs; def. false

; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work.  Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; The supervisorctl section configures how supervisorctl will connect to
; supervisord.  configure it match the settings in either the unix_http_server
; or inet_http_server section.

[supervisorctl]
;此處是關(guān)鍵,否則執(zhí)行supervisorctl status會爆出unix:// not found錯誤
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0,2                 ; 'expected' exit codes used with autorestart (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample group section below shows all possible group values.  Create one
; or more 'real' group: sections to create "heterogeneous" process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

奴艾;在最后加上include净当,新增的配置將在/etc/supervisord.d/conf.d目錄下并以.conf結(jié)尾
[include]
files = /etc/supervisord.d/conf.d/*.conf

上面的一些目錄如果做了修改首先要創(chuàng)建這些目錄,并且賦予相應(yīng)的權(quán)限

sudo chmod 777 /var/run
sudo chmod 777 /var/log/supervisor

現(xiàn)在我們啟動服務(wù)

supervisord -c /etc/supervisord.conf

此處最好別用root權(quán)限啟動蕴潦,否則可能導(dǎo)致后面php artisan命令出錯

現(xiàn)在我們查看進程

pgrep supervisord | xargs ps -u --pid
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     13744  0.0  1.7 226320 17460 ?        Ss   1月12   0:08 /bin/python /usr/bin/supervisord -c /etc/supervisord.conf

具體項目配置

Supervisorctl 是 Supervisord 的一個命令行客戶端工具像啼,啟動時需要指定與 Supervisord 使用同一份配置文件

我們在上面默認配置文件中加入了[include],所以我們在/etc/supervisord.d/conf.d下新建一個配置文件larashop-worker.conf

[program:larashop-worker]  ;項目名稱
process_name=%(program_name)s_%(process_num)02d
command=php (填入你的artisan路徑)/artisan queue:work redis --sleep=3 --tries=3  ;需要啟動的命令
autostart=true
autorestart=true
user=root ;此處填入你運行WEB應(yīng)用的用戶
numprocs=8  ;進程數(shù)
redirect_stderr=true  ;把 stderr 重定向到 stdout品擎,默認 false
stdout_logfile=/var/log/supervisor/larashop-queue.log  ;注意分配好日志文件夾權(quán)限

配置完成后我們執(zhí)行

supervisorctl reread  # 讀取有更新(增加)的配置文件埋合,不會啟動新添加的程序

supervisorctl update # 重啟配置文件修改過的程序

supervisorctl start larashop-worker:* # 啟動 larashop-worker 程序

運行

supervisorctl status # 查看狀態(tài)

看到

larashop-worker:larashop-worker_00   RUNNING   pid 13769, uptime 14:03:33
larashop-worker:larashop-worker_01   RUNNING   pid 13770, uptime 14:03:33
larashop-worker:larashop-worker_02   RUNNING   pid 13767, uptime 14:03:33
larashop-worker:larashop-worker_03   RUNNING   pid 13768, uptime 14:03:33
larashop-worker:larashop-worker_04   RUNNING   pid 13765, uptime 14:03:33
larashop-worker:larashop-worker_05   RUNNING   pid 13766, uptime 14:03:33
larashop-worker:larashop-worker_06   RUNNING   pid 13763, uptime 14:03:33
larashop-worker:larashop-worker_07   RUNNING   pid 13764, uptime 14:03:33

說明Laravel隊列已經(jīng)開始正常運行了

如果在Laravel中修改了隊列代碼,需要重啟Supervisor才能生效

可能出現(xiàn)的問題

1. ERROR (spawn error)

此時如果提示

larashop-worker:larashop-worker_00: ERROR (spawn error)
...

先檢查上述的日志目錄有沒有權(quán)限萄传,如果權(quán)限正常甚颂,則進入上面設(shè)置的日志目錄查看日志文件

我這里進入var/log/supervisor查看日志文件larashop-queue.conf

可以看到錯誤均為

PHP Parse error:  syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in xxx/artisan on line 33

然后我進入Laravel目錄,使用

sudo php artisan 你的命令

執(zhí)行artisan命令秀菱,同樣爆出這個錯誤振诬。所以原因在于啟動supervisord和服務(wù)端的時候使用了root權(quán)限導(dǎo)致

先將原來的進程kill掉,使用非root權(quán)限重新執(zhí)行服務(wù)端和客戶端即可成功運行

2. unix:///tmp/supervisor.sock no such file

在服務(wù)端配置文件中將/tmp/supervisor.sock修改為var/run/supervisor.sock

3. supervisorctl start錯誤

執(zhí)行supervisorctl start 你的項目名稱衍菱,遲遲不出結(jié)果赶么,按ctrl+c后爆出錯誤

Traceback (most recent call last):
  File "/bin/supervisorctl", line 11, in <module>
  ...

建議kill掉supervisord的進程,然后重新用pip安裝一次脊串,并重新配置一遍默認配置文件

鏈接

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辫呻,一起剝皮案震驚了整個濱河市清钥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌放闺,老刑警劉巖祟昭,帶你破解...
    沈念sama閱讀 211,290評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異怖侦,居然都是意外死亡篡悟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評論 2 385
  • 文/潘曉璐 我一進店門匾寝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搬葬,“玉大人,你說我怎么就攤上這事艳悔〖被耍” “怎么了?”我有些...
    開封第一講書人閱讀 156,872評論 0 347
  • 文/不壞的土叔 我叫張陵很钓,是天一觀的道長香府。 經(jīng)常有香客問我,道長码倦,這世上最難降的妖魔是什么企孩? 我笑而不...
    開封第一講書人閱讀 56,415評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮袁稽,結(jié)果婚禮上勿璃,老公的妹妹穿的比我還像新娘。我一直安慰自己推汽,他們只是感情好补疑,可當我...
    茶點故事閱讀 65,453評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著歹撒,像睡著了一般莲组。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上暖夭,一...
    開封第一講書人閱讀 49,784評論 1 290
  • 那天锹杈,我揣著相機與錄音,去河邊找鬼迈着。 笑死竭望,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的裕菠。 我是一名探鬼主播咬清,決...
    沈念sama閱讀 38,927評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了旧烧?” 一聲冷哼從身側(cè)響起影钉,我...
    開封第一講書人閱讀 37,691評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎粪滤,沒想到半個月后斧拍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雀扶,經(jīng)...
    沈念sama閱讀 44,137評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡杖小,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,472評論 2 326
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了愚墓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片予权。...
    茶點故事閱讀 38,622評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡忆嗜,死狀恐怖椎例,靈堂內(nèi)的尸體忽然破棺而出制妄,到底是詐尸還是另有隱情徒仓,我是刑警寧澤瘤旨,帶...
    沈念sama閱讀 34,289評論 4 329
  • 正文 年R本政府宣布鲫懒,位于F島的核電站因悲,受9級特大地震影響嘶居,放射性物質(zhì)發(fā)生泄漏厚者。R本人自食惡果不足惜躁劣,卻給世界環(huán)境...
    茶點故事閱讀 39,887評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望库菲。 院中可真熱鬧账忘,春花似錦、人聲如沸熙宇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽烫止。三九已至蒋荚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間馆蠕,已是汗流浹背期升。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留荆几,地道東北人吓妆。 一個月前我還...
    沈念sama閱讀 46,316評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像吨铸,于是被迫代替她去往敵國和親行拢。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,490評論 2 348

推薦閱讀更多精彩內(nèi)容

  • 記得喵喵說過:如果別人的言語引發(fā)你的情緒诞吱,那是你自己的問題舟奠,而不是別人的問題竭缝。 今天有點更深的體會,有時候自尊心會...
    魚兒飛飛_閱讀 113評論 0 0
  • 愿舍去一個最能實現(xiàn)的心愿 只為來換取一次給你送一杯茶的機緣 送給那個永遠擁有慈悲之心的怡紅公子 在我最美好的年華 ...
    詩意_c3b1閱讀 84評論 0 2