Seldaek / Signal Handler
Licence: mit
Simple unix signal handler that silently fails on windows for easy cross-platform development
Stars: ✭ 99
Signal Handler
A simple cross-platform1 signal handler.
1 It does not actually work on windows as the pcntl extension is not available, but it will silently fail to work so you can use it without caring for windows, and you can use signal names as strings to avoid undefined constant errors on windows. So it is a best-effort cross-platform that does not get in your way.
Usage
Default usage, listen to SIGTERM, 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()) {
break;
}
}
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 it will work 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();
}
}
PSR-3 Logger will make it log ->info('Received '.$signalName)
Passing in a 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 ($signal, $signalName) {
echo 'Received ' . $signalName . PHP_EOL;
});
Installation
For a quick install with Composer use:
$ composer require seld/signal-handler
Requirements
- PHP 5.4+
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub
Author
Jordi Boggiano - [email protected] - http://twitter.com/seldaek
License
signal-handler is licensed under the MIT License - see the LICENSE file for details
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].