jdecool/scaleway-runtimepublic Fork
⑂ 5b47e80
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

fix: retry a failed Kernel boot of a Console Front ControllerUnverified

FrameworkBundle's Application boots its Kernel on its first run only and
marks its commands registered before that boot, so a failed boot left the
Warm Process answering every Command Invocation with a non-booted Kernel
error. Booting it while building the Application makes it a boot failure
the Handler retries, as for a Kernel.
Jérémy DECOOL committed 2026-11-14T02:06:41+01:00 Browse files
5b47e80 parent: 79de57a
modified src/Runner/ConsoleRunner.php +3 -4
@@ -56,8 +56,8 @@ final class ConsoleRunner implements InvocationRunner
5656 $input->setInteractive(false);
5757 $output = new BufferedOutput(decorated: false);
5858
59- // The Kernel resets its services before handling a request, not before a command: the previous HTTP Invocation
60- // left them dirty. Only a runner serving HTTP has had such Invocations, and its Kernel is booted already.
59+ // The Kernel resets its services before handling a request, not before a command: a previous HTTP Invocation
60+ // left them dirty. Only a runner serving HTTP has such Invocations.
6161 if (null !== $this->httpRunner) {
6262 $this->console->reset();
6363 }
@@ -68,8 +68,7 @@ final class ConsoleRunner implements InvocationRunner
6868 try {
6969 $this->console->reset();
7070 } catch (\Throwable $e) {
71- // A Kernel that failed to boot has nothing to reset, and its boot error is in the output already;
72- // like a failed Kernel termination, it must not replace the Function Response.
71+ // The command has run: like a failed Kernel termination, this must not replace its Function Response.
7372 fwrite($this->errorLog, 'Service reset failed: '.$e.\PHP_EOL);
7473 }
7574 }
@@ -56,8 +56,8 @@ final class ConsoleRunner implements InvocationRunner
56 $input->setInteractive(false);56 $input->setInteractive(false);
57 $output = new BufferedOutput(decorated: false);57 $output = new BufferedOutput(decorated: false);
58 58
59- // The Kernel resets its services before handling a request, not before a command: the previous HTTP Invocation59+ // The Kernel resets its services before handling a request, not before a command: a previous HTTP Invocation
60- // left them dirty. Only a runner serving HTTP has had such Invocations, and its Kernel is booted already.60+ // left them dirty. Only a runner serving HTTP has such Invocations.
61 if (null !== $this->httpRunner) {61 if (null !== $this->httpRunner) {
62 $this->console->reset();62 $this->console->reset();
63 }63 }
@@ -68,8 +68,7 @@ final class ConsoleRunner implements InvocationRunner
68 try {68 try {
69 $this->console->reset();69 $this->console->reset();
70 } catch (\Throwable $e) {70 } catch (\Throwable $e) {
71- // A Kernel that failed to boot has nothing to reset, and its boot error is in the output already;71+ // The command has run: like a failed Kernel termination, this must not replace its Function Response.
72- // like a failed Kernel termination, it must not replace the Function Response.
73 fwrite($this->errorLog, 'Service reset failed: '.$e.\PHP_EOL);72 fwrite($this->errorLog, 'Service reset failed: '.$e.\PHP_EOL);
74 }73 }
75 }74 }
modified src/ScalewayRuntime.php +6 -0
@@ -111,6 +111,12 @@ class ScalewayRuntime extends SymfonyRuntime
111111 $application->setDefaultCommand((string) $command->getName(), true);
112112 }
113113 if ($application instanceof Application) {
114+ // FrameworkBundle's Application boots its Kernel on its first run only, and never again once that boot
115+ // failed: booting here makes it a boot failure the Handler retries, as for a Kernel.
116+ if ($application instanceof FrameworkApplication) {
117+ $application->getKernel()->boot();
118+ }
119+
114120 return new ConsoleRunner(static fn (): Application => $application, null, $errorLog);
115121 }
116122
@@ -111,6 +111,12 @@ class ScalewayRuntime extends SymfonyRuntime
111 $application->setDefaultCommand((string) $command->getName(), true);111 $application->setDefaultCommand((string) $command->getName(), true);
112 }112 }
113 if ($application instanceof Application) {113 if ($application instanceof Application) {
114+ // FrameworkBundle's Application boots its Kernel on its first run only, and never again once that boot
115+ // failed: booting here makes it a boot failure the Handler retries, as for a Kernel.
116+ if ($application instanceof FrameworkApplication) {
117+ $application->getKernel()->boot();
118+ }
119+
114 return new ConsoleRunner(static fn (): Application => $application, null, $errorLog);120 return new ConsoleRunner(static fn (): Application => $application, null, $errorLog);
115 }121 }
116 122
added tests/Fixtures/front-controllers/console-boot-fails.php +35 -0
new file mode 100644
@@ -0,0 +1,35 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel;
6+use Symfony\Bundle\FrameworkBundle\Console\Application;
7+use Symfony\Component\Config\Loader\LoaderInterface;
8+use Symfony\Component\DependencyInjection\Container;
9+use Symfony\Component\EventDispatcher\EventDispatcher;
10+use Symfony\Component\HttpKernel\Kernel;
11+
12+// A bin/console whose Kernel fails to boot while RecordingKernel::$failingBuilds is positive.
13+return static fn (): Application => new Application(new class('test', false) extends Kernel {
14+ public function registerBundles(): iterable
15+ {
16+ return [];
17+ }
18+
19+ public function registerContainerConfiguration(LoaderInterface $loader): void
20+ {
21+ }
22+
23+ // Skips the container compilation, which would write a cache dir into the repository.
24+ public function boot(): void
25+ {
26+ if (RecordingKernel::$failingBuilds > 0) {
27+ --RecordingKernel::$failingBuilds;
28+
29+ throw new RuntimeException('Boot failed on purpose.');
30+ }
31+ $this->container ??= new Container();
32+ $this->container->set('event_dispatcher', new EventDispatcher());
33+ $this->booted = true;
34+ }
35+});
new file mode 100644
@@ -0,0 +1,35 @@
1+<?php
2+
3+declare(strict_types=1);
4+
5+use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel;
6+use Symfony\Bundle\FrameworkBundle\Console\Application;
7+use Symfony\Component\Config\Loader\LoaderInterface;
8+use Symfony\Component\DependencyInjection\Container;
9+use Symfony\Component\EventDispatcher\EventDispatcher;
10+use Symfony\Component\HttpKernel\Kernel;
11+
12+// A bin/console whose Kernel fails to boot while RecordingKernel::$failingBuilds is positive.
13+return static fn (): Application => new Application(new class('test', false) extends Kernel {
14+ public function registerBundles(): iterable
15+ {
16+ return [];
17+ }
18+
19+ public function registerContainerConfiguration(LoaderInterface $loader): void
20+ {
21+ }
22+
23+ // Skips the container compilation, which would write a cache dir into the repository.
24+ public function boot(): void
25+ {
26+ if (RecordingKernel::$failingBuilds > 0) {
27+ --RecordingKernel::$failingBuilds;
28+
29+ throw new RuntimeException('Boot failed on purpose.');
30+ }
31+ $this->container ??= new Container();
32+ $this->container->set('event_dispatcher', new EventDispatcher());
33+ $this->booted = true;
34+ }
35+});
modified tests/HandlerTest.php +10 -0
@@ -218,6 +218,16 @@ final class HandlerTest extends TestCase
218218 self::assertStringContainsString('Service reset failed: LogicException: Reset failed on purpose.', $this->errorLog());
219219 }
220220
221+ public function testFailedKernelBootOfAConsoleFrontControllerIsRetriedOnNextInvocation(): void
222+ {
223+ RecordingKernel::$failingBuilds = 1;
224+ $handler = $this->handler(['front_controller' => 'front-controllers/console-boot-fails.php', 'debug' => false]);
225+
226+ self::assertSame(500, $handler->invoke($this->commandEvent('list'), [])['statusCode']);
227+ self::assertStringContainsString('Boot failed on purpose.', $this->errorLog());
228+ self::assertSame(200, $handler->invoke($this->commandEvent('list'), [])['statusCode']);
229+ }
230+
221231 public function testCommandFrontControllerRunsItsCommandWhateverTheName(): void
222232 {
223233 $handler = $this->handler(['front_controller' => 'front-controllers/command.php']);
@@ -218,6 +218,16 @@ final class HandlerTest extends TestCase
218 self::assertStringContainsString('Service reset failed: LogicException: Reset failed on purpose.', $this->errorLog());218 self::assertStringContainsString('Service reset failed: LogicException: Reset failed on purpose.', $this->errorLog());
219 }219 }
220 220
221+ public function testFailedKernelBootOfAConsoleFrontControllerIsRetriedOnNextInvocation(): void
222+ {
223+ RecordingKernel::$failingBuilds = 1;
224+ $handler = $this->handler(['front_controller' => 'front-controllers/console-boot-fails.php', 'debug' => false]);
225+
226+ self::assertSame(500, $handler->invoke($this->commandEvent('list'), [])['statusCode']);
227+ self::assertStringContainsString('Boot failed on purpose.', $this->errorLog());
228+ self::assertSame(200, $handler->invoke($this->commandEvent('list'), [])['statusCode']);
229+ }
230+
221 public function testCommandFrontControllerRunsItsCommandWhateverTheName(): void231 public function testCommandFrontControllerRunsItsCommandWhateverTheName(): void
222 {232 {
223 $handler = $this->handler(['front_controller' => 'front-controllers/command.php']);233 $handler = $this->handler(['front_controller' => 'front-controllers/command.php']);