2021-03-29 15:34:24 +08:00
|
|
|
<?php /** @noinspection PhpUnusedParameterInspection */
|
|
|
|
|
|
|
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM\Event\SwooleEvent;
|
|
|
|
|
|
|
|
|
|
|
2021-07-09 01:38:30 +08:00
|
|
|
use Error;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Swoole\Event;
|
|
|
|
|
use Swoole\Process;
|
2021-03-29 15:34:24 +08:00
|
|
|
use Swoole\Server;
|
|
|
|
|
use ZM\Annotation\Swoole\SwooleHandler;
|
|
|
|
|
use ZM\Console\Console;
|
|
|
|
|
use ZM\Event\SwooleEvent;
|
2021-05-08 10:02:41 +08:00
|
|
|
use ZM\Framework;
|
2021-07-09 01:38:30 +08:00
|
|
|
use ZM\Store\ZMBuf;
|
|
|
|
|
use ZM\Utils\DataProvider;
|
2022-03-13 22:50:01 +08:00
|
|
|
use ZM\Utils\Manager\ProcessManager;
|
2021-06-16 00:17:30 +08:00
|
|
|
use ZM\Utils\SignalListener;
|
2021-07-09 01:38:30 +08:00
|
|
|
use ZM\Utils\Terminal;
|
|
|
|
|
use ZM\Utils\ZMUtil;
|
2021-03-29 15:34:24 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnManagerStart
|
|
|
|
|
* @package ZM\Event\SwooleEvent
|
|
|
|
|
* @SwooleHandler("ManagerStart")
|
|
|
|
|
*/
|
|
|
|
|
class OnManagerStart implements SwooleEvent
|
|
|
|
|
{
|
2022-03-13 22:50:01 +08:00
|
|
|
private static $last_hash = "";
|
|
|
|
|
private static $watch = -1;
|
2021-07-09 01:38:30 +08:00
|
|
|
|
2021-03-29 15:34:24 +08:00
|
|
|
public function onCall(Server $server) {
|
2021-07-09 01:38:30 +08:00
|
|
|
Console::debug("Calling onManagerStart event(1)");
|
2021-05-08 10:02:41 +08:00
|
|
|
if (!Framework::$argv["disable-safe-exit"]) {
|
2021-06-16 00:17:30 +08:00
|
|
|
SignalListener::signalManager();
|
2021-05-08 10:02:41 +08:00
|
|
|
}
|
2022-03-13 22:50:01 +08:00
|
|
|
Framework::saveProcessState(ZM_PROCESS_MANAGER, $server->manager_pid);
|
|
|
|
|
|
|
|
|
|
ProcessManager::createUserProcess('monitor', function() use ($server){
|
|
|
|
|
Process::signal(SIGINT, function() {
|
|
|
|
|
Console::success("用户进程检测到了Ctrl+C");
|
|
|
|
|
});
|
2021-07-09 01:38:30 +08:00
|
|
|
if (Framework::$argv["watch"]) {
|
|
|
|
|
if (extension_loaded('inotify')) {
|
|
|
|
|
Console::info("Enabled File watcher, framework will reload automatically.");
|
|
|
|
|
/** @noinspection PhpUndefinedFieldInspection */
|
|
|
|
|
Framework::$server->inotify = $fd = inotify_init();
|
|
|
|
|
$this->addWatcher(DataProvider::getSourceRootDir() . "/src", $fd);
|
|
|
|
|
Event::add($fd, function () use ($fd) {
|
|
|
|
|
$r = inotify_read($fd);
|
|
|
|
|
Console::verbose("File updated: " . $r[0]["name"]);
|
|
|
|
|
ZMUtil::reload();
|
|
|
|
|
});
|
2022-03-13 22:50:01 +08:00
|
|
|
Framework::$argv["polling-watch"] = false; // 如果开启了inotify则关闭轮询热更新
|
2021-07-09 01:38:30 +08:00
|
|
|
} else {
|
2022-03-13 22:50:01 +08:00
|
|
|
Console::warning(zm_internal_errcode("E00024") . "你还没有安装或启用 inotify 扩展,将默认使用轮询检测模式开启热更新!");
|
|
|
|
|
Framework::$argv["polling-watch"] = true;
|
2021-07-09 01:38:30 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-13 22:50:01 +08:00
|
|
|
if (Framework::$argv["polling-watch"]) {
|
|
|
|
|
self::$watch = swoole_timer_tick(3000, function () use ($server) {
|
|
|
|
|
$data = (DataProvider::scanDirFiles(DataProvider::getSourceRootDir() . '/src/'));
|
|
|
|
|
$hash = md5("");
|
|
|
|
|
foreach ($data as $file) {
|
|
|
|
|
$hash = md5($hash . md5_file($file));
|
|
|
|
|
}
|
|
|
|
|
if (self::$last_hash == "") {
|
|
|
|
|
self::$last_hash = $hash;
|
|
|
|
|
} elseif (self::$last_hash !== $hash) {
|
|
|
|
|
self::$last_hash = $hash;
|
|
|
|
|
$server->reload();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-07-09 01:38:30 +08:00
|
|
|
if (Framework::$argv["interact"]) {
|
|
|
|
|
Console::info("Interact mode");
|
|
|
|
|
ZMBuf::$terminal = $r = STDIN;
|
|
|
|
|
Event::add($r, function () use ($r) {
|
|
|
|
|
$fget = fgets($r);
|
|
|
|
|
if ($fget === false) {
|
|
|
|
|
Event::del($r);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$var = trim($fget);
|
|
|
|
|
if ($var == "stop") Event::del($r);
|
|
|
|
|
try {
|
|
|
|
|
Terminal::executeCommand($var);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Console::error(zm_internal_errcode("E00025") . "Uncaught exception " . get_class($e) . ": " . $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")");
|
|
|
|
|
} catch (Error $e) {
|
|
|
|
|
Console::error(zm_internal_errcode("E00025") . "Uncaught error " . get_class($e) . ": " . $e->getMessage() . " at " . $e->getFile() . "(" . $e->getLine() . ")");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-03-13 22:50:01 +08:00
|
|
|
|
|
|
|
|
ProcessManager::getUserProcess('monitor')->set(['enable_coroutine' => true]);
|
|
|
|
|
ProcessManager::getUserProcess('monitor')->start();
|
|
|
|
|
|
|
|
|
|
/*$dispatcher = new EventDispatcher(OnManagerStartEvent::class);
|
|
|
|
|
$dispatcher->setRuleFunction(function($v) {
|
|
|
|
|
return eval("return " . $v->getRule() . ";");
|
|
|
|
|
});
|
|
|
|
|
$dispatcher->dispatchEvents($server);
|
|
|
|
|
*/
|
2021-03-29 15:34:24 +08:00
|
|
|
Console::verbose("进程 Manager 已启动");
|
|
|
|
|
}
|
2021-07-09 01:38:30 +08:00
|
|
|
|
|
|
|
|
private function addWatcher($maindir, $fd) {
|
|
|
|
|
$dir = scandir($maindir);
|
|
|
|
|
if ($dir[0] == ".") {
|
|
|
|
|
unset($dir[0], $dir[1]);
|
|
|
|
|
}
|
|
|
|
|
foreach ($dir as $subdir) {
|
|
|
|
|
if (is_dir($maindir . "/" . $subdir)) {
|
|
|
|
|
Console::debug("添加监听目录:" . $maindir . "/" . $subdir);
|
|
|
|
|
inotify_add_watch($fd, $maindir . "/" . $subdir, IN_ATTRIB | IN_ISDIR);
|
|
|
|
|
$this->addWatcher($maindir . "/" . $subdir, $fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-29 15:34:24 +08:00
|
|
|
}
|