This commit is contained in:
2021-12-06 17:58:11 +08:00
parent 94a522588d
commit c19e141f68
2 changed files with 43 additions and 5 deletions
+29 -1
View File
@@ -3,7 +3,7 @@
namespace Server\Abstracts;
use JetBrains\PhpStorm\Pure;
use Kiri\Context;
use Server\Contract\OnProcessInterface;
use Swoole\Coroutine;
use Swoole\Process;
@@ -80,4 +80,32 @@ abstract class BaseProcess implements OnProcessInterface
}
/**
*
*/
public function onSigterm(): static
{
if (!Context::inCoroutine()) {
Process::signal(SIGTERM, fn($data) => $this->onShutdown($data));
} else {
Coroutine::create(function () {
$data = Coroutine::waitSignal(SIGTERM, -1);
if ($data) {
$this->onShutdown($data);
}
});
}
return $this;
}
/**
* @param $data
*/
protected function onShutdown($data): void
{
$this->isStop = true;
}
}
+14 -4
View File
@@ -67,6 +67,9 @@ class ServerManager extends Component
private Server|null $server = null;
protected array $initProcesses = [];
const DEFAULT_EVENT = [
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
@@ -160,14 +163,23 @@ class ServerManager extends Component
if (Kiri::getPlatform()->isLinux()) {
$process->name($system . '(' . $customProcess->getName() . ')');
}
$customProcess->process($process);
$customProcess->onSigterm()->process($process);
}, $customProcess->getRedirectStdinAndStdout(), $customProcess->getPipeType(), $customProcess->isEnableCoroutine());
$this->logger->debug($system . ' ' . $customProcess->getName() . ' start.');
$this->container->setBindings($customProcess->getName(), $process);
$this->initProcesses[$customProcess->getName()] = $process;
$this->server->addProcess($process);
}
/**
* @return array<string,Process>
*/
public function getProcesses(): array
{
return $this->initProcesses;
}
/**
* @return array
*/
@@ -346,7 +358,6 @@ class ServerManager extends Component
* @param array $events
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
private function addTaskListener(array $events = []): void
{
@@ -365,7 +376,6 @@ class ServerManager extends Component
* @param array|null $settings
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function bindCallback(?array $settings = [])
{