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 public function init(): void
{ {
$this->_loader = new Loader(); $this->_loader = new Loader();
} }
/** /**
* @param string $className * @return Loader
* @param string $method */
* @return array 根据类名获取注解 public function getLoader(): Loader
* 根据类名获取注解 {
*/ return $this->_loader;
public function getMethods(string $className, string $method = ''): mixed }
{
return $this->_loader->getMethod($className, $method);
}
/** /**
* @param object $class * @param string $className
*/ * @param string $method
public function injectProperty(object $class) * @return array 根据类名获取注解
{ * 根据类名获取注解
$this->_loader->injectProperty(get_class($class), $class); */
} public function getMethods(string $className, string $method = ''): mixed
{
return $this->_loader->getMethod($className, $method);
}
/** /**
* @param string $path * @param object $class
* @param string $namespace */
* @param string $alias public function injectProperty(object $class)
* @return void {
* @throws Exception $this->_loader->injectProperty(get_class($class), $class);
*/ }
public function read(string $path, string $namespace, string $alias = 'root'): void
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
}
/** /**
* @param string $dir * @param string $path
*/ * @param string $namespace
public function instanceDirectoryFiles(string $dir) * @param string $alias
{ * @return void
$this->_loader->loadByDirectory($dir); * @throws Exception
} */
public function read(string $path, string $namespace, string $alias = 'root'): void
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
}
/** /**
* @param string $filename * @param string $dir
* @return mixed * @throws Exception
*/ */
public function getFilename(string $filename): mixed public function instanceDirectoryFiles(string $dir)
{ {
return $this->_loader->getClassByFilepath($filename); $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; namespace HttpServer\Events;
use Annotation\Loader;
use Annotation\Target; use Annotation\Target;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
@@ -10,6 +11,7 @@ use Snowflake\Abstracts\Config;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Process\ServerInotify;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Server; use Swoole\Server;
@@ -23,60 +25,66 @@ class OnWorkerStart extends Callback
{ {
/** /**
* @param Server $server * @param Server $server
* @param int $worker_id * @param int $worker_id
* *
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server, int $worker_id): void public function onHandler(Server $server, int $worker_id): void
{ {
putenv('state=start'); putenv('state=start');
putenv('worker=' . $worker_id); 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']) { $loader = Snowflake::app()->get(ServerInotify::class);
$this->onTask($server, $worker_id);
} else { if ($worker_id >= $server->setting['worker_num']) {
$this->onWorker($server, $worker_id); $this->onTask($server, $worker_id, $loader);
} } else {
} $this->onWorker($server, $worker_id, $loader);
}
}
/** /**
* @param Server $server * @param Server $server
* @param int $worker_id * @param int $worker_id
* @throws Exception * @throws Exception
*/ */
public function onTask(Server $server, int $worker_id) public function onTask(Server $server, int $worker_id, Loader $loader)
{ {
putenv('environmental=' . Snowflake::TASK); 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 Server $server
* @param int $worker_id * @param int $worker_id
* @throws Exception * @throws Exception
*/ */
public function onWorker(Server $server, int $worker_id) public function onWorker(Server $server, int $worker_id, Loader $loader)
{ {
Snowflake::setWorkerId($server->worker_pid); Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER); putenv('environmental=' . Snowflake::WORKER);
try { $loader->loadByDirectory(APP_PATH);
fire(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) { try {
$this->addError($exception,'throwable'); fire(Event::SERVER_WORKER_START, [$worker_id]);
write($exception->getMessage(), 'worker'); } 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; 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 * @param \Swoole\Process $process
* @throws Exception * @throws Exception
@@ -42,6 +62,9 @@ class ServerInotify extends Process
{ {
set_error_handler([$this, 'onErrorHandler']); set_error_handler([$this, 'onErrorHandler']);
$this->dirs = Config::get('inotify', [APP_PATH]); $this->dirs = Config::get('inotify', [APP_PATH]);
$this->loadAnnotation();
if (extension_loaded('inotify')) { if (extension_loaded('inotify')) {
$this->inotify = inotify_init(); $this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE; $this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
@@ -194,14 +217,17 @@ class ServerInotify extends Process
{ {
$this->isReloading = true; $this->isReloading = true;
$this->trigger_reload(); $this->trigger_reload();
$this->clearWatch();
foreach ($this->dirs as $root) { $this->exit(0);
$this->watch($root);
} // $this->clearWatch();
$this->int = -1; // foreach ($this->dirs as $root) {
$this->isReloading = FALSE; // $this->watch($root);
$this->isReloadingOut = FALSE; // }
$this->md5Map = []; // $this->int = -1;
// $this->isReloading = FALSE;
// $this->isReloadingOut = FALSE;
// $this->md5Map = [];
} }
/** /**
@@ -211,14 +237,18 @@ class ServerInotify extends Process
{ {
$this->isReloading = true; $this->isReloading = true;
$this->trigger_reload(); $this->trigger_reload();
$this->int = -1;
$this->loadDirs(); $this->exit(0);
//
$this->isReloading = FALSE; //
$this->isReloadingOut = FALSE; // $this->int = -1;
//
$this->tick(); // $this->loadDirs();
//
// $this->isReloading = FALSE;
// $this->isReloadingOut = FALSE;
//
// $this->tick();
} }