Problem/Motivation

The Swoole PHP Server runs Drupal in CLI mode, instead of the regular mode which is used by Apache or NginX. The Swoole module for Drupal needs to know when the Swoole PHP server is used. For instance session handling is different and cookies need to be set in a special way with the Swoole PHP Server.

Proposed resolution

Add a new container parameter "asynchronous", which defaults to false.

The container parameter can be set by using a new fifth parameter to the method Drupal\Core\DrupalKernel::__construct() or by a new sixth parameter to the method Drupal\Core\DrupalKernel::createFromRequest(). Both new parameters will default to false.

Remaining tasks

TBD

User interface changes

None

API changes

See proposed solution.

Data model changes

None

Release notes snippet

TBD

Comments

daffie created an issue. See original summary.

daffie’s picture

Status: Active » Needs review
StatusFileSize
new7.99 KB

The patch with testing.

geek-merlin’s picture

Version: 10.0.x-dev » 9.5.x-dev
Status: Needs review » Needs work

THX for the awesome work daffie!

Code-wise the patch does what it announces and contains a green test that covers both kernel creation paths.
1) Technically this needs a red test-only patch. NW for that.
I can not see (but may overlook sth) why this can not go to 9.x so setting to 9.5.x

This is not a BC breaking change but may want a CR to make people aware of the feature.

Rationale: Is a container parameter the best API for that? Nut sure, but as the information is likely to be needed in early bootstrap phases, it looks reasonable to me.

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new2.07 KB

Rationale: Is a container parameter the best API for that? Nut sure, but as the information is likely to be needed in early bootstrap phases, it looks reasonable to me.

The Swoole module does not use the default index.php file that is used by Apache and the NginX web server. In the file is Drupal core started and the user request handled. This is done by the class Drupal\Core\DrupalKernel. Other parameter of this class are $app_root, which holds the webroot directory and $site_path, which holds the relative directory where the settings.php is located. Both are stored as container parameters. That is why I have chosen to store the $asynchronous value is the same manner. You can store it in other ways, only to me this most in line with how Drupal works.

Added a change record and a tests only patch which should fail.

@geek-merlin: Thanks for the review!

Status: Needs review » Needs work

The last submitted patch, 4: 3279191-2-test-only-should-fail.patch, failed testing. View results

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new7.99 KB

Reuploading the patch from comment #2.

geek-merlin’s picture

Status: Needs review » Reviewed & tested by the community

All from #2 is resolved, test-only patch is red, draft CR lgtm, so rtbc.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/core.services.yml
    @@ -4,6 +4,7 @@ parameters:
    +  asynchronous: false
    

    This does not set this. This is only here to allow for auto-completion. The comment above need to be updated.

  2. +++ b/core/lib/Drupal.php
    @@ -221,6 +221,15 @@ public static function root() {
    +  public static function isAsynchronous() {
    +    return static::getContainer()->getParameter('asynchronous');
    +  }
    

    Return typehint.

  3. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -432,6 +444,13 @@ public function getAppRoot() {
    +  /**
    +   * {@inheritdoc}
    +   */
    +  public function isAsynchronous() {
    +    return $this->asynchronous;
    

    Return type hint.

  4. +++ b/core/lib/Drupal/Core/DrupalKernelInterface.php
    @@ -99,6 +99,13 @@ public function getSitePath();
    +  /**
    +   * Whether Drupal is run in asynchronous mode.
    +   *
    +   * @return bool
    +   */
    +  public function isAsynchronous();
    

    Let's add a return typehint on new code.

  5. Do we really need this? Can't the Swoole module set its own container parameter in a service provider and be done? Also if we're going to use the word asynchronous we need some definition of what that really is.
ravi.shankar’s picture

StatusFileSize
new8.01 KB
new1.27 KB

Addressed points 2, 3, and 4 of comment #8. Still needs work for other points.

andypost’s picture

The only blocker is left, patch needs CS clean-up

andypost’s picture

The #8.5 makes a lot of sense, technically it's good idea to allow kernel to know better the env it's using but it makes code more fragile

Core already has separate kernels for update/install and very probably ReactPhp will need further modifications to kernel, and not clear how drush/cli trigger op-cache clean-up, maybe it will need another kernel-kind

@daffie there's also SERVER_SOFTWARE environment variable (displayed at status/report) so it could be used to detect swoole/reactphp

daffie’s picture

Status: Needs work » Postponed

The loading of the DrupalKernel instance is separate from handling of the user request for Swoole. I have added the Swoole Server instance to the DrupalKernel instance in Swoole. When handling user request I only need to check if the Swoole Server instance is added to the DrupalKernel to know if I am handling a Swoole server instance.
Using a environment variable as @andypost is suggestion is also a great solution for the problem. Thanks for that!

Marking as postponed for now. If for some reason the 2 solutions will not work in the future for some reason, we will still have this issue to reopen.

Thanks to everyone who helped with this issue. It is really appreciated!

geek-merlin’s picture

Great discussuion!

Note about SERVER_SOFTWARE: It is given in the headers when responding to requests. I'd be cautious to re-use it for internal signaling. Think about a site owner that clears the variable to not disclose its content in headers.

The other approach, probing a specific service, sounds very clean.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.