This commit is contained in:
as2252258@163.com
2021-04-16 00:22:26 +08:00
parent e9243de9c1
commit de6e01b058
4 changed files with 273 additions and 275 deletions
+53 -55
View File
@@ -18,70 +18,68 @@ use Swoole\Server;
class OnPipeMessage extends Callback
{
/**
* @param Server $server
* @param int $src_worker_id
* @param $swollen_universalize
* @throws Exception
*/
public function onHandler(Server $server, int $src_worker_id, $swollen_universalize)
{
go(function () use ($server, $src_worker_id, $swollen_universalize) {
match ($swollen_universalize['action'] ?? null) {
'kafka' => $this->onKafkaWorker($swollen_universalize),
'crontab' => $this->onCrontabWorker($swollen_universalize),
default => $this->onMessageWorker($server, $src_worker_id, $swollen_universalize)
};
fire(Event::SYSTEM_RESOURCE_RELEASES);
});
}
/**
* @param Server $server
* @param int $src_worker_id
* @param $swollen_universalize
* @throws Exception
*/
public function onHandler(Server $server, int $src_worker_id, $swollen_universalize)
{
go(function () use ($server, $src_worker_id, $swollen_universalize) {
match ($swollen_universalize['action'] ?? null) {
'kafka' => $this->onKafkaWorker($swollen_universalize),
'crontab' => $this->onCrontabWorker($swollen_universalize),
default => $this->onMessageWorker($server, $src_worker_id, $swollen_universalize)
};
fire(Event::SYSTEM_RESOURCE_RELEASES);
});
}
/**
* @param array $message
* @return string
* @throws Exception
*/
private function onCrontabWorker(array $message): string
{
if (!isset($message['handler'])) {
throw new Exception('unknown handler');
}
/** @var Crontab $crontab */
$crontab = swoole_unserialize($message['handler']);
$crontab->increment()->execute();
return 'success';
}
/**
* @param array $message
* @return string
* @throws Exception
*/
private function onCrontabWorker(array $message): string
{
if (empty($handler = $message['handler'] ?? null)) {
throw new Exception('unknown handler');
}
/** @var Crontab $handler */
$handler->increment()->execute();
return 'success';
}
/**
* @param $server
* @param $src_worker_id
* @param $message
* @return string
* @throws Exception
*/
private function onMessageWorker($server, $src_worker_id, $message): string
{
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
/**
* @param $server
* @param $src_worker_id
* @param $message
* @return string
* @throws Exception
*/
private function onMessageWorker($server, $src_worker_id, $message): string
{
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
return 'success';
}
return 'success';
}
/**
* @param array $message
* @return string
*/
private function onKafkaWorker(array $message): string
{
[$topic, $rdMessage] = $message['body'];
/**
* @param array $message
* @return string
*/
private function onKafkaWorker(array $message): string
{
[$topic, $rdMessage] = $message['body'];
call_user_func($message['handler'], new Struct($topic, $rdMessage));
call_user_func($message['handler'], new Struct($topic, $rdMessage));
return 'success';
}
return 'success';
}
}