Conversation
| public function on($signal, callable $listener) | ||
| { | ||
| if (!$this->enabled) { | ||
| return; |
| } | ||
|
|
||
| if ($this->timer === null) { | ||
| $this->timer = $this->loop->addPeriodicTimer(0.1, function () { |
| public function testSignal() | ||
| { | ||
| if (!extension_loaded('pcntl')) { | ||
| $this->markTestSkipped('Signal test skipped because PCNTL extension isn\'t loaded.'); |
| $this->loop = new EventLoop(); | ||
| $this->futureTickQueue = new FutureTickQueue(); | ||
| $this->timerEvents = new SplObjectStorage(); | ||
| $this->signals = new Pcntl($this); |
| be notified about OS signals. This is useful to catch user interrupt signals or | ||
| shutdown signals from tools like `supervisor` or `systemd`. | ||
|
|
||
| The |
| * @param callable $listener | ||
| * | ||
| * @return void | ||
| */ |
| * @param callable $listener | ||
| * | ||
| * @return void | ||
| */ |
| public function on($signal, callable $listener) | ||
| { | ||
| if (!$this->enabled) { | ||
| return; |
| } | ||
|
|
||
| if ($this->timer === null) { | ||
| $this->timer = $this->loop->addPeriodicTimer(0.1, function () { |
| }); | ||
|
|
||
| $this->loop->futureTick(function () { | ||
| posix_kill(posix_getpid(), SIGUSR1); |
| $this->signalEvents[$signal]->add(); | ||
| }); | ||
| $this->signals->on('off', function ($signal) { | ||
| $this->futureTick(function () use ($signal) { |
| "require": { | ||
| "php": ">=5.4.0" | ||
| "php": ">=5.4.0", | ||
| "evenement/evenement": "^3.0||^2.0" |
|
|
||
| **Note: A listener can be added more then once to the same signal and it will be called | ||
| for each time it is added. But on [`removeSignal`](#removeSignal) it is removed for all | ||
| the times it has been added. |
| "ext-event": "~1.0", | ||
| "ext-libev": "*" | ||
| "ext-libev": "*", | ||
| "ext-pcntl": "For signals support" |
| * @param callable $listener | ||
| * | ||
| * @throws UnsupportedFeatureException when signals | ||
| * aren't supported by the loop, e.g. when required |
| $available = $this->streamSelect($read, $write, $timeout); | ||
| if ($this->pcntl) { | ||
| \pcntl_signal_dispatch(); | ||
| } |
| if (isset($this->signals[$signal]) && \count($this->signals[$signal]) === 0) { | ||
| unset($this->signals[$signal]); | ||
| $this->emit('off', [$signal]); | ||
| } |
| $this->signals->on('on', function ($signal) { | ||
| \pcntl_signal($signal, function ($signal) { | ||
| $this->signals->call($signal); | ||
| }); |
jsor approved these changes Aug 29, 2017
Open
|
|
||
| $loop = React\EventLoop\Factory::create(); | ||
|
|
||
| $loop->onSignal(SIGINT, $func = function ($signal) use ($loop, &$func) { |
| $this->signalEvents[$signal]->add(); | ||
| }, | ||
| function ($signal) { | ||
| $this->futureTick(function () use ($signal) { |
| * @param callable $listener | ||
| * | ||
| * @throws UnsupportedFeatureException when signals | ||
| * aren't supported by the loop, e.g. when required |
| { | ||
| return new self('Event loop feature "' . $feature . '" isn\'t supported by the "' . $loopName . '" event loop'); | ||
| } | ||
| } |
|
|
||
| final class SignalsHandlerTest extends TestCase | ||
| { | ||
| public function testEmittedEventsAndCallHandling() |
| { | ||
| public static function create($feature, $loopName) | ||
| { | ||
| return new self('Event loop feature "' . $feature . '" isn\'t supported by the "' . $loopName . '" event loop'); |
|
|
||
| ### addSignal() | ||
|
|
||
| **Note: Signaling is only available on Unix-like platform, Windows isn't supported.** |
| See also [example #4](examples). | ||
|
|
||
| **Note: A listener can only be added once to the same signal, any attempts to add it | ||
| more then once will be ignored.** |
| $this->loop->addTimer(0.1, function () {}); | ||
|
|
||
| $this->assertRunFasterThan(0.2); | ||
| } |
| $loop->stop(); | ||
| }); | ||
|
|
||
| // This timer is here to keep the loop alive, signaling by itself doesn't do that |
| $loop->addSignal(SIGINT, $func = function ($signal) use ($loop, &$func) { | ||
| echo 'Signal: ', (string)$signal, PHP_EOL; | ||
| $loop->removeSignal(SIGINT, $func); | ||
| $loop->stop(); |
| { | ||
| if (count($this->signals) == 0 && $this->timer === null) { | ||
| $this->timer = $this->loop->addPeriodicTimer(1, function () {}); | ||
| } |
WyriHaximus added a commit to WyriHaximus-labs/event-loop that referenced this pull request
Nov 4, 2017WyriHaximus added a commit to WyriHaximus-labs/event-loop that referenced this pull request
Nov 4, 2017clue approved these changes Nov 10, 2017
clue
changed the title
OS signaling
Add support for signal handling
clue
mentioned this pull request
Closed
This was referenced
Feb 6, 2018Merged
Merged
cebe
mentioned this pull request
Closed
TBK
mentioned this pull request
Closed
clue
mentioned this pull request
Closed