配置Supervisor管理Laravel队列

安装

可以用Python的pip工具进行安装

sudo pip install supervisor

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

sudo apt-get install supervisor

配置

运行如下命令可以查看默认的配置

echo_supervisord_conf

如果需要修改默认的配置,可以运行下面命令生成一个默认的配置文件

echo_supervisord_conf > /etc/supervisord.conf

修改默认配置

默认配置文件中的supervisord.socksupervisord.log以及supervisord.pid是放在/tmp目录下,这个目录存放的是Linux中的临时文件,一旦被系统删除,就会提示unix:///tmp/supervisor.sock no such file,所以我们要把这三个文件放到其他目录中保存。

此处注意中文注释的几个需要修改的地方

[unix_http_server]
;此处修改为/var/run目录,避免被系统删除
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]
;此处是关键,否则执行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结尾
[include]
files = /etc/supervisord.d/conf.d/*.conf

上面的一些目录如果做了修改首先要创建这些目录,并且赋予相应的权限

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

现在我们启动服务

supervisord -c /etc/supervisord.conf

此处最好别用root权限启动,否则可能导致后面php artisan命令出错

现在我们查看进程

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应用的用户
numprocs=8  ;进程数
redirect_stderr=true  ;把 stderr 重定向到 stdout,默认 false
stdout_logfile=/var/log/supervisor/larashop-queue.log  ;注意分配好日志文件夹权限

配置完成后我们执行

supervisorctl reread  # 读取有更新(增加)的配置文件,不会启动新添加的程序

supervisorctl update # 重启配置文件修改过的程序

supervisorctl start larashop-worker:* # 启动 larashop-worker 程序

运行

supervisorctl status # 查看状态

看到

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队列已经开始正常运行了

如果在Laravel中修改了队列代码,需要重启Supervisor才能生效

可能出现的问题

1. ERROR (spawn error)

此时如果提示

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

先检查上述的日志目录有没有权限,如果权限正常,则进入上面设置的日志目录查看日志文件

我这里进入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 你的命令

执行artisan命令,同样爆出这个错误。所以原因在于启动supervisord和服务端的时候使用了root权限导致

先将原来的进程kill掉,使用非root权限重新执行服务端和客户端即可成功运行

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

在服务端配置文件中将/tmp/supervisor.sock修改为var/run/supervisor.sock

3. supervisorctl start错误

执行supervisorctl start 你的项目名称,迟迟不出结果,按ctrl+c后爆出错误

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

建议kill掉supervisord的进程,然后重新用pip安装一次,并重新配置一遍默认配置文件

链接

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容

  • 记得喵喵说过:如果别人的言语引发你的情绪,那是你自己的问题,而不是别人的问题。 今天有点更深的体会,有时候自尊心会...
    鱼儿飞飞_阅读 118评论 0 0
  • 愿舍去一个最能实现的心愿 只为来换取一次给你送一杯茶的机缘 送给那个永远拥有慈悲之心的怡红公子 在我最美好的年华 ...
    诗意_c3b1阅读 86评论 0 2