phasync
Swerve / awaitShutdown

Method

Swerve::awaitShutdown

Wait until this worker must close its connections, see Swerve::onShutdown.

Description#

public static Swerve::awaitShutdown(
    ?float $timeout = null
): bool

Parameters#

$timeout ?float = null
Seconds to wait at most; null for as long as it takes

Return values#

True once it must (at once if it must already), false when $timeout passed first bool

Errors / Exceptions#

ExceptionWhen
LogicExceptionOutside a coroutine

Examples#

Example 1Wait with a timeout
use Swerve\ClientRequest;
use Swerve\RequestHandler;
use Swerve\Swerve;

$app = new RequestHandler(function (ClientRequest $request) {
    // false when the timeout passes first
    $request->write('must close within 0.1 s: ' . var_export(Swerve::awaitShutdown(0.1), true) . "\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 / HTTP/1.1\r\nHost: example\r\n\r\n"]);
Output
HTTP/1.1 200 OK
Server: Swerve
Transfer-Encoding: chunked
Connection: close

1f
must close within 0.1 s: false

0
Example 2
if (Swerve::awaitShutdown(30)) {
    $ws->end(1001);
}