Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/ClusterWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ final class ClusterWatcher
use ForbidCloning;
use ForbidSerialization;

/**
* The default worker shutdown timeout in seconds.
*/
public const WORKER_TIMEOUT = 5;

private readonly ContextFactory $contextFactory;
Expand Down Expand Up @@ -120,8 +123,10 @@ public function getMessageIterator(): ConcurrentIterator

/**
* @param int $count Number of cluster workers to spawn.
* @param float|null $workerShutdownTimeout The maximum time to wait for a worker to shut down, in seconds,
* or null to wait indefinitely.
*/
public function start(int $count): void
public function start(int $count, ?float $workerShutdownTimeout = ClusterWatcher::WORKER_TIMEOUT): void
{
if ($this->running || $this->queue->isComplete()) {
throw new \Error("The cluster watcher is already running or has already run");
Expand All @@ -137,7 +142,7 @@ public function start(int $count): void
try {
for ($i = 0; $i < $count; ++$i) {
$id = $this->nextId++;
$this->workers[$id] = $this->startWorker($id);
$this->workers[$id] = $this->startWorker($id, $workerShutdownTimeout);
}
} catch (\Throwable $exception) {
$this->stop();
Expand All @@ -146,9 +151,11 @@ public function start(int $count): void
}

/**
* @param positive-int $id
* @param positive-int $id The worker ID.
* @param float|null $shutdownTimeout The maximum time to wait for the worker to shut down, in seconds,
* or null to wait indefinitely.
*/
private function startWorker(int $id): ContextClusterWorker
private function startWorker(int $id, ?float $shutdownTimeout): ContextClusterWorker
{
$context = $this->contextFactory->start($this->script);

Expand Down Expand Up @@ -203,12 +210,13 @@ private function startWorker(int $id): ContextClusterWorker
$socket,
$deferredCancellation,
$id,
$shutdownTimeout,
): void {
async($this->provider->provideFor(...), $socket, $deferredCancellation->getCancellation())->ignore();

try {
try {
$worker->run();
$worker->run($shutdownTimeout);

$worker->info("Worker {$id} terminated cleanly" .
($this->running ? ", restarting..." : ""));
Expand All @@ -230,7 +238,7 @@ private function startWorker(int $id): ContextClusterWorker
}

if ($this->running) {
$this->workers[$id] = $this->startWorker($this->nextId++);
$this->workers[$id] = $this->startWorker($this->nextId++, $shutdownTimeout);
}
} catch (\Throwable $exception) {
$this->stop();
Expand Down
18 changes: 14 additions & 4 deletions src/Internal/ContextClusterWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Amp\Cancellation;
use Amp\CancelledException;
use Amp\Cluster\ClusterWatcher;
use Amp\Cluster\ClusterWorker;
use Amp\Cluster\ClusterWorkerMessage;
use Amp\DeferredCancellation;
Expand Down Expand Up @@ -72,7 +71,13 @@ public function send(mixed $data): void
$this->context->send(new WatcherMessage(WatcherMessageType::Data, $data));
}

public function run(): void
/**
* Run the worker.
*
* @param float|null $shutdownTimeout The maximum time to wait for the worker to shut down, in seconds,
* or null to wait indefinitely.
*/
public function run(?float $shutdownTimeout): void
{
$watcher = EventLoop::repeat(self::PING_TIMEOUT / 2, weakClosure(function (): void {
if ($this->lastActivity < \time() - self::PING_TIMEOUT) {
Expand Down Expand Up @@ -112,10 +117,15 @@ public function run(): void
}

try {
$this->joinFuture->await(new TimeoutCancellation(ClusterWatcher::WORKER_TIMEOUT));
if ($shutdownTimeout === null) {
$this->joinFuture->await();
} else {
$this->joinFuture->await(new TimeoutCancellation($shutdownTimeout));
}
} catch (CancelledException) {
$this->close();
// Give it a second to reap the result. Generally this never should time out, unless something is seriously broken.
// Give it a second to reap the result. Generally this never should time out, unless something is
// seriously broken.
$this->joinFuture->await(new TimeoutCancellation(1));
}
} catch (\Throwable $exception) {
Expand Down
Loading