diff --git a/docker/README.md b/docker/README.md index c0df085..235efa4 100644 --- a/docker/README.md +++ b/docker/README.md @@ -60,7 +60,7 @@ After changing an env switch, recreate: `docker compose up -d --force-recreate a ```sh docker compose exec app-stock php artisan fixtures:dispatch --list -docker compose exec app-stock php artisan fixtures:dispatch # all but crashes +docker compose exec app-stock php artisan fixtures:dispatch # all but crashes and calls-artisan docker compose exec app-stock php artisan fixtures:dispatch crashes # exit(255); --crash=segv for SIGSEGV docker compose exec app-stock php artisan fixtures:dispatch sleeps --sleep=20 --queue=import docker compose exec app-stock php artisan fixtures:dispatch sleeps-steady --sleep=20 # sleeps through signals diff --git a/src/Wiring.php b/src/Wiring.php index 8bf8312..2e9b669 100644 --- a/src/Wiring.php +++ b/src/Wiring.php @@ -2,8 +2,10 @@ namespace Tevkert\QueuePrefork; +use Closure; use Illuminate\Console\Events\CommandFinished; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Foundation\Events\Terminating; use Illuminate\Queue\Events\Looping; use Illuminate\Queue\Events\WorkerStopping; use Laravel\Horizon\Console\HorizonCommand; @@ -106,9 +108,17 @@ private static function child(Application $app): void Settings::mergeDefaults($app); - $app['events']->listen(CommandFinished::class, function (CommandFinished $event): void { - if (Runtime::$role === 'unit-loop' && Runtime::$statusDir !== null && Runtime::$unitId !== null) { - StatusFiles::writeIntent(Runtime::$statusDir, Runtime::$unitId, (int) $event->exitCode); + $exitCode = null; + + // Artisan::call() inside a job fires CommandFinished too, so this only keeps the exit code. + // The last one before the kernel terminates is this process's own command. + $app['events']->listen(CommandFinished::class, function (CommandFinished $event) use (&$exitCode): void { + $exitCode = (int) $event->exitCode; + }); + + self::whenCommandEnds($app, function () use (&$exitCode): void { + if ($exitCode !== null && Runtime::$role === 'unit-loop' && Runtime::$statusDir !== null && Runtime::$unitId !== null) { + StatusFiles::writeIntent(Runtime::$statusDir, Runtime::$unitId, $exitCode); } RoleGuard::armWatchdog(); @@ -178,13 +188,29 @@ private static function hostedMaster(Application $app): void $app->singleton(Master::class); $app->bind(HorizonCommand::class, PreforkHorizonCommand::class); - $app['events']->listen(CommandFinished::class, fn () => RoleGuard::armWatchdog()); + self::whenCommandEnds($app, fn () => RoleGuard::armWatchdog()); $app['events']->listen(MasterSupervisorLooped::class, fn () => $app->make(Master::class)->watchHost()); } catch (Throwable $e) { Master::handBack(get_class($e).': '.$e->getMessage()); } } + /** + * Runs $callback once this process's own command is over. Laravel calls the console kernel's + * terminate() for that command only, never for Artisan::call(). + */ + private static function whenCommandEnds(Application $app, Closure $callback): void + { + if (class_exists(Terminating::class)) { + // Laravel 11.18+, fired before the app's terminating callbacks + $app['events']->listen(Terminating::class, fn () => $callback()); + + return; + } + + $app->terminating($callback); + } + private static function installedProtocol(): int { $source = (string) @file_get_contents(__DIR__.'/Runtime.php'); diff --git a/tests/Integration/QueueWorkStockParityTest.php b/tests/Integration/QueueWorkStockParityTest.php index ed7973c..783ec90 100644 --- a/tests/Integration/QueueWorkStockParityTest.php +++ b/tests/Integration/QueueWorkStockParityTest.php @@ -35,7 +35,7 @@ public function test_a_job_sees_the_same_process_as_in_stock(): void public function test_every_fixture_ends_as_in_stock(): void { - // All but `crashes` (QueueWorkParityTest has it): random values, connections, Concurrency::run, + // All but `crashes` (QueueWorkParityTest has it) and `calls-artisan` (its own test): random values, connections, Concurrency::run, // batches, releases, failures, a timeout kill, a --memory exit, output. self::both(fn ($c) => self::artisan($c, 'fixtures:dispatch --queue='.self::QUEUE)); $outcomes = []; @@ -58,6 +58,23 @@ public function test_every_fixture_ends_as_in_stock(): void $this->assertSame($outcomes[self::STOCK], $outcomes[self::PREFORK]); } + public function test_a_job_keeps_its_worker_after_running_an_artisan_command(): void + { + // The job keeps working for longer than exit_grace (15 s) after its Artisan::call(). The Horizon + // counterpart is in StockParityTest. + self::both(fn ($c) => self::artisan($c, 'fixtures:dispatch calls-artisan --work=18 --queue='.self::QUEUE)); + $seen = []; + + foreach (self::BOTH as $container) { + [$called] = self::waitUntil(fn () => self::rows($container, 'CallsArtisan.called'), 20, "CallsArtisan.called in {$container}"); + [$done] = self::waitUntil(fn () => self::rows($container, 'CallsArtisan'), 40, "{$container}: the job to finish 18 s after its Artisan::call()"); + $this->assertSame($called['pid'], $done['pid'], "{$container}: one worker ran the whole job"); + $seen[$container] = [$called['value'], $done['value']]; + } + + $this->assertSame($seen[self::STOCK], $seen[self::PREFORK], 'SIGALRM does after the command what it does in stock'); + } + public function test_concurrent_workers_draw_different_random_numbers(): void { // Forked workers reseed: two of them must not share a sequence (the template's seed). Each job diff --git a/tests/Integration/StockParityTest.php b/tests/Integration/StockParityTest.php index 41e1814..ea9318d 100644 --- a/tests/Integration/StockParityTest.php +++ b/tests/Integration/StockParityTest.php @@ -71,6 +71,27 @@ public function test_every_fixture_ends_as_in_stock(): void $this->assertSame($outcomes[self::STOCK], $outcomes[self::PREFORK]); } + public function test_a_job_keeps_its_worker_after_running_an_artisan_command(): void + { + // The job keeps working for longer than exit_grace (15 s) after its Artisan::call(). + foreach ([self::STOCK, self::PREFORK] as $container) { + self::artisan($container, 'fixtures:dispatch calls-artisan --work=18'); + } + + // fixtures:results matches --job as a prefix: pick each row by its exact name + $rows = fn (string $container, string $job) => array_values(array_filter(self::results($container, 'CallsArtisan'), fn (array $row) => $row['job'] === $job)); + $seen = []; + + foreach ([self::STOCK, self::PREFORK] as $container) { + [$called] = self::waitUntil(fn () => $rows($container, 'CallsArtisan.called'), 20, "CallsArtisan.called in {$container}"); + [$done] = self::waitUntil(fn () => $rows($container, 'CallsArtisan'), 40, "{$container}: the job to finish 18 s after its Artisan::call()"); + $this->assertSame($called['pid'], $done['pid'], "{$container}: one worker ran the whole job"); + $seen[$container] = [$called['value'], $done['value']]; + } + + $this->assertSame($seen[self::STOCK], $seen[self::PREFORK], 'SIGALRM does after the command what it does in stock'); + } + public function test_worker_output_reaches_the_horizon_log(): void { foreach ([self::STOCK, self::PREFORK] as $container) { diff --git a/workbench/app/Console/Commands/FixturesDispatch.php b/workbench/app/Console/Commands/FixturesDispatch.php index e34c91b..5c5bbc6 100644 --- a/workbench/app/Console/Commands/FixturesDispatch.php +++ b/workbench/app/Console/Commands/FixturesDispatch.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Jobs\CallsArtisan; use App\Jobs\Crashes; use App\Jobs\DispatchesBatch; use App\Jobs\Dumps; @@ -17,15 +18,16 @@ use App\Jobs\TimesOut; use Illuminate\Console\Command; -/** Dispatches fixture jobs by name. With no names, every fixture except `crashes`. */ +/** Dispatches fixture jobs by name. With no names, every fixture except `crashes` and `calls-artisan`. */ class FixturesDispatch extends Command { protected $signature = 'fixtures:dispatch - {fixtures?* : fixture names (see --list); default: all but crashes} + {fixtures?* : fixture names (see --list); default: all but crashes and calls-artisan} {--queue=default : queue to dispatch on} {--times=1 : how many of each} {--sleep=3 : seconds for sleeps} {--hold=0 : seconds reads-random keeps its worker after drawing} + {--work=18 : seconds calls-artisan keeps working after its command} {--leak=160 : megabytes for leaks-memory} {--crash=exit : exit or segv, for crashes} {--list : list the fixture names}'; @@ -44,7 +46,7 @@ public function handle(): int return self::SUCCESS; } - $names = $this->argument('fixtures') ?: array_values(array_diff(array_keys($fixtures), ['crashes'])); + $names = $this->argument('fixtures') ?: array_values(array_diff(array_keys($fixtures), ['crashes', 'calls-artisan'])); $unknown = array_diff($names, array_keys($fixtures)); if ($unknown) { @@ -84,6 +86,7 @@ private function fixtures(): array 'dumps' => fn () => new Dumps, 'runs-concurrency' => fn () => new RunsConcurrency, 'dispatches-batch' => fn () => new DispatchesBatch, + 'calls-artisan' => fn () => new CallsArtisan((int) $this->option('work')), ]; } } diff --git a/workbench/app/Jobs/CallsArtisan.php b/workbench/app/Jobs/CallsArtisan.php new file mode 100644 index 0000000..1ea3d65 --- /dev/null +++ b/workbench/app/Jobs/CallsArtisan.php @@ -0,0 +1,41 @@ + get_debug_type(pcntl_signal_get_handler(SIGALRM)); + $before = $alarm(); + + Artisan::call('env'); + + $started = microtime(true); + JobResult::record('CallsArtisan.called', ['alarm_before' => $before, 'alarm_after' => $alarm()]); + + while (($left = $started + $this->seconds - microtime(true)) > 0) { + usleep((int) (min($left, 0.5) * 1_000_000)); + } + + JobResult::record('CallsArtisan', ['alarm_before' => $before, 'alarm_after' => $alarm(), 'seconds' => $this->seconds]); + } +}