Skip to content

Commit dd78dbd

Browse files
author
xboard
committed
feat: workerman 增加热重载
1 parent 1cfd077 commit dd78dbd

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

‎Dockerfile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM phpswoole/swoole:php8.1-alpine
22

33
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
44

5-
RUN install-php-extensions pcntl bcmath \
5+
RUN install-php-extensions pcntl bcmath inotify \
66
&& apk --no-cache add shadow supervisor nginx sqlite nginx-mod-http-brotli mysql-client git patch \
77
&& addgroup -S -g 1000 www && adduser -S -G www -u 1000 www
88
#复制项目文件以及配置文件

‎app/Http/Controllers/V1/Admin/ConfigController.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ public function save(ConfigSave $request)
188188
);
189189
}
190190
}
191-
191+
// 如果是workerman环境,则触发reload
192+
if(isset(get_defined_constants(true)['user']['Workerman'])){
193+
posix_kill(posix_getppid(), SIGUSR1);
194+
}
192195
Cache::forget('admin_settings');
193196
// \Artisan::call('horizon:terminate'); //重启队列使配置生效
194197
return $this->success(true);

‎docs/aapanel安装指南.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ URL=https://www.aapanel.com/script/install_6.0_en.sh && if [ -f /usr/bin/curl ];
2323
- swoole4
2424
- readline
2525
- event
26+
- inotify
2627

2728
4. 解除被禁止函数
2829
> aaPanel 面板 > App Store > 找到PHP 8.1点击Setting > Disabled functions 将以下函数从列表中删除

‎webman.php‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,73 @@
44

55
use Adapterman\Adapterman;
66
use Workerman\Worker;
7+
use \Workerman\Events\EventInterface;
78

8-
Adapterman::init();
9+
define('Workerman', true);
910

11+
Adapterman::init();
1012
$http_worker = new Worker('http://127.0.0.1:7010');
1113
$http_worker->count = getenv('WEBMAN_WORKERS') ?: max(swoole_cpu_num(), 2);
12-
$http_worker->name = 'AdapterMan';
13-
14+
$http_worker->name = 'Xboard';
1415
$http_worker->onWorkerStart = static function () {
15-
//init();
1616
require __DIR__ . '/start.php';
1717
};
18-
1918
$http_worker->onMessage = static function ($connection, $request) {
20-
2119
static $request_count;
22-
2320
$connection->send(run());
2421
if (++$request_count > 10000) {
2522
Worker::stopAll();
2623
}
2724
};
2825

26+
$worker = new Worker();
27+
$worker->name = 'FileMonitor';
28+
$worker->reloadable = false;
29+
$monitor_dirs = ['app', 'bootstrap', 'config', 'resources', 'routes', 'public', '.env'];
30+
$monitor_files = array();
31+
32+
// 进程启动后创建inotify监控句柄
33+
$worker->onWorkerStart = function ($worker) {
34+
if (!extension_loaded('inotify')) {
35+
echo "FileMonitor : Please install inotify extension.\n";
36+
return;
37+
}
38+
global $monitor_dirs, $monitor_files;
39+
$worker->inotifyFd = inotify_init();
40+
stream_set_blocking($worker->inotifyFd, 0);
41+
42+
foreach ($monitor_dirs as $monitor_dir) {
43+
$monitor_realpath = realpath(__DIR__ . "/{$monitor_dir}");
44+
addInofity($monitor_realpath, $worker->inotifyFd);
45+
if (is_file($monitor_realpath))
46+
continue;
47+
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($monitor_realpath, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
48+
foreach ($iterator as $file) {
49+
if ($file->isDir()) {
50+
$realpath = realpath($file);
51+
addInofity($realpath, $worker->inotifyFd);
52+
}
53+
}
54+
}
55+
Worker::$globalEvent->add($worker->inotifyFd, EventInterface::EV_READ, 'check_files_change');
56+
};
57+
function addInofity(string $realpath, $fd)
58+
{
59+
global $monitor_files;
60+
$wd = inotify_add_watch($fd, $realpath, IN_MODIFY | IN_CREATE | IN_DELETE);
61+
$monitor_files[$wd] = $realpath;
62+
63+
}
64+
function check_files_change($inotify_fd)
65+
{
66+
global $monitor_files;
67+
$events = inotify_read($inotify_fd);
68+
if ($events) {
69+
foreach ($events as $ev) {
70+
$file = $monitor_files[$ev['wd']];
71+
echo $file . "/{$ev['name']} update and reload\n";
72+
}
73+
posix_kill(posix_getppid(), SIGUSR1);
74+
}
75+
}
2976
Worker::runAll();

0 commit comments

Comments
 (0)