2020-09-29 15:07:43 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
declare(strict_types=1);
|
2020-09-29 15:07:43 +08:00
|
|
|
|
|
|
|
|
namespace ZM\Event;
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
use Closure;
|
2020-09-29 15:07:43 +08:00
|
|
|
use Error;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Swoole\Timer;
|
|
|
|
|
use ZM\Annotation\AnnotationBase;
|
|
|
|
|
use ZM\Annotation\AnnotationParser;
|
|
|
|
|
use ZM\Annotation\Swoole\OnTick;
|
2021-02-15 15:15:26 +08:00
|
|
|
use ZM\Config\ZMConfig;
|
2020-09-29 15:07:43 +08:00
|
|
|
use ZM\Console\Console;
|
2021-11-16 15:41:01 +08:00
|
|
|
use ZM\Exception\AnnotationException;
|
2021-02-15 15:15:26 +08:00
|
|
|
use ZM\Store\LightCache;
|
2020-11-03 21:02:24 +08:00
|
|
|
use ZM\Store\ZMAtomic;
|
2020-09-29 15:07:43 +08:00
|
|
|
|
|
|
|
|
class EventManager
|
|
|
|
|
{
|
|
|
|
|
public static $events = [];
|
2022-03-15 18:05:33 +08:00
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
public static $middleware_map = [];
|
2022-03-15 18:05:33 +08:00
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
public static $middlewares = [];
|
2022-03-15 18:05:33 +08:00
|
|
|
|
2020-09-29 15:07:43 +08:00
|
|
|
public static $req_mapping = [];
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
public static function addEvent($event_name, ?AnnotationBase $event_obj)
|
|
|
|
|
{
|
|
|
|
|
if ($event_obj->method instanceof Closure) {
|
|
|
|
|
Console::debug("Adding event {$event_name} at @Anonymous");
|
2021-11-02 16:01:24 +08:00
|
|
|
} else {
|
2022-03-15 18:05:33 +08:00
|
|
|
Console::debug("Adding event {$event_name} at " . ($event_obj->class) . ':' . ($event_obj->method));
|
2021-11-02 16:01:24 +08:00
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
self::$events[$event_name][] = $event_obj;
|
2021-01-02 13:15:50 +08:00
|
|
|
(new AnnotationParser())->sortByLevel(self::$events, $event_name);
|
2020-09-29 15:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-16 15:41:01 +08:00
|
|
|
/**
|
|
|
|
|
* @throws AnnotationException
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
public static function loadEventByParser(AnnotationParser $parser)
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
self::$events = $parser->generateAnnotationEvents();
|
|
|
|
|
self::$middlewares = $parser->getMiddlewares();
|
|
|
|
|
self::$middleware_map = $parser->getMiddlewareMap();
|
|
|
|
|
self::$req_mapping = $parser->getReqMapping();
|
2021-09-01 14:14:00 +08:00
|
|
|
$parser->verifyMiddlewares();
|
2020-09-29 15:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注册所有计时器给每个进程
|
2021-03-18 14:56:35 +08:00
|
|
|
* @throws Exception
|
2020-09-29 15:07:43 +08:00
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
public static function registerTimerTick()
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
$dispatcher = new EventDispatcher(OnTick::class);
|
|
|
|
|
foreach (self::$events[OnTick::class] ?? [] as $vss) {
|
2022-03-15 18:05:33 +08:00
|
|
|
if (server()->worker_id !== $vss->worker_id && $vss->worker_id != -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
//echo server()->worker_id.PHP_EOL;
|
|
|
|
|
$plain_class = $vss->class;
|
2022-03-15 18:05:33 +08:00
|
|
|
Console::debug('Added Middleware-based timer: ' . $plain_class . ' -> ' . $vss->method);
|
2020-09-29 15:07:43 +08:00
|
|
|
Timer::tick($vss->tick_ms, function () use ($vss, $dispatcher) {
|
|
|
|
|
set_coroutine_params([]);
|
2022-03-15 18:05:33 +08:00
|
|
|
if (ZMAtomic::get('stop_signal')->get() != 0) {
|
2020-09-29 15:07:43 +08:00
|
|
|
Timer::clearAll();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$dispatcher->dispatchEvent($vss, null);
|
|
|
|
|
} catch (Exception $e) {
|
2022-03-15 18:05:33 +08:00
|
|
|
Console::error(zm_internal_errcode('E00034') . 'Uncaught error from TimerTick: ' . $e->getMessage() . ' at ' . $e->getFile() . "({$e->getLine()})");
|
2020-09-29 15:07:43 +08:00
|
|
|
} catch (Error $e) {
|
2022-03-15 18:05:33 +08:00
|
|
|
Console::error(zm_internal_errcode('E00034') . 'Uncaught fatal error from TimerTick: ' . $e->getMessage());
|
|
|
|
|
echo Console::setColor($e->getTraceAsString(), 'gray');
|
|
|
|
|
Console::error('Please check your code!');
|
2020-09-29 15:07:43 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
$conf = ZMConfig::get('global', 'worker_cache') ?? ['worker' => 0];
|
|
|
|
|
if (server()->worker_id == $conf['worker']) {
|
|
|
|
|
zm_timer_tick(ZMConfig::get('global', 'light_cache')['auto_save_interval'] * 1000, function () {
|
2021-02-15 15:15:26 +08:00
|
|
|
LightCache::savePersistence();
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
}
|
|
|
|
|
}
|