modify
This commit is contained in:
@@ -20,88 +20,83 @@ class OnWorkerStart extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $worker_id): void
|
||||
{
|
||||
putenv('state=start');
|
||||
putenv('worker=' . $worker_id);
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $worker_id): void
|
||||
{
|
||||
putenv('state=start');
|
||||
putenv('worker=' . $worker_id);
|
||||
|
||||
$content = System::readFile(storage('runtime.php'));
|
||||
$content = System::readFile(storage('runtime.php'));
|
||||
|
||||
$annotation = Snowflake::app()->getAnnotation();
|
||||
$annotation->setLoader(unserialize($content));
|
||||
if ($worker_id < $server->setting['worker_num']) {
|
||||
$this->onWorker($server, $annotation);
|
||||
} else {
|
||||
$this->onTask($server, $annotation);
|
||||
}
|
||||
}
|
||||
$annotation = Snowflake::app()->getAnnotation();
|
||||
$annotation->setLoader(unserialize($content));
|
||||
if ($worker_id < $server->setting['worker_num']) {
|
||||
$this->onWorker($server, $annotation);
|
||||
} else {
|
||||
$this->onTask($server, $annotation);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
* @return bool
|
||||
*/
|
||||
private function isWorker(Server $server, int $worker_id): bool
|
||||
{
|
||||
return $worker_id < $server->setting['worker_num'];
|
||||
}
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
* @return bool
|
||||
*/
|
||||
private function isWorker(Server $server, int $worker_id): bool
|
||||
{
|
||||
return $worker_id < $server->setting['worker_num'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Annotation $annotation
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onTask(Server $server, Annotation $annotation)
|
||||
{
|
||||
putenv('environmental=' . Snowflake::TASK);
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Annotation $annotation
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onTask(Server $server, Annotation $annotation)
|
||||
{
|
||||
putenv('environmental=' . Snowflake::TASK);
|
||||
|
||||
$annotation->runtime(directory('app'), [
|
||||
CONTROLLER_PATH,
|
||||
LISTENER_PATH,
|
||||
SOCKET_PATH,
|
||||
TASK_PATH,
|
||||
]);
|
||||
name($server->worker_pid, 'Task#' . $server->worker_id);
|
||||
$annotation->runtime(directory('app'), [
|
||||
CONTROLLER_PATH,
|
||||
LISTENER_PATH,
|
||||
SOCKET_PATH,
|
||||
TASK_PATH,
|
||||
]);
|
||||
name($server->worker_pid, 'Task#' . $server->worker_id);
|
||||
|
||||
Snowflake::setTaskId($server->worker_pid);
|
||||
Snowflake::setTaskId($server->worker_pid);
|
||||
|
||||
fire(Event::SERVER_TASK_START);
|
||||
}
|
||||
fire(Event::SERVER_TASK_START);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Annotation $annotation
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onWorker(Server $server, Annotation $annotation)
|
||||
{
|
||||
try {
|
||||
name($server->worker_pid, 'Worker#' . $server->worker_id);
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Annotation $annotation
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onWorker(Server $server, Annotation $annotation)
|
||||
{
|
||||
name($server->worker_pid, 'Worker#' . $server->worker_id);
|
||||
|
||||
$time = microtime(true);
|
||||
$annotation->runtime(CONTROLLER_PATH);
|
||||
$this->debug('use time.' . (microtime(true) - $time));
|
||||
$annotation->runtime(directory('app'), CONTROLLER_PATH);
|
||||
$time = microtime(true);
|
||||
$annotation->runtime(CONTROLLER_PATH);
|
||||
$this->debug('use time.' . (microtime(true) - $time));
|
||||
$annotation->runtime(directory('app'), CONTROLLER_PATH);
|
||||
|
||||
Snowflake::setWorkerId($server->worker_pid);
|
||||
putenv('environmental=' . Snowflake::WORKER);
|
||||
Snowflake::setWorkerId($server->worker_pid);
|
||||
putenv('environmental=' . Snowflake::WORKER);
|
||||
|
||||
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
write($exception->getMessage(), 'worker');
|
||||
}
|
||||
}
|
||||
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
class Pipeline
|
||||
{
|
||||
|
||||
private ?\Closure $_if = null;
|
||||
private ?\Closure $_else = null;
|
||||
private ?\Closure $_catch = null;
|
||||
private ?\Closure $_after = null;
|
||||
private ?\Closure $_before = null;
|
||||
|
||||
private bool $condition;
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $condition
|
||||
* @param \Closure $handler
|
||||
* @return $this
|
||||
*/
|
||||
public function if(bool $condition, \Closure $handler): static
|
||||
{
|
||||
$this->condition = $condition;
|
||||
$this->_if = $handler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Closure $handler
|
||||
* @return $this
|
||||
*/
|
||||
public function else(\Closure $handler): static
|
||||
{
|
||||
$this->_else = $handler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Closure $handler
|
||||
* @return $this
|
||||
*/
|
||||
public function catch(\Closure $handler): static
|
||||
{
|
||||
$this->_catch = $handler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Closure $handler
|
||||
* @return $this
|
||||
*/
|
||||
public function after(\Closure $handler): static
|
||||
{
|
||||
$this->_after = $handler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Closure $handler
|
||||
* @return $this
|
||||
*/
|
||||
public function before(\Closure $handler): static
|
||||
{
|
||||
$this->_before = $handler;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $argv
|
||||
* @return mixed
|
||||
*/
|
||||
public function exec(...$argv)
|
||||
{
|
||||
try {
|
||||
if ($this->_before instanceof \Closure) {
|
||||
call_user_func($this->_before, ...$argv);
|
||||
}
|
||||
if ($this->condition !== true) {
|
||||
call_user_func($this->_else, ...$argv);
|
||||
} else {
|
||||
call_user_func($this->_if, ...$argv);
|
||||
}
|
||||
return $argv;
|
||||
} catch (\Throwable $exception) {
|
||||
call_user_func($this->_catch, $exception);
|
||||
return $argv;
|
||||
} finally {
|
||||
if ($this->_after instanceof \Closure) {
|
||||
call_user_func($this->_after, ...$argv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer\Events\Utility;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
class DataResolve
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $unpack
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function pack($unpack, $data)
|
||||
{
|
||||
if (empty($unpack)) {
|
||||
$params = Json::encode($data);
|
||||
} else {
|
||||
$params = self::callbackResolve($unpack, null, null, $data);
|
||||
}
|
||||
if ($params === null) {
|
||||
return 'Format error.';
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $unpack
|
||||
* @param $address
|
||||
* @param $port
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public static function unpack($unpack, $address, $port, $data)
|
||||
{
|
||||
if (empty($unpack)) {
|
||||
$params = Json::decode($data);
|
||||
} else {
|
||||
$params = self::callbackResolve($unpack, $address, $port, $data);
|
||||
}
|
||||
if ($params === null) {
|
||||
return 'Format error.';
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $callback
|
||||
* @param $address
|
||||
* @param $port
|
||||
* @param $data
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private static function callbackResolve($callback, $address, $port, $data): mixed
|
||||
{
|
||||
if ($callback instanceof Closure) {
|
||||
if (empty($address) && empty($port)) {
|
||||
return $callback($data);
|
||||
}
|
||||
return $callback($address, $port, $data);
|
||||
}
|
||||
if (is_string($callback)) {
|
||||
$callback = [$callback, 'onHandler'];
|
||||
}
|
||||
if (isset($callback[0]) && is_string($callback[0])) {
|
||||
$callback[0] = Snowflake::getDi()->get($callback[0]);
|
||||
}
|
||||
if (!empty($address) && !empty($port)) {
|
||||
return call_user_func($callback, $address, $port, $data);
|
||||
}
|
||||
return call_user_func($callback, $data);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user