diff --git a/kiri-actor/ActorManager.php b/kiri-actor/ActorManager.php index f2fca9a5..41677a4a 100644 --- a/kiri-actor/ActorManager.php +++ b/kiri-actor/ActorManager.php @@ -8,91 +8,103 @@ use Swoole\Coroutine; class ActorManager { - /** @var array */ - private array $nodes = []; + /** @var array */ + private array $nodes = []; - /** - * @param Actor $actor - * @return void - */ - public function addActor(ActorInterface $actor): void - { - $this->nodes[$actor->uniqueId] = $actor; - Coroutine::create(function (Actor $actor) { - $actor->run(); - }, $actor); - } + /** + * @param Actor $actor + * @return void + */ + public function addActor(ActorInterface $actor): void + { + $this->nodes[$actor->uniqueId] = $actor; + Coroutine::create(function (Actor $actor) { + $actor->run(); + }, $actor); + } - /** - * @param $name - * @return void - */ - public function closeActor($name): void - { - $node = $this->nodes[$name] ?? null; - if (is_null($node)) { - return; - } - foreach ($node as $actor) { - $actor->shutdown(); - } - } + /** + * @param $name + * @return void + */ + public function closeActor($name): void + { + $node = $this->nodes[$name] ?? null; + if (is_null($node)) { + return; + } + foreach ($node as $actor) { + $actor->shutdown(); + } + } - /** - * @param $name - * @param $message - * @return bool - */ - public function write($name, $message): bool - { - $actor = $this->nodes[$name] ?? null; - if (is_null($actor)) { - return false; - } - return $actor->write($message); - } + /** + * @param $name + * @param $message + * @return bool + */ + public function write($name, $message): bool + { + $actor = $this->nodes[$name] ?? null; + if (is_null($actor)) { + return false; + } + return $actor->write($message); + } - /** - * @param $name - * @return array - */ - public function lists($name): array - { - $array = []; - foreach ($this->nodes[$name] as $actor) { - $array[] = [ - 'id' => $actor->getName(), - 'state' => $actor->getState()->name, - 'runTime' => $actor->getRunTime() - ]; - } - return $array; - } + /** + * @param $name + * @return array + */ + public function lists($name): array + { + $array = []; + foreach ($this->nodes[$name] as $actor) { + $array[] = [ + 'id' => $actor->getName(), + 'state' => $actor->getState()->name, + 'runTime' => $actor->getRunTime() + ]; + } + return $array; + } - /** - * @param string $uniqueId - * @return bool - */ - public function hasActor(string $uniqueId): bool - { - return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface; - } + /** + * @param string $uniqueId + * @return bool + */ + public function hasActor(string $uniqueId): bool + { + return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface; + } - /** - * @return void - */ - public function clean(): void - { - foreach ($this->nodes as $actor) { - $actor->shutdown(); - } - $this->nodes = []; - } + /** + * @param array|null $data + * @return void + */ + public static function exec(?array $data): void + { + if (is_null($data)) { + return; + } + } + + + /** + * @return void + */ + public function clean(): void + { + foreach ($this->nodes as $actor) { + $actor->shutdown(); + } + $this->nodes = []; + } } diff --git a/kiri-actor/ActorMessage.php b/kiri-actor/ActorMessage.php index 458895a7..91bcaa2d 100644 --- a/kiri-actor/ActorMessage.php +++ b/kiri-actor/ActorMessage.php @@ -3,6 +3,8 @@ declare(strict_types=1); namespace Kiri\Actor; +use JetBrains\PhpStorm\ArrayShape; + class ActorMessage implements \JsonSerializable { @@ -63,7 +65,8 @@ class ActorMessage implements \JsonSerializable /** * @return array */ - public function jsonSerialize(): array + #[ArrayShape(['userId' => "int", 'event' => "string", 'body' => "array"])] + public function jsonSerialize(): array { return [ 'userId' => $this->userId, diff --git a/kiri-actor/ActorProcess.php b/kiri-actor/ActorProcess.php new file mode 100644 index 00000000..586b86f1 --- /dev/null +++ b/kiri-actor/ActorProcess.php @@ -0,0 +1,55 @@ +isStop() === false) { + $read = $process->read(); + + ActorManager::exec(json_decode($read, true)); + + Coroutine::sleep(1000 / 120); + } + } + + + /** + * @return $this + */ + public function onSigterm(): static + { + // TODO: Implement onSigterm() method. + Coroutine::create(function () { + $sign = Coroutine::waitSignal(SIGINT | SIGTERM); + if ($sign) { + $this->onShutdown(true); + } + }); + return $this; + } +} \ No newline at end of file diff --git a/kiri-engine/Abstracts/Component.php b/kiri-engine/Abstracts/Component.php index 4bbdac3e..507a3888 100644 --- a/kiri-engine/Abstracts/Component.php +++ b/kiri-engine/Abstracts/Component.php @@ -22,66 +22,76 @@ class Component implements Configure { - /** - * BaseAbstract constructor. - */ - public function __construct() - { - } + /** + * BaseAbstract constructor. + */ + public function __construct() + { + } - /** - * @return void - */ - public function init(): void - { - } + /** + * @return void + */ + public function init(): void + { + } - /** - * @return string - */ - #[Pure] public static function className(): string - { - return static::class; - } + /** + * @return string + */ + #[Pure] public static function className(): string + { + return static::class; + } - /** - * @param string $name - * @return mixed - * @throws Exception - */ - public function __get(string $name) - { - $method = 'get' . ucfirst($name); - if (method_exists($this, $method)) { - return $this->{$method}(); - } else if (method_exists($this, $name)) { - return $this->{$name}; - } else { - throw new Exception('Unable getting property ' . get_called_class() . '::' . $name); - } - } + /** + * @return Kiri\Error\StdoutLogger + * @throws \ReflectionException + */ + public function getLogger(): Kiri\Error\StdoutLogger + { + return Kiri::getLogger(); + } - /** - * @param string $name - * @param $value - * @return void - * @throws Exception - */ - public function __set(string $name, $value): void - { - $method = 'set' . ucfirst($name); - if (method_exists($this, $method)) { - $this->{$method}($value); - } else if (method_exists($this, $name)) { - $this->{$name} = $value; - } else { - throw new Exception('Unable setting property ' . get_called_class() . '::' . $name); - } - } + /** + * @param string $name + * @return mixed + * @throws Exception + */ + public function __get(string $name) + { + $method = 'get' . ucfirst($name); + if (method_exists($this, $method)) { + return $this->{$method}(); + } else if (method_exists($this, $name)) { + return $this->{$name}; + } else { + throw new Exception('Unable getting property ' . get_called_class() . '::' . $name); + } + } + + + /** + * @param string $name + * @param $value + * @return void + * @throws Exception + */ + public function __set(string $name, $value): void + { + $method = 'set' . ucfirst($name); + if (method_exists($this, $method)) { + $this->{$method}($value); + } else if (method_exists($this, $name)) { + $this->{$name} = $value; + } else { + throw new Exception('Unable setting property ' . get_called_class() . '::' . $name); + } + } } diff --git a/kiri-engine/Error/ErrorHandler.php b/kiri-engine/Error/ErrorHandler.php index e7c456b6..c408cfec 100644 --- a/kiri-engine/Error/ErrorHandler.php +++ b/kiri-engine/Error/ErrorHandler.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Kiri\Error; use Closure; +use ErrorException; use Exception; use Kiri; use Kiri\Abstracts\Component; @@ -18,6 +19,7 @@ use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; use ReflectionException; use Kiri\Events\OnSystemError; +use Throwable; /** * Class ErrorHandler @@ -110,13 +112,13 @@ class ErrorHandler extends Component implements ErrorInterface /** - * @param \Throwable $exception + * @param Throwable $exception * * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ - public function exceptionHandler(\Throwable $exception): void + public function exceptionHandler(Throwable $exception): void { $this->category = 'exception'; @@ -127,7 +129,7 @@ class ErrorHandler extends Component implements ErrorInterface /** - * @throws \ErrorException + * @throws ErrorException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws ReflectionException @@ -136,8 +138,10 @@ class ErrorHandler extends Component implements ErrorInterface { $error = func_get_args(); + var_dump($error); + event(new OnSystemError()); - throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); + throw new ErrorException($error[1], $error[0], 1, $error[2], $error[3]); } } diff --git a/p.php b/p.php index 427e22f8..59976b5a 100644 --- a/p.php +++ b/p.php @@ -1,17 +1,14 @@ handle('/', function () { - }); - $server->start(); -}); + $channel = new \Swoole\Coroutine\Channel(100); + for ($i = 0; $i < 90; $i++) { + $channel->push(100); + } + $channel->close(); + $channel = null; + var_dump($channel); +}); \ No newline at end of file diff --git a/test.php b/test.php index e4068577..4c2beb42 100644 --- a/test.php +++ b/test.php @@ -1,52 +1,13 @@ get('/v1/agent/services?filter=Service == FriendRpcService'); -// $client->close(); -// var_dump($client->getBody()); -// -//}); - -$spl = new \SplPriorityQueue(); -$spl->insert(1,0); -$spl->insert(2,0); -$spl->insert(3,0); -$spl->insert(4,0); -$spl->insert(5,0); -$spl->insert(6,0); -$spl->insert(7,0); - - -$spl->compare(); -$spl->extract(); - -var_dump($spl); \ No newline at end of file + echo '[' . $i . '%]' . "\r"; + } + sleep(1); +} \ No newline at end of file