V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
onanying
V2EX  ›  PHP

SwooleFor: 监控文件变化自动重启 Swoole 服务

  •  1
     
  •   onanying · 2019-08-14 13:41:35 +08:00 · 3039 次点击
    这是一个创建于 1716 天前的主题,其中的信息可能已经有所发展或是发生改变。

    SwooleFor

    监控你的 Swoole 程序文件变化并自动重启服务器 - 适用于开发

    Monitor for any changes in your swoole application and automatically restart the server - perfect for development

    SwooleFor 的定位就如同 PHP 版本的 nodemon, node-dev

    该项目使用 mix-phar 开发

    依赖扩展 (Depend extensions)

    下载 (Download)

    使用 (Usage)

    执行脚本命令:

    php swoolefor.phar --exec="php app.php arg..."
    

    如果 disable_functions 禁用了 proc_open 方法,按如下方法执行:

    php -d disable_functions='' swoolefor.phar --exec="php app.php arg..."
    

    启动成功:

       _____                     __     ______          
      / ___/      ______  ____  / /__  / ____/___  _____
      \__ \ | /| / / __ \/ __ \/ / _ \/ /_  / __ \/ ___/
     ___/ / |/ |/ / /_/ / /_/ / /  __/ __/ / /_/ / /    
    /____/|__/|__/\____/\____/_/\___/_/    \____/_/  Version: 1.1.1, Swoole: 4.4.0
    
    [info] 2019-08-14 11:51:05.937 <920> [message] executor start, exec: [php /data/bin/mix-httpd start]
    [info] 2019-08-14 11:51:05.938 <920> [message] fork sub process, pid: 921
    [info] 2019-08-14 11:51:05.939 <920> [message] monitor start
    [info] 2019-08-14 11:51:05.939 <920> [message] watch: /data
    [info] 2019-08-14 11:51:05.939 <920> [message] delay: 3s
    [info] 2019-08-14 11:51:05.939 <920> [message] ext: .php,.json
    

    全部命令参数

    php swoolefor.phar --help
    
    • -e, --exec Swoole application or other script start command
    • -d, --daemon Run in the background
    • --watch Watch code file directory
    • --delay File change delay processing (seconds)
    • --ext Monitor only changes to these extensions
    • --signal Send this signal to the process

    执行脚本命令

    --exec 内部可以是任何命令,必须为绝对路径,必须为前台执行的常驻程序 (否则会导致不断 fork 进程)

    php swoolefor.phar --exec="php app.php"
    

    也可使用短参数

    php swoolefor.phar -e "php app.php"
    

    执行非 PHP 脚本

    • node
    php swoolefor.phar --exec="node app.js"
    
    • python
    php swoolefor.phar --exec="python app.py"
    

    在后台执行

    SwooleFor 本身可以在后台执行,这样可以脱离终端,增加 --daemon 即可

    php swoolefor.phar --exec="node app.js" --daemon
    

    也可使用短参数

    php swoolefor.phar --exec="node app.js" -d
    

    指定监控目录

    --watch 的默认值为 --exec 参数中脚本的当前目录,如果脚本是在 bin 目录中则会监控上一级的目录。

    // 会自动监控 /data 目录
    php swoolefor.phar --exec="php /data/bin/app.php"
    

    指定监控其他目录

    php swoolefor.phar --exec="php app.php" --watch=/tmp
    

    推迟执行重启

    当更新了很多文件时,我们并不希望程序一直频繁的重启,所以我们需要设置一个延迟执行重启的时间,只有在达到设置的时间才执行重启操作。

    --delay 默认为 3s

    php swoolefor.phar --exec="php app.php" --delay=5
    

    指定观察的扩展名

    --ext 默认为 php,json,当需要观察其他扩展名时可配置

    php swoolefor.phar --exec="php app.php" --ext=php,json,ini
    

    重启时发送的信号

    程序重启时终止进程是通过给进程发送信号完成的,当我们需要指定信号时

    --signal 默认为 15

    php swoolefor.phar --exec="php app.php" --signal=1
    

    常用的信号表

    | 信号 | 值 |
    | --- | --- |
    | SIGTERM | 15 |
    | SIGKILL | 9 |
    | SIGHUP | 1 |
    | ... | ... |

    支持流行的 Swoole 框架

    • MixPHP:
    php swoolefor.phar -e "php /data/bin/mix-httpd start -c /data/applications/http/config/httpd.php"
    
    • Swoft:
    php swoolefor.phar -e "php /data/bin/swoft http:start"
    
    • EasySwoole:
    php swoolefor.phar -e "php /data/bin/easyswoole start"
    
    • laravel-s
    php swoolefor.phar -e "php /data/bin/laravels start"
    

    License

    Apache License Version 2.0, http://www.apache.org/licenses/

    1 条回复    2019-08-14 13:47:25 +08:00
    mcfog
        1
    mcfog  
       2019-08-14 13:47:25 +08:00   ❤️ 1
    推荐 https://github.com/cortesi/modd

    # modd.conf
    **/*.php {
    prep: composer run test
    daemon: php bin/server.php
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5370 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:55 · PVG 16:55 · LAX 01:55 · JFK 04:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.