2020-03-02 16:14:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM\Utils;
|
|
|
|
|
|
|
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
use Exception;
|
2021-03-24 23:34:46 +08:00
|
|
|
use Swoole\Process;
|
2020-08-31 10:11:06 +08:00
|
|
|
use ZM\Console\Console;
|
2021-03-24 23:34:46 +08:00
|
|
|
use ZM\Framework;
|
2021-03-06 17:22:42 +08:00
|
|
|
use ZM\Store\Lock\SpinLock;
|
2020-11-03 21:02:24 +08:00
|
|
|
use ZM\Store\ZMAtomic;
|
2020-08-31 10:11:06 +08:00
|
|
|
use ZM\Store\ZMBuf;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
class ZMUtil
|
|
|
|
|
{
|
2021-03-18 14:56:35 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-09-29 15:07:43 +08:00
|
|
|
public static function stop() {
|
2021-03-06 17:22:42 +08:00
|
|
|
if (SpinLock::tryLock("_stop_signal") === false) return;
|
2020-09-29 15:07:43 +08:00
|
|
|
Console::warning(Console::setColor("Stopping server...", "red"));
|
2021-03-08 00:48:51 +08:00
|
|
|
if (Console::getLevel() >= 4) Console::trace();
|
2020-11-03 21:02:24 +08:00
|
|
|
ZMAtomic::get("stop_signal")->set(1);
|
2021-03-24 23:34:46 +08:00
|
|
|
for ($i = 0; $i < ZM_WORKER_NUM; ++$i) {
|
|
|
|
|
if (Process::kill(zm_atomic("_#worker_" . $i)->get(), 0))
|
|
|
|
|
Process::kill(zm_atomic("_#worker_" . $i)->get(), SIGUSR1);
|
2020-03-02 16:14:20 +08:00
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
server()->shutdown();
|
|
|
|
|
server()->stop();
|
2020-03-02 16:14:20 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 14:56:35 +08:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-03-24 23:34:46 +08:00
|
|
|
public static function reload() {
|
|
|
|
|
zm_atomic("_int_is_reload")->set(1);
|
|
|
|
|
system("kill -INT " . intval(server()->master_pid));
|
2020-03-02 16:14:20 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
public static function getModInstance($class) {
|
|
|
|
|
if (!isset(ZMBuf::$instance[$class])) {
|
2020-09-29 15:07:43 +08:00
|
|
|
//Console::debug("Class instance $class not exist, so I created it.");
|
2020-08-31 10:11:06 +08:00
|
|
|
return ZMBuf::$instance[$class] = new $class();
|
2020-04-29 15:29:56 +08:00
|
|
|
} else {
|
|
|
|
|
return ZMBuf::$instance[$class];
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-29 22:27:10 +08:00
|
|
|
|
|
|
|
|
public static function sendActionToWorker($target_id, $action, $data) {
|
2021-03-24 23:34:46 +08:00
|
|
|
Console::verbose($action . ": " . $data);
|
2021-01-29 22:27:10 +08:00
|
|
|
server()->sendMessage(json_encode(["action" => $action, "data" => $data]), $target_id);
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在工作进程中返回可以通过reload重新加载的php文件列表
|
|
|
|
|
* @return string[]|string[][]
|
|
|
|
|
*/
|
|
|
|
|
public static function getReloadableFiles() {
|
|
|
|
|
return array_map(
|
|
|
|
|
function ($x) {
|
|
|
|
|
return str_replace(DataProvider::getWorkingDir() . "/", "", $x);
|
|
|
|
|
}, array_diff(
|
|
|
|
|
get_included_files(),
|
|
|
|
|
Framework::$loaded_files
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-04-29 15:29:56 +08:00
|
|
|
}
|