Signal
Index
Install Dependencies
composer require hyperf/signal
Publish Config
config/autoload/signal.php
<?php
declare(strict_types=1);
return [
'handlers' => [
// ่ฟ้ๆไปฌ่ชๅทฑๅฎ็ฐ, ๆไปฅๆณจ้
// Hyperf\Signal\Handler\WorkerStopHandler::class => PHP_INT_MIN,
],
// ๆถๅฐไฟกๅทๅ็ญๅพ
ๆถ้ด
'timeout' => 5.0,
];
Register Signal Handler
Custom Process Signal Handler
<?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);
}
}
Custom Work Process Signal Handler
<?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);
}
}