This commit is contained in:
as2252258@163.com
2021-07-26 04:59:03 +08:00
parent abdaeb3e08
commit 826b3b2102
7 changed files with 394 additions and 342 deletions
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Server;
use Swoole\Coroutine\Channel;
class ApplicationStore
{
private static ?ApplicationStore $applicationStore = null;
private Channel $lock;
private function __construct()
{
$this->lock = new Channel(99999);
}
/**
* @return \Server\ApplicationStore|null
*/
public static function getStore()
{
if (!(static::$applicationStore instanceof ApplicationStore)) {
static::$applicationStore = new ApplicationStore();
}
return static::$applicationStore;
}
public function add()
{
$this->lock->push(1);
return $this;
}
public function waite()
{
$this->lock->pop(-1);
}
public function done()
{
$this->lock->pop();
}
/**
* @return bool
*/
public function isBusy()
{
return !$this->lock->isEmpty();
}
/**
* @return string
*/
public function getStatus(): string
{
return env('state');
}
}
+101 -98
View File
@@ -26,121 +26,124 @@ use Throwable;
class HTTPServerListener extends Abstracts\Server
{
protected static bool|Port $_http;
protected static bool|Port $_http;
use ListenerHelper;
use ListenerHelper;
private Router $router;
private Router $router;
private ApplicationStore $store;
/**
* HTTPServerListener constructor.
* @throws Exception
*/
public function __construct()
{
$this->router = Snowflake::getApp('router');
parent::__construct();
}
/**
* HTTPServerListener constructor.
* @throws Exception
*/
public function __construct()
{
$this->router = Snowflake::getApp('router');
$this->store = ApplicationStore::getStore();
parent::__construct();
}
/**
* UDPServerListener constructor.
* @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server
* @param string $host
* @param int $port
* @param int $mode
* @param array|null $settings
* @return Server\Port
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
{
if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) {
trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.');
}
/**
* UDPServerListener constructor.
* @param Server|\Swoole\WebSocket\Server|\Swoole\Http\Server $server
* @param string $host
* @param int $port
* @param int $mode
* @param array|null $settings
* @return Server\Port
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = []): Server\Port
{
if (!in_array($mode, [SWOOLE_TCP, SWOOLE_TCP6])) {
trigger_error('Port mode ' . $host . '::' . $port . ' must is udp listener type.');
}
/** @var static $reflect */
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
static::$_http = $server->addlistener($host, $port, $mode);
if (!(static::$_http instanceof Port)) {
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
}
/** @var static $reflect */
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
static::$_http = $server->addlistener($host, $port, $mode);
if (!(static::$_http instanceof Port)) {
trigger_error('Port is ' . $host . '::' . $port . ' must is tcp listener type.');
}
static::$_http->set($settings['settings'] ?? []);
static::$_http->on('request', [$reflect, 'onRequest']);
static::$_http->on('connect', [$reflect, 'onConnect']);
static::$_http->on('close', [$reflect, 'onClose']);
if (swoole_version() >= '4.7.0') {
static::$_http->on('disconnect', [$reflect, 'onDisconnect']);
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
}
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
return static::$_http;
}
static::$_http->set($settings['settings'] ?? []);
static::$_http->on('request', [$reflect, 'onRequest']);
static::$_http->on('connect', [$reflect, 'onConnect']);
static::$_http->on('close', [$reflect, 'onClose']);
if (swoole_version() >= '4.7.0') {
static::$_http->on('disconnect', [$reflect, 'onDisconnect']);
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
}
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
return static::$_http;
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onConnect(Server $server, int $fd)
{
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onConnect(Server $server, int $fd)
{
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
/**
* @param Request $request
* @param Response $response
* @throws Exception
*/
public function onRequest(Request $request, Response $response)
{
try {
[$request, $_] = [HRequest::create($request), HResponse::create($response)];
if ($request->is('favicon.ico')) {
throw new Exception('Not found.', 404);
}
$this->router->dispatch();
} catch (ExitException | Error | Throwable $exception) {
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
$response->status($exception->getCode() == 0 ? 500 : $exception->getCode());
$response->end($exception->getMessage());
} finally {
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
}
/**
* @param Request $request
* @param Response $response
* @throws Exception
*/
public function onRequest(Request $request, Response $response)
{
try {
$request = HRequest::create($request, $response);
if ($request->is('favicon.ico')) {
throw new Exception('Not found.', 404);
}
$this->router->dispatch();
} catch (ExitException | Error | Throwable $exception) {
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
$response->status($exception->getCode() == 0 ? 500 : $exception->getCode());
$response->end($exception->getMessage());
} finally {
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onDisconnect(Server $server, int $fd)
{
$this->runEvent(Constant::DISCONNECT, null, [$server, $fd]);
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onDisconnect(Server $server, int $fd)
{
$this->runEvent(Constant::DISCONNECT, null, [$server, $fd]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onClose(Server $server, int $fd)
{
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
/**
* @param Server $server
* @param int $fd
* @throws Exception
*/
public function onClose(Server $server, int $fd)
{
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
}
+104 -107
View File
@@ -24,142 +24,139 @@ class ServerWorker extends \Server\Abstracts\Server
{
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId)
{
$this->_setConfigCache($workerId);
$annotation = Snowflake::app()->getAnnotation();
$annotation->read(APP_PATH . 'app');
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStart(Server $server, int $workerId)
{
$this->_setConfigCache($workerId);
$annotation = Snowflake::app()->getAnnotation();
$annotation->read(APP_PATH . 'app');
$this->runEvent(Constant::WORKER_START, null, [$server, $workerId]);
$this->runEvent(Constant::WORKER_START, null, [$server, $workerId]);
$this->workerInitExecutor($server, $annotation, $workerId);
}
$this->workerInitExecutor($server, $annotation, $workerId);
}
/**
* @param Server $server
* @param Annotation $annotation
* @param int $workerId
* @throws ConfigException
* @throws Exception
*/
private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId)
{
if ($workerId < $server->setting['worker_num']) {
$loader = Snowflake::app()->getRouter();
$loader->_loader();
/**
* @param Server $server
* @param Annotation $annotation
* @param int $workerId
* @throws ConfigException
* @throws Exception
*/
private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId)
{
if ($workerId < $server->setting['worker_num']) {
$loader = Snowflake::app()->getRouter();
$loader->_loader();
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
$annotation->runtime(CONTROLLER_PATH);
$annotation->runtime(MODEL_PATH);
} else {
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$annotation->runtime(CONTROLLER_PATH);
$annotation->runtime(MODEL_PATH);
} else {
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
$annotation->runtime(APP_PATH, [CONTROLLER_PATH]);
}
}
$annotation->runtime(APP_PATH, [CONTROLLER_PATH]);
}
}
/**
* @param $worker_id
* @throws Exception
*/
private function _setConfigCache($worker_id)
{
putenv('state=start');
putenv('worker=' . $worker_id);
/**
* @param $worker_id
* @throws Exception
*/
private function _setConfigCache($worker_id)
{
putenv('state=start');
putenv('worker=' . $worker_id);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (empty($serialize)) {
return;
}
Config::sets(unserialize($serialize));
}
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (empty($serialize)) {
return;
}
Config::sets(unserialize($serialize));
}
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStop(Server $server, int $workerId)
{
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerStop(Server $server, int $workerId)
{
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
Event::trigger(Event::SERVER_WORKER_STOP);
Event::trigger(Event::SERVER_WORKER_STOP);
var_dump('worker stop .' . $workerId);
fire(Event::SYSTEM_RESOURCE_CLEAN);
fire(Event::SYSTEM_RESOURCE_CLEAN);
Timer::clearAll();
}
Timer::clearAll();
}
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerExit(Server $server, int $workerId)
{
$this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]);
/**
* @param Server $server
* @param int $workerId
* @throws Exception
*/
public function onWorkerExit(Server $server, int $workerId)
{
$this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]);
putenv('state=exit');
putenv('state=exit');
Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $workerId]);
Snowflake::getApp('logger')->insert();
}
}
/**
* @param Server $server
* @param int $worker_id
* @param int $worker_pid
* @param int $exit_code
* @param int $signal
* @throws Exception
*/
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{
$this->runEvent(Constant::WORKER_ERROR, null, [$server, $worker_id, $worker_pid, $exit_code, $signal]);
/**
* @param Server $server
* @param int $worker_id
* @param int $worker_pid
* @param int $exit_code
* @param int $signal
* @throws Exception
*/
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{
$this->runEvent(Constant::WORKER_ERROR, null, [$server, $worker_id, $worker_pid, $exit_code, $signal]);
Event::trigger(Event::SERVER_WORKER_ERROR);
Event::trigger(Event::SERVER_WORKER_ERROR);
$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(), 9)
);
write($message, 'worker-exit');
$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(), 9)
);
write($message, 'worker-exit');
$this->system_mail($message);
}
$this->system_mail($message);
}
/**
* @param $messageContent
* @throws Exception
*/
protected function system_mail($messageContent)
{
try {
$email = Config::get('email');
if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
} catch (\Throwable $e) {
error($e, 'email');
}
}
/**
* @param $messageContent
* @throws Exception
*/
protected function system_mail($messageContent)
{
try {
$email = Config::get('email');
if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent);
}
} catch (\Throwable $e) {
error($e, 'email');
}
}
}