From dd78dbde5ca3ed63ee1d4e8113d8fa2929ead818 Mon Sep 17 00:00:00 2001 From: xboard Date: Thu, 23 May 2024 11:40:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20workerman=20=E5=A2=9E=E5=8A=A0=E7=83=AD?= =?UTF-8?q?=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- .../Controllers/V1/Admin/ConfigController.php | 5 +- docs/aapanel安装指南.md | 1 + webman.php | 61 ++++++++++++++++--- 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4e81d3b..3ce274a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM phpswoole/swoole:php8.1-alpine COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/ -RUN install-php-extensions pcntl bcmath \ +RUN install-php-extensions pcntl bcmath inotify \ && apk --no-cache add shadow supervisor nginx sqlite nginx-mod-http-brotli mysql-client git patch \ && addgroup -S -g 1000 www && adduser -S -G www -u 1000 www #复制项目文件以及配置文件 diff --git a/app/Http/Controllers/V1/Admin/ConfigController.php b/app/Http/Controllers/V1/Admin/ConfigController.php index 50cdbdf..ae5c95c 100755 --- a/app/Http/Controllers/V1/Admin/ConfigController.php +++ b/app/Http/Controllers/V1/Admin/ConfigController.php @@ -188,7 +188,10 @@ class ConfigController extends Controller ); } } - + // 如果是workerman环境,则触发reload + if(isset(get_defined_constants(true)['user']['Workerman'])){ + posix_kill(posix_getppid(), SIGUSR1); + } Cache::forget('admin_settings'); // \Artisan::call('horizon:terminate'); //重启队列使配置生效 return $this->success(true); diff --git a/docs/aapanel安装指南.md b/docs/aapanel安装指南.md index f408d32..028eb85 100644 --- a/docs/aapanel安装指南.md +++ b/docs/aapanel安装指南.md @@ -23,6 +23,7 @@ URL=https://www.aapanel.com/script/install_6.0_en.sh && if [ -f /usr/bin/curl ]; - swoole4 - readline - event +- inotify 4. 解除被禁止函数 > aaPanel 面板 > App Store > 找到PHP 8.1点击Setting > Disabled functions 将以下函数从列表中删除 diff --git a/webman.php b/webman.php index 3c3e362..0e6cfd4 100644 --- a/webman.php +++ b/webman.php @@ -4,26 +4,73 @@ require_once __DIR__ . '/vendor/autoload.php'; use Adapterman\Adapterman; use Workerman\Worker; +use \Workerman\Events\EventInterface; + +define('Workerman', true); Adapterman::init(); - $http_worker = new Worker('http://127.0.0.1:7010'); $http_worker->count = getenv('WEBMAN_WORKERS') ?: max(swoole_cpu_num(), 2); -$http_worker->name = 'AdapterMan'; - +$http_worker->name = 'Xboard'; $http_worker->onWorkerStart = static function () { - //init(); require __DIR__ . '/start.php'; }; - $http_worker->onMessage = static function ($connection, $request) { - static $request_count; - $connection->send(run()); if (++$request_count > 10000) { Worker::stopAll(); } }; +$worker = new Worker(); +$worker->name = 'FileMonitor'; +$worker->reloadable = false; +$monitor_dirs = ['app', 'bootstrap', 'config', 'resources', 'routes', 'public', '.env']; +$monitor_files = array(); + +// 进程启动后创建inotify监控句柄 +$worker->onWorkerStart = function ($worker) { + if (!extension_loaded('inotify')) { + echo "FileMonitor : Please install inotify extension.\n"; + return; + } + global $monitor_dirs, $monitor_files; + $worker->inotifyFd = inotify_init(); + stream_set_blocking($worker->inotifyFd, 0); + + foreach ($monitor_dirs as $monitor_dir) { + $monitor_realpath = realpath(__DIR__ . "/{$monitor_dir}"); + addInofity($monitor_realpath, $worker->inotifyFd); + if (is_file($monitor_realpath)) + continue; + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($monitor_realpath, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST); + foreach ($iterator as $file) { + if ($file->isDir()) { + $realpath = realpath($file); + addInofity($realpath, $worker->inotifyFd); + } + } + } + Worker::$globalEvent->add($worker->inotifyFd, EventInterface::EV_READ, 'check_files_change'); +}; +function addInofity(string $realpath, $fd) +{ + global $monitor_files; + $wd = inotify_add_watch($fd, $realpath, IN_MODIFY | IN_CREATE | IN_DELETE); + $monitor_files[$wd] = $realpath; + +} +function check_files_change($inotify_fd) +{ + global $monitor_files; + $events = inotify_read($inotify_fd); + if ($events) { + foreach ($events as $ev) { + $file = $monitor_files[$ev['wd']]; + echo $file . "/{$ev['name']} update and reload\n"; + } + posix_kill(posix_getppid(), SIGUSR1); + } +} Worker::runAll();