kafkiansky/signaler

Signaler library that provide stack for signal listeners.

v0.1.0 2022-03-24 10:22 UTC

This package is auto-updated.

Last update: 2024-10-24 15:52:15 UTC


README

test Software License Total Downloads

Contents

Installation

composer require kafkiansky/signaler

Usage

Simple example with \SIGINT signal.

use Kafkiansky\Signaler\SeldSignalFactory;
use Psr\Log\NullLogger;

$factory = new SeldSignalFactory(new NullLogger());

$signaler = $factory->subscribe([
    \SIGINT => function () use ($worker): void {
        $worker->stop();
    }
]);

while ($signaler->isTriggered() === false) {
    //
}

The main purpose of this library is to prevent the signal listener from being replaced by the pcntl_signal function if it was previously configured by vendor code. The library carefully saves previous signal listeners and will call them after yours.

In e.g.:

use Kafkiansky\Signaler\SeldSignalFactory;
use Psr\Log\NullLogger;

pcntl_signal(\SIGINT, function (): void {
    // This function will still be called after all your listeners.
});

$factory = new SeldSignalFactory(new NullLogger());

$signaler = $factory->subscribe([
    \SIGINT => function () use ($worker): void {
        $worker->stop();
    }
]);

while ($signaler->isTriggered() === false) {
    //
}

Testing

$ composer test

License

The MIT License (MIT). See License File for more information.