Enum
StopReason
Why a worker stops, see WorkerStoppingException.
Class synopsis#
final enum StopReason implements \UnitEnum {
}Constants#
| Name | Value | Description |
|---|---|---|
Shutdown | StopReason::Shutdown | swerve stops: SIGTERM, Ctrl+C, or the master died. Tell clients the server is going away. |
Reload | StopReason::Reload | swerve restarts with new code. A WebSocket closes with 1012 (Service Restart): reconnect. |
Recycle | StopReason::Recycle | Only this worker is replaced (--max-requests, --max-memory); the server goes on. |
Properties#
| Name | Type | Description |
|---|---|---|
$name | readonly string |
Examples#
use Swerve\ClientRequest;
use Swerve\RequestHandler;
use Swerve\StopReason;
use Swerve\WorkerStoppingException;
$app = new RequestHandler(function (ClientRequest $request) {
$request->write("waiting for news\n");
try {
phasync::sleep(30);
} catch (WorkerStoppingException $e) {
phasync::shielded(fn () => $request->write(match ($e->reason) {
StopReason::Shutdown => "the server is going away\n",
StopReason::Reload => "new code is starting: reconnect\n",
StopReason::Recycle => "this worker is replaced: reconnect\n",
}));
}
});
if (debug_backtrace()) {
return $app; // swerve loads this file: it is the application's swerve.php
}
// Run directly: serve the application with swerve, and send it requests
require __DIR__ . '/../serve.php';
serve(__FILE__, ["GET /news HTTP/1.1\r\nHost: example\r\n\r\n"], stop: SIGHUP); // a reload, once it answersOutput
HTTP/1.1 200 OK Server: Swerve Transfer-Encoding: chunked Connection: close 11 waiting for news 20 new code is starting: reconnect 0