2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-02 11:38:47 +08:00
|
|
|
namespace HttpServer\Events;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2020-09-02 17:02:13 +08:00
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2021-04-11 17:18:54 +08:00
|
|
|
use Kafka\Struct;
|
2021-04-12 11:05:57 +08:00
|
|
|
use Snowflake\Crontab\Crontab;
|
2020-09-02 17:02:13 +08:00
|
|
|
use Snowflake\Event;
|
|
|
|
|
use Swoole\Server;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-02 17:02:13 +08:00
|
|
|
/**
|
|
|
|
|
* Class OnPipeMessage
|
|
|
|
|
* @package HttpServer\Events
|
|
|
|
|
*/
|
2020-08-31 01:27:08 +08:00
|
|
|
class OnPipeMessage extends Callback
|
|
|
|
|
{
|
|
|
|
|
|
2021-04-15 18:15:23 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $src_worker_id
|
|
|
|
|
* @param $swollen_universalize
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-04-15 18:18:41 +08:00
|
|
|
public function onHandler(Server $server, int $src_worker_id, $swollen_universalize)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
match ($swollen_universalize['action'] ?? null) {
|
|
|
|
|
'kafka' => $this->onKafkaWorker($swollen_universalize),
|
|
|
|
|
'crontab' => $this->onCrontabWorker($swollen_universalize),
|
|
|
|
|
default => $this->onMessageWorker($server, $src_worker_id, $swollen_universalize)
|
|
|
|
|
};
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
$this->addError($exception);
|
|
|
|
|
} finally {
|
|
|
|
|
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2021-04-11 17:18:54 +08:00
|
|
|
|
2021-04-12 11:05:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $message
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-04-15 18:18:41 +08:00
|
|
|
private function onCrontabWorker(array $message): string
|
|
|
|
|
{
|
|
|
|
|
if (!isset($message['handler'])) {
|
|
|
|
|
throw new Exception('unknown handler');
|
|
|
|
|
}
|
2021-04-15 18:21:20 +08:00
|
|
|
|
|
|
|
|
/** @var Crontab $crontab */
|
|
|
|
|
$crontab = swoole_unserialize($message['handler']);
|
2021-04-15 18:18:41 +08:00
|
|
|
$crontab->increment()->execute();
|
|
|
|
|
return 'success';
|
|
|
|
|
}
|
2021-04-12 02:55:12 +08:00
|
|
|
|
|
|
|
|
|
2021-04-12 11:05:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param $server
|
|
|
|
|
* @param $src_worker_id
|
|
|
|
|
* @param $message
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-04-15 18:18:41 +08:00
|
|
|
private function onMessageWorker($server, $src_worker_id, $message): string
|
|
|
|
|
{
|
|
|
|
|
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
|
2021-04-11 17:18:54 +08:00
|
|
|
|
2021-04-15 18:18:41 +08:00
|
|
|
return 'success';
|
|
|
|
|
}
|
2021-04-11 17:18:54 +08:00
|
|
|
|
|
|
|
|
|
2021-04-12 11:05:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $message
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-04-15 18:18:41 +08:00
|
|
|
private function onKafkaWorker(array $message): string
|
|
|
|
|
{
|
|
|
|
|
[$topic, $rdMessage] = $message['body'];
|
2021-04-11 19:02:42 +08:00
|
|
|
|
2021-04-15 18:18:41 +08:00
|
|
|
call_user_func($message['handler'], new Struct($topic, $rdMessage));
|
2021-04-11 19:02:42 +08:00
|
|
|
|
2021-04-15 18:18:41 +08:00
|
|
|
return 'success';
|
|
|
|
|
}
|
2021-04-11 17:18:54 +08:00
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|