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

作者 : 开心源码 本文共2466个字,预计阅读时间需要7分钟 发布时间: 2022-05-13 共279人阅读

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)

  • ext-swoole >= v4.4
  • ext-inotify

下载 (Download)

  • mix-php/swoolefor

使用 (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

常用的信号表

信号
SIGTERM15
SIGKILL9
SIGHUP1

支持流行的 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. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » SwooleFor: 监控文件变化自动重启 Swoole 服务

发表回复