2020-06-09 20:29:48 +08:00
|
|
|
|
# 信號處理器
|
2020-06-04 14:13:37 +08:00
|
|
|
|
|
2022-12-27 13:37:04 +08:00
|
|
|
|
信號處理器會監聽 `Worker` 進程和 `自定義` 進程啓動後,自動註冊到信號管理器中。
|
2020-06-04 14:13:37 +08:00
|
|
|
|
|
2020-06-09 20:29:48 +08:00
|
|
|
|
## 安裝
|
2020-06-04 14:13:37 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
composer require hyperf/signal
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-27 15:52:51 +08:00
|
|
|
|
## 發佈配置
|
|
|
|
|
|
|
|
|
|
您可以通過下面的命令來發布默認的配置文件到您的項目中:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
php bin/hyperf.php vendor:publish hyperf/signal
|
|
|
|
|
```
|
|
|
|
|
|
2020-06-09 20:29:48 +08:00
|
|
|
|
## 添加處理器
|
2020-06-04 14:13:37 +08:00
|
|
|
|
|
2020-06-09 20:29:48 +08:00
|
|
|
|
以下我們監聽 `Worker` 進程的 `SIGTERM` 信號,當收到信號後,打印出信號值。
|
2020-06-04 14:13:37 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Signal;
|
|
|
|
|
|
|
|
|
|
use Hyperf\Signal\Annotation\Signal;
|
|
|
|
|
use Hyperf\Signal\SignalHandlerInterface;
|
|
|
|
|
|
2021-12-01 16:19:47 +08:00
|
|
|
|
#[Signal]
|
2020-06-04 14:13:37 +08:00
|
|
|
|
class TermSignalHandler implements SignalHandlerInterface
|
|
|
|
|
{
|
|
|
|
|
public function listen(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
[SignalHandlerInterface::WORKER, SIGTERM],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(int $signal): void
|
|
|
|
|
{
|
|
|
|
|
var_dump($signal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-27 15:52:51 +08:00
|
|
|
|
因為 Worker 進程接收的 SIGTERM 信號被捕獲後,無法正常退出,所以用户可以直接 `Ctrl + C` 退出,或者修改 `config/autoload/signal.php` 配置,如下:
|
2020-06-04 14:13:37 +08:00
|
|
|
|
|
2020-08-31 13:40:33 +08:00
|
|
|
|
> WorkerStopHandler 不適配於 CoroutineServer,如有需要請自行實現
|
|
|
|
|
|
2020-06-04 14:13:37 +08:00
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'handlers' => [
|
|
|
|
|
Hyperf\Signal\Handler\WorkerStopHandler::class => PHP_INT_MIN
|
|
|
|
|
],
|
|
|
|
|
'timeout' => 5.0,
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
2020-07-27 15:52:51 +08:00
|
|
|
|
`WorkerStopHandler` 觸發後,會在所設置的 [max_wait_time](https://wiki.swoole.com/#/server/setting?id=max_wait_time) 配置時間後,關閉掉當前進程。
|
2020-08-31 13:40:33 +08:00
|
|
|
|
|
|
|
|
|
## 協程風格服務監聽器配置示例
|
|
|
|
|
|
|
|
|
|
> 以上默認的監聽器都是適配於異步風格服務的,如果需要在協程風格服務下使用,可以按照以下自定義配置
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Kernel\Signal;
|
|
|
|
|
|
|
|
|
|
use Hyperf\AsyncQueue\Driver\Driver;
|
|
|
|
|
use Hyperf\Contract\ConfigInterface;
|
|
|
|
|
use Hyperf\Process\ProcessManager;
|
|
|
|
|
use Hyperf\Server\ServerManager;
|
|
|
|
|
use Hyperf\Signal\SignalHandlerInterface;
|
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
|
|
|
|
|
class CoroutineServerStopHandler implements SignalHandlerInterface
|
|
|
|
|
{
|
2021-12-01 16:19:47 +08:00
|
|
|
|
|
|
|
|
|
protected ContainerInterface $container;
|
|
|
|
|
|
|
|
|
|
protected ConfigInterface $config;
|
2020-08-31 13:40:33 +08:00
|
|
|
|
|
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
|
{
|
|
|
|
|
$this->container = $container;
|
|
|
|
|
$this->config = $container->get(ConfigInterface::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function listen(): array
|
|
|
|
|
{
|
|
|
|
|
// 協程風格只會存在一個 Worker 進程,故這裏只需要監聽 WORKER 即可
|
|
|
|
|
return [
|
|
|
|
|
[self::WORKER, SIGTERM],
|
|
|
|
|
[self::WORKER, SIGINT],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(int $signal): void
|
|
|
|
|
{
|
|
|
|
|
ProcessManager::setRunning(false);
|
|
|
|
|
|
|
|
|
|
foreach (ServerManager::list() as [$type, $server]) {
|
2022-12-27 13:37:04 +08:00
|
|
|
|
// 循環關閉開啓的服務
|
2020-08-31 13:40:33 +08:00
|
|
|
|
$server->shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|