-
php教程之PHP7 生产环境队列 Beanstalkd 正确使用姿势(3)
监控 beanstalkd 状态
1
2
3
4
5
6
7
|
<?php //监控服务状态 require_once( './vendor/autoload.php' ); use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk( '127.0.0.1' ,11300); $isAlive = $pheanstalk->getConnection()->isServiceListening(); var_dump( $isAlive ); |
可以配合 email 做一个报警邮件,脚本每分钟去执行,判断状态是 false,就给管理员发送邮件报警。
一些相关命令
查看 beanstalkd 服务内存占用
1
|
top -u beanstalkd |
后台运行 consumer 脚本
1
|
nohup php googlehome_subscribe.php & |
查看 consumer 脚本运行时间
1
|
ps -A -opid,stime,etime,args | grep consumer.php |
手工重启 consumer 脚本
1
2
|
ps auxf|grep 'googlehome_subscribe.php' |grep -v grep|awk '{print $2}' |xargs kill -9 nohup php googlehome_subscribe.php & |
一些总结
php 要把错误日志打开,方便收集 consumer 脚本 crash 的 log,脚本跑出一些致命的 error 一定要及时修复,因为一旦有错就会挂掉,这会影响你脚本的可用性,后期稳定之后可以上 supervisor 这种进程管理程序来管控脚本生命周期。
一些网络请求操作,一定要 try catch 到所有错误,一旦没有 catch 到,脚本就崩。我用的是 Guzzle 去做的网络请求,下面是我 catch 的一些错误,代码片段供参考。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
try { /* TODO: 逻辑操作 */ } catch ( ClientException $e ) { $results[ 'mid' ] = $ this ->mid; $results[ 'code' ] = $e->getResponse()->getStatusCode(); $results[ 'reason' ] = $e->getResponse()->getReasonPhrase(); $ this ->log->error( 'properties-changed ClientException' , $results ); } catch ( ServerException $e ) { $results[ 'mid' ] = $ this ->mid; $results[ 'code' ] = $e->getResponse()->getStatusCode(); $results[ 'reason' ] = $e->getResponse()->getReasonPhrase(); $ this ->log->error( 'properties-changed ServerException' , $results ); } catch ( ConnectException $e ) { $results[ 'mid' ] = $ this ->mid; $ this ->log->error( 'properties-changed ConnectException' , $results ); } |