2020-03-02 16:14:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM\Utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Co;
|
2020-09-29 15:07:43 +08:00
|
|
|
use Exception;
|
|
|
|
|
use Swoole\Event;
|
|
|
|
|
use Swoole\Timer;
|
2020-08-31 10:11:06 +08:00
|
|
|
use ZM\Console\Console;
|
2020-09-29 15:07:43 +08:00
|
|
|
use ZM\Store\LightCache;
|
2020-11-03 21:02:24 +08:00
|
|
|
use ZM\Store\LightCacheInside;
|
|
|
|
|
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
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
public static function stop() {
|
|
|
|
|
Console::warning(Console::setColor("Stopping server...", "red"));
|
2020-11-03 21:02:24 +08:00
|
|
|
LightCache::savePersistence();
|
2020-09-29 15:07:43 +08:00
|
|
|
if (ZMBuf::$terminal !== null)
|
|
|
|
|
Event::del(ZMBuf::$terminal);
|
2020-11-03 21:02:24 +08:00
|
|
|
ZMAtomic::get("stop_signal")->set(1);
|
2020-09-29 15:07:43 +08:00
|
|
|
try {
|
|
|
|
|
LightCache::set('stop', 'OK');
|
|
|
|
|
} catch (Exception $e) {
|
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
|
|
|
}
|
|
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
public static function reload($delay = 800) {
|
2020-03-02 16:14:20 +08:00
|
|
|
Console::info(Console::setColor("Reloading server...", "gold"));
|
2020-09-29 15:07:43 +08:00
|
|
|
usleep($delay * 1000);
|
2020-11-03 21:02:24 +08:00
|
|
|
foreach ((LightCacheInside::get("wait_api", "wait_api") ?? []) as $k => $v) {
|
|
|
|
|
if ($v["result"] === null && isset($v["coroutine"])) Co::resume($v["coroutine"]);
|
2020-05-06 17:19:59 +08:00
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
foreach (server()->connections as $v) {
|
|
|
|
|
server()->close($v);
|
2020-03-02 16:14:20 +08:00
|
|
|
}
|
2020-10-03 23:00:18 +08:00
|
|
|
//DataProvider::saveBuffer();
|
2020-09-29 15:07:43 +08:00
|
|
|
Timer::clearAll();
|
|
|
|
|
server()->reload();
|
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];
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-03 21:02:24 +08:00
|
|
|
|
|
|
|
|
public static function sendActionToWorker($target_id, $action, $data) {
|
|
|
|
|
server()->sendMessage(json_encode(["action" => $action, "data" => $data]), $target_id);
|
|
|
|
|
}
|
2020-04-29 15:29:56 +08:00
|
|
|
}
|