Skip to content
Draft
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 config/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
// seconds longer than every effective job or worker timeout to prevent jobs from being
// re-attempted by another queue worker before it has finished executing or timed out.
// https://laravel.com/framework/docs/11.x/queues#job-expirations-and-timeouts
'retry_after' => 100,
'retry_after' => 3605,
'block_for' => null,
],
],
Expand Down
92 changes: 92 additions & 0 deletions tests/Jobs/JobTimeoutTest.php

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Horizon’s timeout guidance says Horizon’s timeout should exceed job-level timeouts. I think an assertion that Horizon’s timeout is greater than every job-level timeout is a good addition

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Tests\Jobs;

use App\Jobs\Job;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use SplFileInfo;
use Tests\TestCase;

class JobTimeoutTest extends TestCase {
public function testRetryAfterIsAtLeastFiveSecondsLongerThanEveryJobTimeout() {
$retryAfter = config('queue.connections.redis.retry_after');
$timeouts = [
...$this->jobTimeouts(),
...$this->horizonTimeouts(),
...$this->queueWorkerTimeouts(),
];

$this->assertIsInt($retryAfter);
$this->assertNotEmpty($timeouts);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So $timeouts is a combined list of timeouts from application jobs, Horizon config and worker. Does that mean that assertNotEmpty($timeouts) only proves at least one of them provides a timeout? Should we instead assert them one by one and then combine/merge them together as a big $timeouts


foreach ($timeouts as $source => $timeout) {
$this->assertIsInt($timeout, "{$source} must be an integer number of seconds.");
$this->assertGreaterThanOrEqual(
$timeout + 5,
$retryAfter,
"queue.connections.redis.retry_after must be at least 5 seconds longer than {$source} ({$timeout} seconds).",
);
}
}

private function jobTimeouts(): array {
$timeouts = [];
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(app_path('Jobs')));

/** @var SplFileInfo $file */
foreach ($files as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}

$relativePath = substr($file->getPathname(), strlen(app_path()) + 1, -4);
$class = 'App\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);

if (!class_exists($class) || !is_a($class, Job::class, true)) {
continue;
}

$properties = (new ReflectionClass($class))->getDefaultProperties();

if (array_key_exists('timeout', $properties) && $properties['timeout'] !== null) {
$timeouts["{$class}::\$timeout"] = $properties['timeout'];
}
}

return $timeouts;
}

private function horizonTimeouts(): array {
return $this->findTimeouts(config('horizon'), 'horizon');
}

private function findTimeouts(array $config, string $path): array {
$timeouts = [];

foreach ($config as $key => $value) {
$valuePath = "{$path}.{$key}";

if ($key === 'timeout') {
$timeouts[$valuePath] = $value;
} elseif (is_array($value)) {
$timeouts = array_merge($timeouts, $this->findTimeouts($value, $valuePath));
}
}

return $timeouts;
}

private function queueWorkerTimeouts(): array {
$startScript = file_get_contents(base_path('start.sh'));
preg_match_all('/--timeout(?:=|\s+)(\d+)/', $startScript, $matches);

$timeouts = [];
foreach ($matches[1] as $index => $timeout) {
$timeouts['start.sh --timeout #' . ($index + 1)] = intval($timeout);
}

return $timeouts;
}
}
Loading