-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Runtime] Automatically use FrankenPHP runner when its worker mode is detected #60503
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
Open
alexandre-daubois
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
alexandre-daubois:frankenphp-runner
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Runtime] Automatically use FrankenPHP runner when its worker mode is…
… detected
- Loading branch information
commit b81d09db36c9607e222f0f65e26677cac634f15f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Symfony/Component/Runtime/Runner/FrankenPhpWorkerRunner.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Runtime\Runner; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\TerminableInterface; | ||
use Symfony\Component\Runtime\RunnerInterface; | ||
|
||
/** | ||
* A runner for FrankenPHP in worker mode. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
class FrankenPhpWorkerRunner implements RunnerInterface | ||
{ | ||
public function __construct( | ||
private HttpKernelInterface $kernel, | ||
private int $loopMax, | ||
) { | ||
} | ||
|
||
public function run(): int | ||
{ | ||
// Prevent worker script termination when a client connection is interrupted | ||
ignore_user_abort(true); | ||
|
||
$server = array_filter($_SERVER, static fn (string $key) => !str_starts_with($key, 'HTTP_'), ARRAY_FILTER_USE_KEY); | ||
$server['APP_RUNTIME_MODE'] = 'web=1&worker=1'; | ||
|
||
$handler = function () use ($server, &$sfRequest, &$sfResponse): void { | ||
Check failure on line 40 in src/Symfony/Component/Runtime/Runner/FrankenPhpWorkerRunner.php
|
||
// Connect to the Xdebug client if it's available | ||
if (\extension_loaded('xdebug') && \function_exists('xdebug_connect_to_client')) { | ||
xdebug_connect_to_client(); | ||
} | ||
|
||
// Merge the environment variables coming from DotEnv with the ones tied to the current request | ||
$_SERVER += $server; | ||
|
||
$sfRequest = Request::createFromGlobals(); | ||
$sfResponse = $this->kernel->handle($sfRequest); | ||
|
||
$sfResponse->send(); | ||
}; | ||
|
||
$loops = 0; | ||
do { | ||
$ret = \frankenphp_handle_request($handler); | ||
|
||
if ($this->kernel instanceof TerminableInterface && $sfRequest && $sfResponse) { | ||
$this->kernel->terminate($sfRequest, $sfResponse); | ||
} | ||
|
||
gc_collect_cycles(); | ||
} while ($ret && (0 >= $this->loopMax || ++$loops < $this->loopMax)); | ||
|
||
return 0; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Runtime/Tests/FrankenPhpWorkerRunnerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Runtime\Tests; | ||
|
||
require_once __DIR__.'/frankenphp-function-mock.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\TerminableInterface; | ||
use Symfony\Component\Runtime\Runner\FrankenPhpWorkerRunner; | ||
|
||
interface TestAppInterface extends HttpKernelInterface, TerminableInterface | ||
{ | ||
} | ||
|
||
class FrankenPhpWorkerRunnerTest extends TestCase | ||
{ | ||
public function testRun() | ||
{ | ||
$application = $this->createMock(TestAppInterface::class); | ||
$application | ||
->expects($this->once()) | ||
->method('handle') | ||
->willReturnCallback(function (Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response { | ||
$this->assertSame('bar', $request->server->get('FOO')); | ||
|
||
return new Response(); | ||
}); | ||
$application->expects($this->once())->method('terminate'); | ||
|
||
$_SERVER['FOO'] = 'bar'; | ||
|
||
$runner = new FrankenPhpWorkerRunner($application, 500); | ||
$this->assertSame(0, $runner->run()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Runtime/Tests/SymfonyRuntimeTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Runtime\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\Runtime\Runner\FrankenPhpWorkerRunner; | ||
use Symfony\Component\Runtime\SymfonyRuntime; | ||
|
||
class SymfonyRuntimeTest extends TestCase | ||
{ | ||
public function testGetRunner() | ||
{ | ||
$application = $this->createStub(HttpKernelInterface::class); | ||
|
||
$runtime = new SymfonyRuntime(); | ||
$this->assertNotInstanceOf(FrankenPhpWorkerRunner::class, $runtime->getRunner(null)); | ||
$this->assertNotInstanceOf(FrankenPhpWorkerRunner::class, $runtime->getRunner($application)); | ||
|
||
$_SERVER['FRANKENPHP_WORKER'] = 1; | ||
$this->assertInstanceOf(FrankenPhpWorkerRunner::class, $runtime->getRunner($application)); | ||
} | ||
|
||
public function testStringWorkerMaxLoopThrows() | ||
{ | ||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('The "worker_loop_max" runtime option must be an integer, "string" given.'); | ||
|
||
new SymfonyRuntime(['worker_loop_max' => 'foo']); | ||
} | ||
|
||
public function testBoolWorkerMaxLoopThrows() | ||
{ | ||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage('The "worker_loop_max" runtime option must be an integer, "bool" given.'); | ||
|
||
new SymfonyRuntime(['worker_loop_max' => false]); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Runtime/Tests/frankenphp-function-mock.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
if (!function_exists('frankenphp_handle_request')) { | ||
function frankenphp_handle_request(callable $callable): bool | ||
{ | ||
$callable(); | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.