This commit is contained in:
2021-07-27 16:15:28 +08:00
parent a36f876792
commit 887209cfaa
3 changed files with 113 additions and 109 deletions
+6 -2
View File
@@ -6,7 +6,9 @@ namespace Annotation;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use ReflectionException;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Exception\NotFindClassException;
/** /**
* Class Annotation * Class Annotation
@@ -166,6 +168,8 @@ class Annotation extends Component
/** /**
* @param object $class * @param object $class
* @throws ReflectionException
* @throws NotFindClassException
*/ */
public function injectProperty(object $class) public function injectProperty(object $class)
{ {
@@ -189,10 +193,10 @@ class Annotation extends Component
/** /**
* @param string $dir * @param string $dir
* @param string|array $outPath * @return array
* @throws Exception * @throws Exception
*/ */
public function runtime(string $dir) public function runtime(string $dir): array
{ {
return $this->_loader->loadByDirectory($dir); return $this->_loader->loadByDirectory($dir);
} }
+3 -3
View File
@@ -165,20 +165,20 @@ class Loader extends BaseObject
/** /**
* @param string $path * @param string $path
* @param string|array $outPath * @return array
* @throws Exception * @throws Exception
*/ */
public function loadByDirectory(string $path) public function loadByDirectory(string $path): array
{ {
try { try {
$path = '/' . trim($path, '/'); $path = '/' . trim($path, '/');
$paths = []; $paths = [];
foreach (static::$_directory as $key => $_path) { foreach (static::$_directory as $key => $_path) {
$key = '/' . trim($key, '/'); $key = '/' . trim($key, '/');
if (!str_starts_with($key, $path)) { if (!str_starts_with($key, $path)) {
continue; continue;
} }
unset(static::$_directory[$key]);
foreach ($_path as $item) { foreach ($_path as $item) {
$paths[] = $item; $paths[] = $item;
} }
+104 -104
View File
@@ -11,7 +11,6 @@ use Snowflake\Event;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Runtime; use Snowflake\Runtime;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Server; use Swoole\Server;
use Swoole\Timer; use Swoole\Timer;
@@ -24,134 +23,135 @@ class ServerWorker extends \Server\Abstracts\Server
{ {
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws Exception * @throws Exception
*/ */
public function onWorkerStart(Server $server, int $workerId) public function onWorkerStart(Server $server, int $workerId)
{ {
$this->_setConfigCache($workerId); $this->_setConfigCache($workerId);
$annotation = Snowflake::app()->getAnnotation(); $annotation = Snowflake::app()->getAnnotation();
$annotation->read(APP_PATH . 'app'); $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);
} $annotation->runtime(APP_PATH . 'app');
}
/** /**
* @param Server $server * @param Server $server
* @param Annotation $annotation * @param Annotation $annotation
* @param int $workerId * @param int $workerId
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId) private function workerInitExecutor(Server $server, Annotation $annotation, int $workerId)
{ {
if ($workerId < $server->setting['worker_num']) { if ($workerId < $server->setting['worker_num']) {
$loader = Snowflake::app()->getRouter(); $loader = Snowflake::app()->getRouter();
$loader->_loader(); $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; 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)); $this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
} else { } else {
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL; 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));
} }
} }
/** /**
* @param $worker_id * @param $worker_id
* @throws Exception * @throws Exception
*/ */
private function _setConfigCache($worker_id) private function _setConfigCache($worker_id)
{ {
putenv('state=start'); putenv('state=start');
putenv('worker=' . $worker_id); putenv('worker=' . $worker_id);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); $serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (empty($serialize)) { if (empty($serialize)) {
return; return;
} }
Config::sets(unserialize($serialize)); Config::sets(unserialize($serialize));
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws Exception * @throws Exception
*/ */
public function onWorkerStop(Server $server, int $workerId) public function onWorkerStop(Server $server, int $workerId)
{ {
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]); $this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
Event::trigger(Event::SERVER_WORKER_STOP); Event::trigger(Event::SERVER_WORKER_STOP);
fire(Event::SYSTEM_RESOURCE_CLEAN); fire(Event::SYSTEM_RESOURCE_CLEAN);
Timer::clearAll(); Timer::clearAll();
} }
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
* @throws Exception * @throws Exception
*/ */
public function onWorkerExit(Server $server, int $workerId) public function onWorkerExit(Server $server, int $workerId)
{ {
$this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]); $this->runEvent(Constant::WORKER_EXIT, null, [$server, $workerId]);
putenv('state=exit'); putenv('state=exit');
Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $workerId]); Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $workerId]);
Snowflake::getApp('logger')->insert(); Snowflake::getApp('logger')->insert();
} }
/** /**
* @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 Exception * @throws Exception
*/ */
public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) 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]); $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', $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) $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
); );
write($message, 'worker-exit'); write($message, 'worker-exit');
$this->system_mail($message); $this->system_mail($message);
} }
/** /**
* @param $messageContent * @param $messageContent
* @throws Exception * @throws Exception
*/ */
protected function system_mail($messageContent) protected function system_mail($messageContent)
{ {
try { try {
$email = Config::get('email'); $email = Config::get('email');
if (!empty($email) && ($email['enable'] ?? false) == true) { if (!empty($email) && ($email['enable'] ?? false) == true) {
Help::sendEmail($email, 'Service Error', $messageContent); Help::sendEmail($email, 'Service Error', $messageContent);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
error($e, 'email'); error($e, 'email');
} }
} }
} }