diff --git a/HttpServer/Events/OnPipeMessage.php b/HttpServer/Events/OnPipeMessage.php index 76e85570..44ea20e0 100644 --- a/HttpServer/Events/OnPipeMessage.php +++ b/HttpServer/Events/OnPipeMessage.php @@ -68,6 +68,10 @@ class OnPipeMessage extends Callback { [$topic, $message] = $message['body']; + call_user_func($message['handler'], new Struct($topic, $message)); + + return 'success'; + /** @var TaskContainer $container */ $container = Snowflake::app()->get('kafka-container'); $container->process($topic, new Struct($topic, $message)); diff --git a/Kafka/Kafka.php b/Kafka/Kafka.php index 0de2effe..d9d095c3 100644 --- a/Kafka/Kafka.php +++ b/Kafka/Kafka.php @@ -104,7 +104,14 @@ class Kafka extends \Snowflake\Process\Process $setting = $server->setting['worker_num']; - $message = swoole_serialize(['action' => 'kafka', 'body' => [$topic, $message]]); + /** @var TaskContainer $container */ + $container = Snowflake::app()->get('kafka-container'); + $handler = $container->getConsumer($topic); + + if (empty($handler)) { + return; + } + $message = swoole_serialize(['action' => 'kafka', 'handler' => $handler, 'body' => [$topic, $message]]); $server->sendMessage($message, random_int(0, $setting - 1)); } catch (Throwable $exception) { diff --git a/Kafka/TaskContainer.php b/Kafka/TaskContainer.php index eb884053..e3c90caa 100644 --- a/Kafka/TaskContainer.php +++ b/Kafka/TaskContainer.php @@ -31,6 +31,16 @@ class TaskContainer extends BaseObject } + /** + * @param $topic + * @return mixed|null + */ + public function getConsumer(string $topic) + { + return $this->_topics[$topic] ?? null; + } + + /** * @param $topic * @param \Kafka\Struct $struct diff --git a/System/Process/Process.php b/System/Process/Process.php index 3499fcb1..91c99b56 100644 --- a/System/Process/Process.php +++ b/System/Process/Process.php @@ -11,6 +11,7 @@ use Snowflake\Application; use Snowflake\Event; use Snowflake\Exception\ComponentException; use Snowflake\Snowflake; +use Swoole\Coroutine\System; /** * Class Process @@ -35,6 +36,11 @@ abstract class Process extends \Swoole\Process implements SProcess parent::__construct([$this, '_load'], false, 1, $enable_coroutine); $this->application = $application; Snowflake::setProcessId($this->pid); + + $content = System::readFile(storage('runtime.php')); + + $annotation = Snowflake::app()->getAnnotation(); + $annotation->setLoader(unserialize($content)); } /**