eee
This commit is contained in:
+19
-7
@@ -4,6 +4,8 @@
|
|||||||
namespace Kiri\Server\Abstracts;
|
namespace Kiri\Server\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri\Error\StdoutLogger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Server
|
* Class Server
|
||||||
* @package Server\Abstracts
|
* @package Server\Abstracts
|
||||||
@@ -11,12 +13,22 @@ namespace Kiri\Server\Abstracts;
|
|||||||
abstract class Server
|
abstract class Server
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Server constructor.
|
* Server constructor.
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return StdoutLogger
|
||||||
|
*/
|
||||||
|
protected function getLogger(): StdoutLogger
|
||||||
|
{
|
||||||
|
return \Kiri::getLogger();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ trait TraitServer
|
|||||||
Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no);
|
Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no);
|
||||||
$this->shutdown();
|
$this->shutdown();
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
error($exception);
|
\Kiri::getLogger()->json_log($exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class OnPipeMessage extends Server
|
|||||||
}
|
}
|
||||||
call_user_func([$message, 'process'], $server, $src_worker_id);
|
call_user_func([$message, 'process'], $server, $src_worker_id);
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
Kiri::getLogger()->error(throwable($throwable));
|
\Kiri::getLogger()->json_log($throwable, ['src_worker_id' => $src_worker_id, 'message' => $message]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+115
-115
@@ -30,139 +30,139 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EventDispatch
|
* @var EventDispatch
|
||||||
*/
|
*/
|
||||||
#[Container(EventDispatch::class)]
|
#[Container(EventDispatch::class)]
|
||||||
public EventDispatch $dispatch;
|
public EventDispatch $dispatch;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
on(OnBeforeWorkerStart::class, [$this, 'onWorkerNameAlias']);
|
on(OnBeforeWorkerStart::class, [$this, 'onWorkerNameAlias']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param OnBeforeWorkerStart $workerStart
|
* @param OnBeforeWorkerStart $workerStart
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function onWorkerNameAlias(OnBeforeWorkerStart $workerStart): void
|
public function onWorkerNameAlias(OnBeforeWorkerStart $workerStart): void
|
||||||
{
|
{
|
||||||
set_env('environmental_workerId', $workerStart->workerId);
|
if ($workerStart->workerId < $workerStart->server->setting['worker_num']) {
|
||||||
if ($workerStart->workerId < $workerStart->server->setting['worker_num']) {
|
$this->processName($workerStart->server, 'Worker');
|
||||||
$this->processName($workerStart->server, 'Worker');
|
set_env('environmental', Kiri::WORKER);
|
||||||
set_env('environmental', Kiri::WORKER);
|
} else {
|
||||||
} else {
|
$this->processName($workerStart->server, 'Tasker');
|
||||||
$this->processName($workerStart->server, 'Tasker');
|
set_env('environmental', Kiri::TASK);
|
||||||
set_env('environmental', Kiri::TASK);
|
}
|
||||||
}
|
set_env('environmental_worker_id', $workerStart->workerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @return void
|
* @return void
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function onWorkerStart(Server $server, int $workerId): void
|
public function onWorkerStart(Server $server, int $workerId): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
|
$this->dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId));
|
||||||
if ($workerId < $server->setting['worker_num']) {
|
if ($workerId < $server->setting['worker_num']) {
|
||||||
CoordinatorManager::utility(Coordinator::WORKER_START)->waite();
|
CoordinatorManager::utility(Coordinator::WORKER_START)->waite();
|
||||||
|
|
||||||
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
|
$this->dispatch->dispatch(new OnWorkerStart($server, $workerId));
|
||||||
} else {
|
} else {
|
||||||
$this->dispatch->dispatch(new OnTaskerStart($server, $workerId));
|
$this->dispatch->dispatch(new OnTaskerStart($server, $workerId));
|
||||||
}
|
}
|
||||||
$this->dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
|
} catch (Throwable $exception) {
|
||||||
} catch (Throwable $exception) {
|
\Kiri::getLogger()->json_log($exception);
|
||||||
Kiri::getLogger()->println(throwable($exception));
|
} finally {
|
||||||
}
|
$this->dispatch->dispatch(new OnAfterWorkerStart($server, $workerId));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param string $prefix
|
* @param string $prefix
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function processName(Server $server, string $prefix): void
|
protected function processName(Server $server, string $prefix): void
|
||||||
{
|
{
|
||||||
Kiri::setProcessName(sprintf($prefix . '[%d]', $server->worker_pid));
|
Kiri::setProcessName(sprintf($prefix . '[%d]', $server->worker_pid));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function onWorkerStop(Server $server, int $workerId): void
|
public function onWorkerStop(Server $server, int $workerId): void
|
||||||
{
|
{
|
||||||
event(new OnWorkerStop($server, $workerId));
|
event(new OnWorkerStop($server, $workerId));
|
||||||
Timer::clearAll();
|
Timer::clearAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $workerId
|
* @param int $workerId
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function onWorkerExit(Server $server, int $workerId): void
|
public function onWorkerExit(Server $server, int $workerId): void
|
||||||
{
|
{
|
||||||
event(new OnWorkerExit($server, $workerId));
|
event(new OnWorkerExit($server, $workerId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $worker_id
|
* @param int $worker_id
|
||||||
* @param int $worker_pid
|
* @param int $worker_pid
|
||||||
* @param int $exit_code
|
* @param int $exit_code
|
||||||
* @param int $signal
|
* @param int $signal
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void
|
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void
|
||||||
{
|
{
|
||||||
event(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
event(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
||||||
|
|
||||||
debug_print_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
|
debug_print_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
|
||||||
|
|
||||||
/** @var RequestInterface $context */
|
/** @var RequestInterface $context */
|
||||||
$context = Kiri\Di\Context::get(RequestInterface::class);
|
$context = Kiri\Di\Context::get(RequestInterface::class);
|
||||||
if (is_null($context)) {
|
if (is_null($context)) {
|
||||||
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal));
|
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal));
|
||||||
} else {
|
} else {
|
||||||
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s, method: %s, path: %s, query: %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal),
|
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s, method: %s, path: %s, query: %s', $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), $signal),
|
||||||
$context->getMethod(), $context->getUri()->getPath(), $context->getUri()->getQuery());
|
$context->getMethod(), $context->getUri()->getPath(), $context->getUri()->getQuery());
|
||||||
}
|
}
|
||||||
error($message . PHP_EOL);
|
$this->getLogger()->println($message);
|
||||||
|
$this->system_mail($message);
|
||||||
$this->system_mail($message);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $messageContent
|
* @param $messageContent
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
protected function system_mail($messageContent): void
|
protected function system_mail($messageContent): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$email = config('email', ['enable' => false]);
|
$email = config('email', ['enable' => false]);
|
||||||
if (!empty($email) && ($email['enable'] ?? false)) {
|
if (!empty($email) && ($email['enable'] ?? false)) {
|
||||||
Help::sendEmail($email, 'Service Error', $messageContent);
|
Help::sendEmail($email, 'Service Error', $messageContent);
|
||||||
}
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
error($e, ['email']);
|
\Kiri::getLogger()->json_log($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -82,7 +82,9 @@ class Task
|
|||||||
|
|
||||||
$response = call_user_func([$handler, 'process'], $task_id, $src_worker_id);
|
$response = call_user_func([$handler, 'process'], $task_id, $src_worker_id);
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$response = throwable($throwable);
|
\Kiri::getLogger()->json_log($throwable, ['task_id' => $task_id, 'src_worker_id' => $src_worker_id, 'data' => $data]);
|
||||||
|
|
||||||
|
$response = throwable($throwable);
|
||||||
} finally {
|
} finally {
|
||||||
$server->finish($response);
|
$server->finish($response);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user