Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow configuring worker shutdown timeout #17

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 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 null|int|float $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, null|int|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 null|int|float $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, null|int|float $shutdownTimeout = ClusterWatcher::WORKER_TIMEOUT): 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 Down
14 changes: 12 additions & 2 deletions src/Internal/ContextClusterWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ public function send(mixed $data): void
$this->context->send(new WatcherMessage(WatcherMessageType::Data, $data));
}

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

$this->joinFuture->await(new TimeoutCancellation(ClusterWatcher::WORKER_TIMEOUT));
if ($shutdownTimeout === null) {
$this->joinFuture->await();
} else {
$this->joinFuture->await(new TimeoutCancellation($shutdownTimeout));
}
} catch (\Throwable $exception) {
$this->joinFuture->ignore();
throw $exception;
Expand Down
Loading