Jerry's WIKIJerry's WIKI
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
Overview
  • ๐Ÿž Web
  • ๐Ÿ“ Components
  • ๐Ÿ’ก Skills
  • ๐ŸŽฑ Specification
  • ๐Ÿ–ฅ Workflows
  • ๐Ÿ›  Tools
  • ๐ŸŒ๏ธ Linux
  • ๐Ÿ“ฆ Container
  • โ™จ๏ธ Language
Coffee
  • ็ฎ€ไฝ“ไธญๆ–‡
  • English
GitHub
  • ๐Ÿ“ž Event Mechanism

    • Event Roles and Considerations
    • Code Example
  • โฐ Crontab
  • โ›“ Processes
  • ๐Ÿ“ File System
  • ๐Ÿ•“ Cache
  • ๐Ÿ“ฉ Queue

    • Queue Usage
    • Notes
  • ๐Ÿšฆ Signal
  • ๐Ÿ“ค GuzzleHttp
  • ๐Ÿ“‰ Rate Limiter
  • โŒ Exception
  • ๐Ÿ–จ Logs
  • ๐Ÿ“ก Command
  • ๐Ÿ” WebSocket

Signal

Index

  • Install Dependencies
  • Publish Config
  • Register Signal Handler
    • Custom Process Signal Handler
    • Custom Work Process Signal Handler

Install Dependencies

Standard Library Address

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);
    }
}

Edit this page
Update At:
Contributor: ็”ฐๆœๅธ†
Prev
Queue Notes
Next
GuzzleHttp