信号处理器
目录
安装依赖
composer require hyperf/signal
发布配置
config/autoload/signal.php
<?php
declare(strict_types=1);
return [
'handlers' => [
// 这里我们自己实现, 所以注释
// Hyperf\Signal\Handler\WorkerStopHandler::class => PHP_INT_MIN,
],
// 收到信号后等待时间
'timeout' => 5.0,
];
注册信号处理器
自定义进程信号处理器
<?php
declare(strict_types=1);
namespace App\Signal;
use Hyperf\Process\ProcessManager;
use Hyperf\Signal\Annotation\Signal;
use Hyperf\Signal\SignalHandlerInterface;
#[Signal]
class ProcessStopHandler implements SignalHandlerInterface
{
public function listen(): array
{
return [
[self::PROCESS, SIGTERM],
];
}
public function handle(int $signal): void
{
ProcessManager::setRunning(false);
}
}
自定义work进程信号处理器
<?php
declare(strict_types=1);
namespace App\Signal;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Signal\Annotation\Signal;
use Hyperf\Signal\SignalHandlerInterface;
use Psr\Container\ContainerInterface;
use Swoole\Server;
#[Signal]
class WorkerStopHandler implements SignalHandlerInterface
{
protected ConfigInterface $config;
public function __construct(protected ContainerInterface $container)
{
$this->config = $container->get(ConfigInterface::class);
}
public function listen(): array
{
return [
[self::WORKER, SIGTERM],
[self::WORKER, SIGINT],
];
}
public function handle(int $signal): void
{
if ($signal !== SIGINT) {
$time = $this->config->get('server.settings.max_wait_time', 3);
Coroutine::sleep($time);
}
// shutdown => https://wiki.swoole.com/#/server/methods?id=shutdown 直接kill -15 不触发后续动作
// stop => https://wiki.swoole.com/#/server/methods?id=stop 使当前 Worker 进程停止运行,并立即触发 onWorkerStop 回调函数。
// $this->container->get(Server::class)->shutdown();
$this->container->get(Server::class)->stop(-1, false);
}
}