|
4 | 4 |
|
5 | 5 | use Adapterman\Adapterman; |
6 | 6 | use Workerman\Worker; |
| 7 | +use \Workerman\Events\EventInterface; |
7 | 8 |
|
8 | | -Adapterman::init(); |
| 9 | +define('Workerman', true); |
9 | 10 |
|
| 11 | +Adapterman::init(); |
10 | 12 | $http_worker = new Worker('http://127.0.0.1:7010'); |
11 | 13 | $http_worker->count = getenv('WEBMAN_WORKERS') ?: max(swoole_cpu_num(), 2); |
12 | | -$http_worker->name = 'AdapterMan'; |
13 | | - |
| 14 | +$http_worker->name = 'Xboard'; |
14 | 15 | $http_worker->onWorkerStart = static function () { |
15 | | - //init(); |
16 | 16 | require __DIR__ . '/start.php'; |
17 | 17 | }; |
18 | | - |
19 | 18 | $http_worker->onMessage = static function ($connection, $request) { |
20 | | - |
21 | 19 | static $request_count; |
22 | | - |
23 | 20 | $connection->send(run()); |
24 | 21 | if (++$request_count > 10000) { |
25 | 22 | Worker::stopAll(); |
26 | 23 | } |
27 | 24 | }; |
28 | 25 |
|
| 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 | +} |
29 | 76 | Worker::runAll(); |
0 commit comments