From 6ee6133f3897530793433ccdcd40048f957fb2ec Mon Sep 17 00:00:00 2001 From: Nick Verschoor Date: Tue, 4 Mar 2025 10:30:42 +0100 Subject: [PATCH] feat: Added worker count environment variable --- composer.json | 3 --- config/kubernetes.php | 12 ++++++++++++ src/Commands/StartCommand.php | 24 +++++++++++++++++------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index ceca790..a0f92f6 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,5 @@ "php-http/discovery": true, "tbachert/spi": true } - }, - "require-dev": { - "laravel/horizon": "^5.30" } } diff --git a/config/kubernetes.php b/config/kubernetes.php index 603c055..e58bcf8 100644 --- a/config/kubernetes.php +++ b/config/kubernetes.php @@ -26,6 +26,18 @@ ], ], + /* + |-------------------------------------------------------------------------- + | Worker Count + |-------------------------------------------------------------------------- + | + | This value defines the number of worker processes that will be started + | to process incoming requests. If you are using a load balancer, you may + | want to set this value to the number of CPU cores available. + | + */ + 'worker_count' => env('KUBERNETES_WORKER_COUNT', 'auto'), + /* |-------------------------------------------------------------------------- | Maximum Execution Time diff --git a/src/Commands/StartCommand.php b/src/Commands/StartCommand.php index 800e12f..76735b5 100644 --- a/src/Commands/StartCommand.php +++ b/src/Commands/StartCommand.php @@ -23,7 +23,7 @@ class StartCommand extends StartFrankenPhpCommand {--port=80 : The port the server should be available on} {--admin-host=localhost : The host the admin server should be available on} {--admin-port=2019 : The port the admin server should be available on} - {--workers=auto : The number of workers that should be available to handle requests} + {--workers= : The number of workers that should be available to handle requests} {--max-requests=500 : The number of requests to process before reloading the server} {--caddyfile= : The path to the FrankenPHP Caddyfile file} {--https : Enable HTTPS, HTTP/2, and HTTP/3, and automatically generate and renew certificates} @@ -51,7 +51,7 @@ protected function writeServerOutput($server): void ->each(function ($output): void { $debug = json_decode($output, true); - if (! is_array($debug)) { + if (!is_array($debug)) { $this->components->info($output); return; @@ -67,9 +67,8 @@ protected function startServerWatcher() { $watch = config('kubernetes.watch.enabled') || $this->option('watch'); - if (! $watch) { - return new class - { + if (!$watch) { + return new class { public function __call($method, $parameters) { return null; @@ -86,8 +85,19 @@ public function __call($method, $parameters) return tap(new Process([ (new ExecutableFinder)->find('node'), 'file-watcher.cjs', - json_encode(collect(config('octane.watch'))->map(fn ($path) => base_path($path))), + json_encode(collect(config('octane.watch'))->map(fn($path) => base_path($path))), $this->option('poll'), - ], realpath(__DIR__.'/../../bin'), null, null, null))->start(); + ], realpath(__DIR__ . '/../../bin'), null, null, null))->start(); + } + + protected function workerCount(): int + { + $count = $this->option('workers') ?? config('kubernetes.worker_count'); + + if ($count === 'auto') { + return 0; + } + + return (int) $count ?? 0; } } \ No newline at end of file