Menu

Reference / Process

supervise()

function
function supervise(int $n, callable $worker): void

Fork $n workers and keep them running.

The supervisor reaps dead workers and restarts any that crashed (non-zero exit or killed by a signal) after a delay that doubles from 0.1 s up to 5 s; a worker that stayed up for 10 s starts over at the bottom. A clean exit(0) is not restarted. SIGTERM and SIGINT sent to the supervisor are forwarded to every child, and no more restarts happen. The supervisor runs no scheduler itself: each child calls $worker($index), which is where Async\async() goes. Returns once every child has exited.

Process\supervise(8, function (int $i) {
    Async\async(function () { Async\shutdownOn(SIGTERM); serve($i); });
});

Parameters

$n int
number of workers (at least 1)
$worker callable
called in each child with the worker index; the child exits when it returns

View source