Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 30 additions & 4 deletions src/Wiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
19 changes: 18 additions & 1 deletion tests/Integration/QueueWorkStockParityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/StockParityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions workbench/app/Console/Commands/FixturesDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Jobs\CallsArtisan;
use App\Jobs\Crashes;
use App\Jobs\DispatchesBatch;
use App\Jobs\Dumps;
Expand All @@ -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}';
Expand All @@ -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) {
Expand Down Expand Up @@ -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')),
];
}
}
41 changes: 41 additions & 0 deletions workbench/app/Jobs/CallsArtisan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Jobs;

use App\Support\JobResult;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Artisan;

/**
* Fixture: runs an Artisan command, then keeps working for $seconds. Records `CallsArtisan.called`
* after the command and `CallsArtisan` after the wait, each with the type of the SIGALRM handler.
* Waits in a loop like TimesOut, because a signal cuts sleep() short.
*/
class CallsArtisan implements ShouldQueue
{
use Queueable;

public $tries = 1;

public function __construct(public int $seconds = 18)
{
}

public function handle(): void
{
$alarm = fn (): string => 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]);
}
}
Loading