Repository navigation
ContinuousPool
ContinuousPool supervises named processes and restarts them after they finish. It extends Pool, keeping the same output and completion callback methods while adding a polling loop and a configurable restart delay.
use GT\Daemon\ContinuousPool;
use GT\Daemon\Process;
$pool = new ContinuousPool(restartDelay: 1.0);
$pool->add("worker", new Process(PHP_BINARY, "worker.php"));
try {
$pool->exec();
while($pool->isActive()) {
$pool->tick();
echo $pool->read();
fwrite(STDERR, $pool->readError());
usleep(10000);
}
}
finally {
$pool->close();
}Add every process before calling exec(). The pool uses non-blocking processes, including when a supplied process previously had blocking enabled. Adding processes or calling exec() again while the pool is active throws DaemonException.
The example runs until application shutdown code calls close(). An empty pool remains inactive after exec().
tick() checks each process and starts replacements whose delay has elapsed. It does not sleep while waiting for a restart or wait for a worker to finish. Keep calling it from the application's supervising loop.
The default delay is one second, measured from when completion is observed by tick(). Pass another number of seconds to the constructor, including fractional values. Negative or non-finite delays throw InvalidArgumentException.
A delay of zero permits a restart on the next tick that observes completion. Each worker can restart at most once per tick, so an immediately completing process cannot create a recursive restart loop.
Both successful and unsuccessful exits are restarted. Exceptions raised when launching a process, such as CommandNotFoundException, stop the pool and propagate to the caller. The pool does not repeatedly retry a command that cannot be launched.
Use isActive() for the supervising loop. numRunning() reports only workers currently running, so it can be zero while the pool waits to restart them. Unlike tick(), counting workers does not restart them.
The usual read(), readError(), readOutputOf() and readErrorOf() methods are available. Unread standard output and standard error are retained before a completed worker restarts, then combined with output from its next execution.
$pool->onComplete(function(Process $process):void {
echo "Worker completed with code " . $process->getExitCode() . PHP_EOL;
});Callbacks run once per execution when process status is checked. They can call close() to stop supervision; a stopped pool does not restart a worker after the callback returns.
close() marks the pool inactive and cancels pending restarts before terminating workers. It returns the same named exit-code array as Pool::close(). Later calls to tick() do nothing until exec() starts a new session.
Already buffered output and output drained immediately before shutdown can still be read after close(). Continue reading while workers run to avoid accumulating output in memory.
Each worker keeps its command, working directory, environment and registered completion callbacks across restarts. The underlying Process releases its previous process resources and resets completion state for every execution. See Process for direct process reuse, and Pool for the inherited output methods.
PHP.GT/Daemon is a separately maintained component of PHP.GT/WebEngine.