Skip to content

Commit

Permalink
Update signal handling documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Dec 3, 2017
1 parent 742e5c6 commit d431fa7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,10 @@ See also [example #3](examples).
### addSignal()

The `addSignal(int $signal, callable $listener): void` method can be used to
be notified about OS signals. This is useful to catch user interrupt signals or
shutdown signals from tools like `supervisor` or `systemd`.
register a listener to be notified when a signal has been caught by this process.

This is useful to catch user interrupt signals or shutdown signals from
tools like `supervisor` or `systemd`.

The listener callback function MUST be able to accept a single parameter,
the signal added by this method or you MAY use a function which
Expand All @@ -326,32 +328,32 @@ no effect, so for performance reasons you're recommended to not return
any excessive data structures.

```php
$listener = function (int $signal) {
echo 'Caught user iterrupt signal', PHP_EOL;
};
$loop->addSignal(SIGINT, $listener);
$loop->addSignal(SIGINT, function (int $signal) {
echo 'Caught user interrupt signal' . PHP_EOL;
});
```

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.**
Signaling is only available on Unix-like platform, Windows isn't
supported due to operating system limitations.
This method may throw a `BadMethodCallException` if signals aren't
supported on this platform, for example when required extensions are
missing.

**Note: Signaling is only available on Unix-like platform, Windows isn't supported due
to limitations from underlying signal handlers.**
**Note: A listener can only be added once to the same signal, any
attempts to add it more then once will be ignored.**

### removeSignal()

The `removeSignal(int $signal, callable $listener): void` removes a previously added
signal listener.

Any attempts to remove listeners that aren't registerred will be ignored.
The `removeSignal(int $signal, callable $listener): void` method can be used to
remove a previously added signal listener.

```php
$loop->removeSignal(SIGINT, $listener);
```

See also [example #4](examples).
Any attempts to remove listeners that aren't registered will be ignored.

### addReadStream()

Expand Down
47 changes: 35 additions & 12 deletions src/LoopInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,32 +317,55 @@ public function isTimerActive(TimerInterface $timer);
public function futureTick(callable $listener);

/**
* Registers a signal listener with the loop, which
* on it's turn registers it with a signal handler
* suitable for the loop implementation.
* Register a listener to be notified when a signal has been caught by this process.
*
* A listener can only be added once, any attempts
* to add it again will be ignored.
* This is useful to catch user interrupt signals or shutdown signals from
* tools like `supervisor` or `systemd`.
*
* The listener callback function MUST be able to accept a single parameter,
* the signal added by this method or you MAY use a function which
* has no parameters at all.
*
* The listener callback function MUST NOT throw an `Exception`.
* The return value of the listener callback function will be ignored and has
* no effect, so for performance reasons you're recommended to not return
* any excessive data structures.
*
* ```php
* $loop->addSignal(SIGINT, function (int $signal) {
* echo 'Caught user interrupt signal' . PHP_EOL;
* });
* ```
*
* See also [example #4](examples).
*
* Signaling is only available on Unix-like platform, Windows isn't
* supported due to operating system limitations.
* This method may throw a `BadMethodCallException` if signals aren't
* supported on this platform, for example when required extensions are
* missing.
*
* **Note: A listener can only be added once to the same signal, any
* attempts to add it more then once will be ignored.**
*
* @param int $signal
* @param callable $listener
*
* @throws \BadMethodCallException when signals
* aren't supported by the loop, e.g. when required
* extensions are missing.
* @throws \BadMethodCallException when signals aren't supported on this
* platform, for example when required extensions are missing.
*
* @return void
*/
public function addSignal($signal, callable $listener);

/**
* Removed previous registered signal listener from
* the loop, which on it's turn removes it from the
* underlying signal handler.
* Removes a previously added signal listener.
*
* See also [example #4](examples).
* ```php
* $loop->removeSignal(SIGINT, $listener);
* ```
*
* Any attempts to remove listeners that aren't registered will be ignored.
*
* @param int $signal
* @param callable $listener
Expand Down

0 comments on commit d431fa7

Please sign in to comment.