phasync
Production / StopReason

Enum

StopReason

Why a worker stops, see WorkerStoppingException.

Class synopsis#

final enum StopReason implements \UnitEnum {

}

Constants#

NameValueDescription
ShutdownStopReason::Shutdownswerve stops: SIGTERM, Ctrl+C, or the master died. Tell clients the server is going away.
ReloadStopReason::Reloadswerve restarts with new code. A WebSocket closes with 1012 (Service Restart): reconnect.
RecycleStopReason::RecycleOnly this worker is replaced (--max-requests, --max-memory); the server goes on.

Properties#

NameTypeDescription
$namereadonly string

Examples#

Example 1Tell the client why
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 answers
Output
HTTP/1.1 200 OK
Server: Swerve
Transfer-Encoding: chunked
Connection: close

11
waiting for news

20
new code is starting: reconnect

0