From 86ca234d4f064d2aa2453c41a9d68c50a6ba5b4b Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Wed, 24 Jul 2024 09:55:37 +0000 Subject: [PATCH 1/4] Automatically disable worker mode in local environments if workers aren't specified explicitly --- src/Commands/StartFrankenPhpCommand.php | 2 ++ src/Commands/stubs/Caddyfile | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Commands/StartFrankenPhpCommand.php b/src/Commands/StartFrankenPhpCommand.php index c4f100ce4..5ab90bb83 100644 --- a/src/Commands/StartFrankenPhpCommand.php +++ b/src/Commands/StartFrankenPhpCommand.php @@ -93,12 +93,14 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve 'APP_ENV' => app()->environment(), 'APP_BASE_PATH' => base_path(), 'APP_PUBLIC_PATH' => public_path(), + 'APP_INDEX_FILE' => $this->workerCount() == 0 && app()->environment('local') ? 'index.php' : 'frankenphp-worker.php', 'LARAVEL_OCTANE' => 1, 'MAX_REQUESTS' => $this->option('max-requests'), 'REQUEST_MAX_EXECUTION_TIME' => $this->maxExecutionTime(), 'CADDY_GLOBAL_OPTIONS' => ($https && $this->option('http-redirect')) ? '' : 'auto_https disable_redirects', 'CADDY_SERVER_ADMIN_PORT' => $this->adminPort(), 'CADDY_SERVER_ADMIN_HOST' => $this->option('admin-host'), + 'CADDY_SERVER_FRANKENPHP_OPTIONS' => $this->workerCount() == 0 && app()->environment('local') ? '' : 'import worker', 'CADDY_SERVER_LOG_LEVEL' => $this->option('log-level') ?: (app()->environment('local') ? 'INFO' : 'WARN'), 'CADDY_SERVER_LOGGER' => 'json', 'CADDY_SERVER_SERVER_NAME' => $serverName, diff --git a/src/Commands/stubs/Caddyfile b/src/Commands/stubs/Caddyfile index 82508830d..12617c040 100644 --- a/src/Commands/stubs/Caddyfile +++ b/src/Commands/stubs/Caddyfile @@ -1,10 +1,14 @@ +(worker) { + worker "{$APP_PUBLIC_PATH}/frankenphp-worker.php" {$CADDY_SERVER_WORKER_COUNT} +} + { {$CADDY_GLOBAL_OPTIONS} admin {$CADDY_SERVER_ADMIN_HOST}:{$CADDY_SERVER_ADMIN_PORT} frankenphp { - worker "{$APP_PUBLIC_PATH}/frankenphp-worker.php" {$CADDY_SERVER_WORKER_COUNT} + {$CADDY_SERVER_FRANKENPHP_OPTIONS} } } @@ -31,7 +35,7 @@ {$CADDY_SERVER_EXTRA_DIRECTIVES} php_server { - index frankenphp-worker.php + index {$APP_INDEX_FILE} # Required for the public/storage/ directory... resolve_root_symlink } From 4944d1d397bdf231b68400b660b207bcc2509631 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Fri, 6 Sep 2024 15:55:07 +0200 Subject: [PATCH 2/4] Added check for XDebug mode as an additional requirement to enable FrankenPHP worker mode to avoid performance issues. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See the discussion with Kévin Dunglas in the PR discussion for the reasoning: #932 Essentially, disabling worker mode comes with performance drawbacks, but it's still the only sensible way to use FrankenPHP with XDebug. By automatically turning off worker mode if a) the amount of workers isn't set explicitly, b) we're running locally, and c) XDebug is not installed, or it's mode is set to "off", we get both optimal performance and XDebug working out of the box. --- bin/checkXdebugMode.php | 3 +++ src/Commands/StartFrankenPhpCommand.php | 29 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 bin/checkXdebugMode.php diff --git a/bin/checkXdebugMode.php b/bin/checkXdebugMode.php new file mode 100644 index 000000000..1396ddaf0 --- /dev/null +++ b/bin/checkXdebugMode.php @@ -0,0 +1,3 @@ +debugModeEnabled($frankenphpBinary); + $workerModeEnabled = $this->workerCount() == 0 && app()->environment('local') && ! $debugEnabled; + $process = tap(new Process([ $frankenphpBinary, 'run', @@ -93,14 +100,14 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve 'APP_ENV' => app()->environment(), 'APP_BASE_PATH' => base_path(), 'APP_PUBLIC_PATH' => public_path(), - 'APP_INDEX_FILE' => $this->workerCount() == 0 && app()->environment('local') ? 'index.php' : 'frankenphp-worker.php', + 'APP_INDEX_FILE' => $workerModeEnabled ? 'frankenphp-worker.php' : 'index.php', 'LARAVEL_OCTANE' => 1, 'MAX_REQUESTS' => $this->option('max-requests'), 'REQUEST_MAX_EXECUTION_TIME' => $this->maxExecutionTime(), 'CADDY_GLOBAL_OPTIONS' => ($https && $this->option('http-redirect')) ? '' : 'auto_https disable_redirects', 'CADDY_SERVER_ADMIN_PORT' => $this->adminPort(), 'CADDY_SERVER_ADMIN_HOST' => $this->option('admin-host'), - 'CADDY_SERVER_FRANKENPHP_OPTIONS' => $this->workerCount() == 0 && app()->environment('local') ? '' : 'import worker', + 'CADDY_SERVER_FRANKENPHP_OPTIONS' => $workerModeEnabled ? 'import worker' : '', 'CADDY_SERVER_LOG_LEVEL' => $this->option('log-level') ?: (app()->environment('local') ? 'INFO' : 'WARN'), 'CADDY_SERVER_LOGGER' => 'json', 'CADDY_SERVER_SERVER_NAME' => $serverName, @@ -143,6 +150,24 @@ protected function ensurePortIsAvailable() } } + /** + * Check if XDebug is installed and enabled. + * + * @param string $frankenPhpBinary + * + * @return bool + */ + protected function debugModeEnabled($frankenPhpBinary) + { + $status = tap(new Process([ + $frankenPhpBinary, + 'php-cli', + dirname(__DIR__, 2).'/bin/checkXdebugMode.php', + ], base_path()))->run(); + + return $status > 0; + } + /** * Get the path to the FrankenPHP configuration file. * From 3236ae37a59e2596ff24f2161b2e951428854891 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Fri, 6 Sep 2024 16:00:59 +0200 Subject: [PATCH 3/4] Fixed docblock formatting --- src/Commands/StartFrankenPhpCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Commands/StartFrankenPhpCommand.php b/src/Commands/StartFrankenPhpCommand.php index 3ca45187e..70c6ac781 100644 --- a/src/Commands/StartFrankenPhpCommand.php +++ b/src/Commands/StartFrankenPhpCommand.php @@ -154,7 +154,6 @@ protected function ensurePortIsAvailable() * Check if XDebug is installed and enabled. * * @param string $frankenPhpBinary - * * @return bool */ protected function debugModeEnabled($frankenPhpBinary) From 74871dad8623b46feeb05af6b46befb219ef4018 Mon Sep 17 00:00:00 2001 From: Moritz Friedrich Date: Fri, 6 Sep 2024 16:02:54 +0200 Subject: [PATCH 4/4] Fixed docblock formatting... using pint. --- src/Commands/StartFrankenPhpCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/StartFrankenPhpCommand.php b/src/Commands/StartFrankenPhpCommand.php index 70c6ac781..0dd6b029f 100644 --- a/src/Commands/StartFrankenPhpCommand.php +++ b/src/Commands/StartFrankenPhpCommand.php @@ -153,7 +153,7 @@ protected function ensurePortIsAvailable() /** * Check if XDebug is installed and enabled. * - * @param string $frankenPhpBinary + * @param string $frankenPhpBinary * @return bool */ protected function debugModeEnabled($frankenPhpBinary)