Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Runtime] Add environment variable APP_RUNTIME_MODE#51408
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
[Runtime] Add environment variable APP_RUNTIME_MODE#51408
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
BaptisteContreras commentedAug 21, 2023
|
dunglas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This would be nice to have in 6.4.
| throw new TypeError(sprintf('Invalid return value: callable object expected, "%s" returned from "%s".', get_debug_type($app), $_SERVER['SCRIPT_FILENAME'])); | ||
| } | ||
| $_ENV['APP_RUNTIME_MODE'] = $_ENV['APP_RUNTIME_MODE'] ?? $_SERVER['APP_RUNTIME_MODE'] ?? (in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? 'cli' : 'web'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| $_ENV['APP_RUNTIME_MODE']= $_ENV['APP_RUNTIME_MODE'] ?? $_SERVER['APP_RUNTIME_MODE'] ?? (in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? 'cli' : 'web'); | |
| $_ENV['APP_RUNTIME_MODE']??= $_SERVER['APP_RUNTIME_MODE'] ?? (in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? 'cli' : 'web'); |
dunglas commentedOct 14, 2023
For#51661, we can add worker mode detection like that: if (null === ($_ENV['APP_RUNTIME_MODE'] ??=$_ENV['APP_RUNTIME_MODE'] ??$_SERVER['APP_RUNTIME_MODE'] ??null)) {if ($_ENV['FRANKENPHP_WORKER'] ??$_SERVER['FRANKENPHP_WORKER']) {$_ENV['APP_RUNTIME_MODE'] ='worker'; }elseif (\PHP_SAPI ==='cli' || \PHP_SAPI ==='phpdbg') {$_ENV['APP_RUNTIME_MODE'] ='cli'; }else {$_ENV['APP_RUNTIME_MODE'] ='web'; }} Support for other workers engine like RoadRunner will have to be done in the runtime themselves, because AFAIK they don't provide a way to detect if they are used at this point. |
54b121b tofd124f6CompareBaptisteContreras commentedOct 14, 2023
Hi@dunglas ! Thanks for the review. I updated the PR with your suggestions. I slightly modified :
to
For the case when |
fd124f6 tob491d0dCompare| } | ||
| if (null === ($_ENV['APP_RUNTIME_MODE'] ??= $_SERVER['APP_RUNTIME_MODE'] ?? null)) { | ||
| if ($_ENV['FRANKENPHP_WORKER'] ?? $_SERVER['FRANKENPHP_WORKER'] ?? false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Do you really need to have this specific check? Wouldn't that be something to put in FrankenPHP instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We could do that also for CLI and SAPI. The main benefit of doing it here is to not have to duplicate this code in every runtime supporting FrankenPHP worker mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
SettingAPP_RUNTIME_MODE in the bridge would be a better demonstration of how this have to be configured. Otherwise we'll receive PR to add Bref or React support in this file.
nicolas-grekas commentedOct 16, 2023
While reviewing this PR, I thought another approach might be preferable, see#52079. |
BaptisteContreras commentedOct 16, 2023
Your implementation seems indeed more flexible ! |
nicolas-grekas commentedOct 16, 2023
Thanks for pushing this forward@BaptisteContreras! |
…`kernel.runtime_mode.*`, all set from env var `APP_RUNTIME_MODE` (nicolas-grekas)This PR was merged into the 6.4 branch.Discussion----------[HttpKernel] Add parameters `kernel.runtime_mode` and `kernel.runtime_mode.*`, all set from env var `APP_RUNTIME_MODE`| Q | A| ------------- | ---| Branch? | 6.4| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets |Fix#51340| License | MIT| Doc PR | TODOAlternative to#51408. I think this approach is simpler and more powerful.Here, we ensure that the kernel always provides a new `kernel.runtime_mode` parameter. This parameter is an array derived by default from the `APP_RUNTIME_MODE` env var, using the `query_string` processor.This also creates 3 new parameters that should be the most common: `kernel.runtime_mode.web`, `kernel.runtime_mode.cli`, and `kernel.runtime_mode.worker`.A long-running server would typically set `APP_RUNTIME_MODE` to `web=1&worker=1` or `web=0&worker=1` when appropriate (eghttps://github.com/php-runtime/frankenphp-symfony/ should do so when `FRANKENPHP_WORKER` is set.)I screened the codebase and updated them all except cache pools (where the SAPI is used to enable/disable locking) and error renderers (where the SAPI is used to turn html-rendering on/off.) These require more work that could be done later on. There are a few other remaining usages of `PHP_SAPI` but these look not appropriate for the new flag.Commits-------7c70aec [HttpKernel] Add parameters `kernel.runtime_mode` and `kernel.runtime_mode.*`, all set from env var `APP_RUNTIME_MODE`
rustatian commentedOct 29, 2023
Hey 👋🏻 |
dunglas commentedOct 30, 2023
Hi@rustatian, thanks for the hint! We decided to move this part in the dedicated adapters provided by php-runtime. If you mind son't hesitate to open a PR for RoadRunner!https://github.com/php-runtime/runtime |
rustatian commentedOct 30, 2023
With pleasure,@dunglas 😄 |
Based on#51340 (comment) I added the envvarAPP_RUNTIME_MODE in the
autoload_runtime.phpIts purpose is to indicate in which mode the application is running (
cliorweb).I had a hard time figuring out how to test this. I finally decided to only test the priority to set the value ($_ENV > $_SERVER > default guessing).
During my test, I noticed that if I define a value for
APP_RUNTIME_MODEin$_ENV, and another value in$_SERVER, the output of$context['APP_RUNTIME_MODE']in my application callable will be the value defined in$_SERVER, and the value$_ENV['APP_RUNTIME_MODE']is untouched (which is normal). This is due to the way DotEnv loads and override envvars.You can check this behavior in
src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_set.php.For me it seems OK because I can't see a way of having
$_SERVER['APP_RUNTIME_MODE']different from$_ENV['APP_RUNTIME_MODE']I did not do the Doc PR, I first want to check if my implementation if OK for you