SWOOLE的热更新实现

使用swoole_http_server替代php-fpm后,由于php长驻内存,修改了代码不能实时调试,需要去手动去重启服务,很是不方便,参考了持续的思考的文章swoole之代码热更新实现之后,决定使用inotify来监控文件状态的改变,来给swoole发送reload信号,来实现swoole的热更新。

如何安装inotify就不写了,安装之后可以建立一个脚本文件,如php_reload.sh:

#!/bin/sh
# src 需要监控的地址
src=/home/server/Project/test/app/
/usr/bin/inotifywait -rmq -e create,modify,delete $src | while read event
  do
        /home/server/Project/test/bin/httpserver reload
  done

配合上一篇的脚本使用非常方便,现在可以实时的修改脚本并查看结果了。

 

linux shell 写swoole重启脚本

linux shell 写swoole重启脚本 代码如下

#!/bin/sh
kill `lsof -t -i:9501`
sleep 2
php /data/web/mircoweb/wwwroot/Public/swoole.php
sleep 1
netstat -ntlp

如果不支持lsof命令 那就yum install lsof安装下吧

 

 

swoole服务平滑重启

1. reload.sh脚本

echo "loading..."
pid="pidof live_name"
echo $pid
kill -USR1 $pid
echo "loading success"

2. linux中执行

sh  reload.sh
代码用的原来只是自己加了一些操作流程

 swoole_reload_server.php

<?php
class Server
{
        private $serv;
        public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            'worker_num' => 8,
            'daemonize' => false,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode'=> 1,
        ));
        $this->serv->on('Start', array($this, 'onStart'));
        $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));
        $this->serv->start();
    }
    public function onStart( $serv ) {
        echo "Start\n";
        cli_set_process_title("reload_master");
    }
    public function onWorkerStart( $serv , $worker_id) {
        require_once "reload_page.php";
        Test();
    }
    public function onConnect( $serv, $fd, $from_id ) {
        echo "Client {$fd} connect\n";
 
    }
    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
        echo "Get Message From Client {$fd}:{$data}\n";
    }
    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }
}
new Server();

reload.sh

echo "Reloading..."
cmd=$(pidof reload_master)
 
kill -USR1 "$cmd"
echo "Reloaded"


reload_page.php 

<?php
/**
 * If you change this function and want
 * swoole_server to use the new function,
 * just run 'reload.sh' to send a restart
 * signal to swoole_server.
 */
function Test() {
        echo "This is not a php file\n";


 

转载于:https://my.oschina.net/mickelfeng/blog/1549500

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值