This commit is contained in:
as2252258@163.com
2021-04-07 02:21:29 +08:00
parent ed6cb4cf28
commit b74b0be018
3 changed files with 157 additions and 109 deletions
+59 -49
View File
@@ -18,66 +18,76 @@ class Annotation extends Component
{
private Loader $_loader;
private Loader $_loader;
public function init(): void
{
$this->_loader = new Loader();
}
public function init(): void
{
$this->_loader = new Loader();
}
/**
* @param string $className
* @param string $method
* @return array 根据类名获取注解
* 根据类名获取注解
*/
public function getMethods(string $className, string $method = ''): mixed
{
return $this->_loader->getMethod($className, $method);
}
/**
* @return Loader
*/
public function getLoader(): Loader
{
return $this->_loader;
}
/**
* @param object $class
*/
public function injectProperty(object $class)
{
$this->_loader->injectProperty(get_class($class), $class);
}
/**
* @param string $className
* @param string $method
* @return array 根据类名获取注解
* 根据类名获取注解
*/
public function getMethods(string $className, string $method = ''): mixed
{
return $this->_loader->getMethod($className, $method);
}
/**
* @param string $path
* @param string $namespace
* @param string $alias
* @return void
* @throws Exception
*/
public function read(string $path, string $namespace, string $alias = 'root'): void
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
}
/**
* @param object $class
*/
public function injectProperty(object $class)
{
$this->_loader->injectProperty(get_class($class), $class);
}
/**
* @param string $dir
*/
public function instanceDirectoryFiles(string $dir)
{
$this->_loader->loadByDirectory($dir);
}
/**
* @param string $path
* @param string $namespace
* @param string $alias
* @return void
* @throws Exception
*/
public function read(string $path, string $namespace, string $alias = 'root'): void
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
}
/**
* @param string $filename
* @return mixed
*/
public function getFilename(string $filename): mixed
{
return $this->_loader->getClassByFilepath($filename);
}
/**
* @param string $dir
* @throws Exception
*/
public function instanceDirectoryFiles(string $dir)
{
$this->_loader->loadByDirectory($dir);
}
/**
* @param string $filename
* @return mixed
*/
public function getFilename(string $filename): mixed
{
return $this->_loader->getClassByFilepath($filename);
}
}
+53 -45
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Annotation\Loader;
use Annotation\Target;
use Exception;
use HttpServer\Abstracts\Callback;
@@ -10,6 +11,7 @@ use Snowflake\Abstracts\Config;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Process\ServerInotify;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Server;
@@ -23,60 +25,66 @@ 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);
name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker');
name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker');
if ($worker_id >= $server->setting['worker_num']) {
$this->onTask($server, $worker_id);
} else {
$this->onWorker($server, $worker_id);
}
}
$loader = Snowflake::app()->get(ServerInotify::class);
if ($worker_id >= $server->setting['worker_num']) {
$this->onTask($server, $worker_id, $loader);
} else {
$this->onWorker($server, $worker_id, $loader);
}
}
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
*/
public function onTask(Server $server, int $worker_id)
{
putenv('environmental=' . Snowflake::TASK);
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
*/
public function onTask(Server $server, int $worker_id, Loader $loader)
{
putenv('environmental=' . Snowflake::TASK);
Snowflake::setTaskId($server->worker_pid);
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
$loader->loadByDirectory(MODEL_PATH);
fire(Event::SERVER_TASK_START);
}
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
*/
public function onWorker(Server $server, int $worker_id)
{
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
/**
* @param Server $server
* @param int $worker_id
* @throws Exception
*/
public function onWorker(Server $server, int $worker_id, Loader $loader)
{
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
try {
fire(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) {
$this->addError($exception,'throwable');
write($exception->getMessage(), 'worker');
}
}
$loader->loadByDirectory(APP_PATH);
try {
fire(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
write($exception->getMessage(), 'worker');
}
}
}
+45 -15
View File
@@ -34,6 +34,26 @@ class ServerInotify extends Process
private int $int = -1;
/**
* @throws Exception
*/
private function loadAnnotation()
{
$annotation = Snowflake::app()->getAnnotation();
$annotation->read(directory('app'), 'App');
}
/**
* @return mixed
*/
public function getLoader()
{
return $this->getLoader();
}
/**
* @param \Swoole\Process $process
* @throws Exception
@@ -42,6 +62,9 @@ class ServerInotify extends Process
{
set_error_handler([$this, 'onErrorHandler']);
$this->dirs = Config::get('inotify', [APP_PATH]);
$this->loadAnnotation();
if (extension_loaded('inotify')) {
$this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
@@ -194,14 +217,17 @@ class ServerInotify extends Process
{
$this->isReloading = true;
$this->trigger_reload();
$this->clearWatch();
foreach ($this->dirs as $root) {
$this->watch($root);
}
$this->int = -1;
$this->isReloading = FALSE;
$this->isReloadingOut = FALSE;
$this->md5Map = [];
$this->exit(0);
// $this->clearWatch();
// foreach ($this->dirs as $root) {
// $this->watch($root);
// }
// $this->int = -1;
// $this->isReloading = FALSE;
// $this->isReloadingOut = FALSE;
// $this->md5Map = [];
}
/**
@@ -211,14 +237,18 @@ class ServerInotify extends Process
{
$this->isReloading = true;
$this->trigger_reload();
$this->int = -1;
$this->loadDirs();
$this->isReloading = FALSE;
$this->isReloadingOut = FALSE;
$this->tick();
$this->exit(0);
//
//
// $this->int = -1;
//
// $this->loadDirs();
//
// $this->isReloading = FALSE;
// $this->isReloadingOut = FALSE;
//
// $this->tick();
}