Files
zhamao-framework/src/ZM/Utils/ZMUtil.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
namespace ZM\Utils;
2020-09-29 15:07:43 +08:00
use Exception;
use Swoole\Process;
2020-08-31 10:11:06 +08:00
use ZM\Console\Console;
use ZM\Framework;
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
{
/**
* @throws Exception
*/
2020-09-29 15:07:43 +08:00
public static function stop() {
if (SpinLock::tryLock("_stop_signal") === false) return;
2020-09-29 15:07:43 +08:00
Console::warning(Console::setColor("Stopping server...", "red"));
if (Console::getLevel() >= 4) Console::trace();
2020-11-03 21:02:24 +08:00
ZMAtomic::get("stop_signal")->set(1);
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
}
/**
* @throws Exception
*/
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
}
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();
} else {
return ZMBuf::$instance[$class];
}
}
public static function sendActionToWorker($target_id, $action, $data) {
Console::verbose($action . ": " . $data);
server()->sendMessage(json_encode(["action" => $action, "data" => $data]), $target_id);
}
/**
* 在工作进程中返回可以通过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
)
);
}
}