eee
This commit is contained in:
@@ -67,6 +67,8 @@ class AsyncServer extends Component implements ServerInterface
|
||||
}
|
||||
$this->shuttingDown = true;
|
||||
|
||||
$this->terminateProcesses();
|
||||
|
||||
if ($this->server !== null) {
|
||||
$this->server->shutdown();
|
||||
}
|
||||
|
||||
+76
-18
@@ -13,6 +13,8 @@ use Kiri\Server\Processes\OnProcessInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Event;
|
||||
use Swoole\Process;
|
||||
use const SIGKILL;
|
||||
use const SIGTERM;
|
||||
use const SIGUSR1;
|
||||
|
||||
class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
@@ -35,6 +37,7 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
private array $extensions = ['php'];
|
||||
private string $strategy;
|
||||
private bool $running = false;
|
||||
private bool $scanning = false;
|
||||
|
||||
private $inotifyFd = null;
|
||||
private array $inotifyWatchMap = [];
|
||||
@@ -132,6 +135,10 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
|
||||
public function process(Process|null $process): void
|
||||
{
|
||||
if (class_exists('\\Swoole\\Coroutine')) {
|
||||
\Swoole\Coroutine::set(['enable_deadlock_check' => false]);
|
||||
}
|
||||
|
||||
if ($this->running) {
|
||||
return;
|
||||
}
|
||||
@@ -163,20 +170,14 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
$this->inotifyFd = null;
|
||||
}
|
||||
|
||||
if (isset($this->fswatchPipes[1]) && is_resource($this->fswatchPipes[1])) {
|
||||
@Event::del($this->fswatchPipes[1]);
|
||||
@fclose($this->fswatchPipes[1]);
|
||||
}
|
||||
|
||||
if (isset($this->fswatchPipes[2]) && is_resource($this->fswatchPipes[2])) {
|
||||
@fclose($this->fswatchPipes[2]);
|
||||
}
|
||||
|
||||
if ($this->fswatchProcess && is_resource($this->fswatchProcess)) {
|
||||
@proc_terminate($this->fswatchProcess);
|
||||
@proc_close($this->fswatchProcess);
|
||||
$this->fswatchProcess = null;
|
||||
foreach ($this->fswatchPipes as $pipe) {
|
||||
if (is_resource($pipe)) {
|
||||
@Event::del($pipe);
|
||||
@fclose($pipe);
|
||||
}
|
||||
}
|
||||
$this->fswatchPipes = [];
|
||||
$this->stopFswatchProcess();
|
||||
|
||||
if ($this->pollTimer) {
|
||||
\Swoole\Timer::clear($this->pollTimer);
|
||||
@@ -191,6 +192,43 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
@Event::exit();
|
||||
}
|
||||
|
||||
private function stopFswatchProcess(): void
|
||||
{
|
||||
if (!$this->fswatchProcess || !is_resource($this->fswatchProcess)) {
|
||||
$this->fswatchProcess = null;
|
||||
return;
|
||||
}
|
||||
|
||||
$status = @proc_get_status($this->fswatchProcess);
|
||||
if (($status['running'] ?? false) === true) {
|
||||
@proc_terminate($this->fswatchProcess, SIGTERM);
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
usleep(100000);
|
||||
$status = @proc_get_status($this->fswatchProcess);
|
||||
if (($status['running'] ?? false) !== true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$status = @proc_get_status($this->fswatchProcess);
|
||||
if (($status['running'] ?? false) === true) {
|
||||
@proc_terminate($this->fswatchProcess, SIGKILL);
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
usleep(100000);
|
||||
$status = @proc_get_status($this->fswatchProcess);
|
||||
if (($status['running'] ?? false) !== true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$status = @proc_get_status($this->fswatchProcess);
|
||||
if (($status['running'] ?? false) !== true) {
|
||||
@proc_close($this->fswatchProcess);
|
||||
}
|
||||
$this->fswatchProcess = null;
|
||||
}
|
||||
private function isExcluded(string $path): bool
|
||||
{
|
||||
foreach ($this->excludePatterns as $pattern) {
|
||||
@@ -377,6 +415,10 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
}
|
||||
|
||||
Event::add($this->inotifyFd, function ($fd) {
|
||||
if (!$this->running) {
|
||||
return;
|
||||
}
|
||||
|
||||
$events = inotify_read($fd);
|
||||
if ($events === false) {
|
||||
return;
|
||||
@@ -466,6 +508,10 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
stream_set_blocking($this->fswatchPipes[1], false);
|
||||
|
||||
Event::add($this->fswatchPipes[1], function ($pipe) {
|
||||
if (!$this->running) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = fread($pipe, 8192);
|
||||
if ($data === false || $data === '') {
|
||||
return;
|
||||
@@ -486,13 +532,25 @@ class FileWatcher extends AbstractProcess implements OnProcessInterface
|
||||
|
||||
$intervalMs = $this->pollInterval * 1000;
|
||||
$this->pollTimer = \Swoole\Timer::tick($intervalMs, function () {
|
||||
$newSnapshot = [];
|
||||
$changedFiles = $this->compareSnapshots($newSnapshot);
|
||||
if (!empty($changedFiles)) {
|
||||
$this->triggerCallback($changedFiles);
|
||||
if (!$this->running || $this->scanning) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fileSnapshot = $newSnapshot;
|
||||
$this->scanning = true;
|
||||
try {
|
||||
$newSnapshot = [];
|
||||
$changedFiles = $this->compareSnapshots($newSnapshot);
|
||||
if (!$this->running) {
|
||||
return;
|
||||
}
|
||||
if (!empty($changedFiles)) {
|
||||
$this->triggerCallback($changedFiles);
|
||||
}
|
||||
|
||||
$this->fileSnapshot = $newSnapshot;
|
||||
} finally {
|
||||
$this->scanning = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+11
-1
@@ -76,10 +76,20 @@ class HotReload extends AbstractProcess
|
||||
*/
|
||||
public function onSigterm(): void
|
||||
{
|
||||
// TODO: Implement onSigterm() method.
|
||||
$this->stop();
|
||||
}
|
||||
|
||||
|
||||
public function stop(): void
|
||||
{
|
||||
parent::stop();
|
||||
if (isset($this->pipe) && is_resource($this->pipe)) {
|
||||
@Event::del($this->pipe);
|
||||
@fclose($this->pipe);
|
||||
}
|
||||
@Event::exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ?Process $process
|
||||
*/
|
||||
|
||||
@@ -49,9 +49,7 @@ trait TraitServer
|
||||
$handling = true;
|
||||
|
||||
try {
|
||||
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
|
||||
$masterPid = is_file($pidFile) ? (int)trim((string)file_get_contents($pidFile)) : 0;
|
||||
if ($masterPid > 1 && $masterPid !== getmypid()) {
|
||||
if (!$this->shouldHandleSigint()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -62,6 +60,81 @@ trait TraitServer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function shouldHandleSigint(): bool
|
||||
{
|
||||
$currentPid = getmypid();
|
||||
if ($this->isMasterProcess($currentPid)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$masterPid = $this->getVerifiedPidFileMasterPid();
|
||||
return $masterPid !== null && $masterPid === $currentPid;
|
||||
}
|
||||
|
||||
|
||||
private function getVerifiedPidFileMasterPid(): ?int
|
||||
{
|
||||
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
|
||||
if (!is_file($pidFile)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pid = (int)trim((string)file_get_contents($pidFile));
|
||||
if ($pid <= 1 || !$this->isProcessAlive($pid) || !$this->isSameAppProcess($pid) || !$this->isMasterProcess($pid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $pid;
|
||||
}
|
||||
|
||||
|
||||
private function isMasterProcess(int $pid): bool
|
||||
{
|
||||
$command = $this->getProcessCommand($pid);
|
||||
if ($command === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prefix = '[' . config('site.id', 'system-service') . '].Master[';
|
||||
return str_contains($command, $prefix);
|
||||
}
|
||||
|
||||
|
||||
private function getProcessCommand(int $pid): string
|
||||
{
|
||||
$command = @file_get_contents('/proc/' . $pid . '/cmdline');
|
||||
if (!is_string($command) || $command === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return str_replace("\0", ' ', $command);
|
||||
}
|
||||
|
||||
|
||||
private function isSameAppProcess(int $pid): bool
|
||||
{
|
||||
$cwd = @readlink('/proc/' . $pid . '/cwd');
|
||||
if ($cwd === false || $cwd === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->normalizePath($cwd) === $this->normalizePath(APP_PATH);
|
||||
}
|
||||
|
||||
|
||||
private function normalizePath(string $path): string
|
||||
{
|
||||
$real = realpath($path) ?: $path;
|
||||
return rtrim(str_replace('\\', '/', $real), '/');
|
||||
}
|
||||
|
||||
|
||||
private function isProcessAlive(int $pid): bool
|
||||
{
|
||||
return $pid > 1 && @Process::kill($pid, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $signal
|
||||
* @param $callback
|
||||
|
||||
Reference in New Issue
Block a user