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
+10
View File
@@ -27,6 +27,15 @@ class Annotation extends Component
} }
/**
* @return Loader
*/
public function getLoader(): Loader
{
return $this->_loader;
}
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
@@ -64,6 +73,7 @@ class Annotation extends Component
/** /**
* @param string $dir * @param string $dir
* @throws Exception
*/ */
public function instanceDirectoryFiles(string $dir) public function instanceDirectoryFiles(string $dir)
{ {
+13 -5
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;
@@ -37,10 +39,12 @@ class OnWorkerStart extends Callback
name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker'); name($server->worker_pid, $worker_id >= $server->setting['worker_num'] ? 'task' : 'worker');
$loader = Snowflake::app()->get(ServerInotify::class);
if ($worker_id >= $server->setting['worker_num']) { if ($worker_id >= $server->setting['worker_num']) {
$this->onTask($server, $worker_id); $this->onTask($server, $worker_id, $loader);
} else { } else {
$this->onWorker($server, $worker_id); $this->onWorker($server, $worker_id, $loader);
} }
} }
@@ -50,12 +54,14 @@ class OnWorkerStart extends Callback
* @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);
$loader->loadByDirectory(MODEL_PATH);
fire(Event::SERVER_TASK_START); fire(Event::SERVER_TASK_START);
} }
@@ -65,15 +71,17 @@ class OnWorkerStart extends Callback
* @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);
$loader->loadByDirectory(APP_PATH);
try { try {
fire(Event::SERVER_WORKER_START, [$worker_id]); fire(Event::SERVER_WORKER_START, [$worker_id]);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->addError($exception,'throwable'); $this->addError($exception, 'throwable');
write($exception->getMessage(), 'worker'); 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();
} }