▾ modified src/Runner/ConsoleRunner.php +3 -4 Viewed
@@ -56,8 +56,8 @@ final class ConsoleRunner implements InvocationRunner 56 56 $input -> setInteractive ( false ); 57 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 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. 61 61 if ( null !== $this -> httpRunner ) { 62 62 $this -> console -> reset (); 63 63 } @@ -68,8 +68,7 @@ final class ConsoleRunner implements InvocationRunner 68 68 try { 69 69 $this -> console -> reset (); 70 70 } 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. 73 72 fwrite ( $this -> errorLog , 'Service reset failed: ' . $e . \PHP_EOL ); 74 73 } 75 74 }
@@ -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 Invocation 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 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 Viewed
@@ -111,6 +111,12 @@ class ScalewayRuntime extends SymfonyRuntime 111 111 $application->setDefaultCommand((string) $command->getName(), true); 112 112 } 113 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 120 return new ConsoleRunner(static fn (): Application => $application, null, $errorLog); 115 121 } 116 122
@@ -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 Viewed
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 Viewed
@@ -218,6 +218,16 @@ final class HandlerTest extends TestCase 218 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 231 public function testCommandFrontControllerRunsItsCommandWhateverTheName () : void 222 232 { 223 233 $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 () : void 231 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' ]);