说明
- 本篇只记录怎么将lumen事件默认同步,改为异步执行。
- lumen默认事件是同步(sync),具体事件监听怎么使用这里不做说明。
- 默认你已经可以正常触发同步事件。
- 默认你已经配置好Redis。
- 参考:lumen - 队列
操作步骤
QUEUE_DRIVER=redis
- 新建
TestEvent
和TestListener
并在事件服务EventServiceProvider
注册事件监听器
TestEvent::class => [
TestListener::class,
],
class TestListener implements ShouldQueue
{
/**
* @param TestEvent $event
*/
public function handle(TestEvent $event)
{
// 业务逻辑
}
}
php artisan queue:work --sleep=3 --tries=3
// 安装
$ yum install supervisor
// 开机启动
$ systemctl start supervisord.service
$ systemctl enable supervisord.service
// 新建子进程配置文件
$ cd /etc/supervisord.d/
$ vim test-queue.ini
[program:test-queue]
directory=/www/test.com
command=php artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=false
stderr_logfile=/www/logs/supervisor/test-queue-stderr.log
stdout_logfile=/www/logs/supervisor/test-queue-stdout.log
#user = test
// 查看状态、更新配置、启动、停止
$ supervisorctl status
$ supervisorctl update
$ supervisorctl start all
$ supervisorctl stop all
- 配置守护进程Supervisor(CentOS6.*),注意:全程使用同一个用户操作,此处使用非root用户
// 安装
$ sudo easy_install supervisor==3.1.3
// 配置
$ sudo mkdir /etc/supervisor
$ sudo echo_supervisord_conf > /etc/supervisor/supervisord.conf
$ sudo vi /etc/supervisor/supervisord.conf
// 修改如下内容
[include]
files = /etc/supervisor/config.d/*.ini
$ sudo mkdir /etc/supervisor/config.d
$ sudo vim /etc/supervisor/config.d/test-queue.ini
[program:test-queue]
directory=/www/test.com
command=php artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=false
stderr_logfile=/www/logs/supervisor/test-queue-stderr.log
stdout_logfile=/www/logs/supervisor/test-queue-stdout.log
#user = test
$ sudo mkdir /data/logs/supervisor
$ sudo chown -R www:www /data/logs/supervisor
// 启动
$ sudo supervisord -c /etc/supervisor/supervisord.conf
// 如果报错:Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
// ps 查看一下进程,然后kill掉之,再重新启动
$ ps aux |grep supervisor
$ kill -9 888888
// 开机自启动
$ sudo vi /etc/rc.d/init.d/supervisor
#!/bin/bash
# chkconfig: - 95 04
# processname: supervisord
# config: /etc/supervisor/supervisord.conf
# pidfile: /tmp/supervisord.pid
#
# source function library
. /etc/rc.d/init.d/functions
PIDFILE=/tmp/supervisord.pid
RETVAL=0
start() {
echo -n $"Starting supervisord: "
daemon "supervisord --pidfile=$PIDFILE -c /etc/supervisor/supervisord.conf"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}
stop() {
echo -n $"Stopping supervisord: "
killproc supervisord
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/supervisord ] && restart
;;
status)
status supervisord
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
$ sudo chmod 755 /etc/rc.d/init.d/supervisor
$ sudo chkconfig --add supervisor
$ sudo chkconfig supervisor on
$ sudo service supervisor start|stop|restart|status
// 查看状态、更新配置、启动、停止
$ sudo supervisorctl status
$ sudo supervisorctl update
$ sudo supervisorctl start all
$ sudo supervisorctl stop all
// 如果上述命令报错:“Address family not supported by protocol...”,则手动指定配置文件,比如:
$ sudo supervisorctl -c /etc/supervisor/supervisord.conf status