承接 seld/signal-handler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

seld/signal-handler

Composer 安装命令:

composer require seld/signal-handler

包简介

Simple unix signal handler that silently fails where signals are not supported for easy cross-platform development

README 文档

README

A simple cross-platform1 signal handler.

1 Windows support is only possible on PHP 7.4+ and only supports catching SIGINT/SIGBREAK (ctrl+c / ctrl+break respectively). On older PHP versions it will simply silently fail to work on Windows so you can use it but signals will just interrupt PHP as if the library was not in use.

Continuous Integration

Usage

Note: To maximize cross-platform support all signals are available as constants on the SignalHandler class and it is recommended to use those instead of the PHP constants as the constants are not available on all platforms.

Default usage, listen to SIGTERM and SIGINT (i.e. Ctrl+C / ^C interrupts)

use Seld\Signal\SignalHandler;

$signal = SignalHandler::create();

while (true) {
    // do some work here ...

    // exit gracefully at the end of an iteration if the process interruption was called for
    if ($signal->isTriggered()) {
        $signal->exitWithLastSignal();
    }
}

Nesting/stacking and unregistering signal handlers

If you create multiple SignalHandler instances they will be kept on a stack and only the top-most / last created one will be triggered when a signal comes in.

When you unregister the top one the previous one becomes active again, etc.

For this to work well however you need to make sure you properly unregister the signal handler when you are done with it. On PHP 8.0+ this is done automatically whenever the signal handler is garbage collected as the stack uses WeakReference instances to keep track of the handlers. On PHP 7 however you need to call $signal->unregister(); explicitly to remove it from the global handler stack.

Typically it would look something like this if you need PHP 7 support:

use Seld\Signal\SignalHandler;

$signal = SignalHandler::create();

try {
    // do things
} finally {
    $signal->unregister();
}

Listen to custom signals and reset the handler to handle the same signal multiple times

use Seld\Signal\SignalHandler;

// using strings for the constant names makes sure the code will run on Windows and
// OSX even if the signal is missing on those platforms
$signal = SignalHandler::create([SignalHandler::SIGHUP, SignalHandler::SIGUSR1]);

while (true) {
    // do some work here ...

    // reload the config when the signal was triggered
    if ($signal->isTriggered()) {
        $this->reloadConfiguration();

        // reset the handler so next time you check isTriggered() it
        // will be false, until SIGHUP/SIGUSR1 is signaled again
        $signal->reset();
    }
}

Passing in a PSR-3 Logger will make it log ->info('Received '.$signalName)

use Seld\Signal\SignalHandler;

$signal = SignalHandler::create(null, new PSR3Logger());

Passing in a callback you can react to the signal as well

use Seld\Signal\SignalHandler;

$signal = SignalHandler::create([SignalHandler::SIGINT], function (string $signalName, SignalHandler $self) {
    echo 'Received ' . $signalName . PHP_EOL;

    // you can optionally receive the SignalHandler instance as second arg to do things on it like
    // resetting its state or exiting to handle the signal
    $self->reset();
    $self->exitWithLastSignal();
});

Warning: As described above on PHP 8.0+ this library uses weak references to keep track of the handlers, so if you do not store the result of ::create() into a variable that you keep around as long as you need the handler, it will not work and your callback function will never be called.

Installation

For a quick install with Composer use:

$ composer require seld/signal-handler

Requirements

  • PHP 7.2+ (or PHP 5.4+ for v1 which is EOL now)

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub

Author

Jordi Boggiano - j.boggiano@seld.be - http://twitter.com/seldaek

License

signal-handler is licensed under the MIT License - see the LICENSE file for details

seld/signal-handler 适用场景与选型建议

seld/signal-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.22M 次下载、GitHub Stars 达 182, 最近一次更新时间为 2015 年 09 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「signal」 「unix」 「posix」 「sigint」 「sigterm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 seld/signal-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 seld/signal-handler 我们能提供哪些服务?
定制开发 / 二次开发

基于 seld/signal-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 79.22M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 183
  • 点击次数: 27
  • 依赖项目数: 21
  • 推荐数: 0

GitHub 信息

  • Stars: 182
  • Watchers: 5
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-15