Skip to content

Commit

Permalink
add new check to ensure all default queues of all connection will be …
Browse files Browse the repository at this point in the history
…processed by a horizon worker
okaufmann committed Nov 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 0e5437f commit 7549f5e
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions src/Commands/LaravelHorizonDoctorCommand.php
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ class LaravelHorizonDoctorCommand extends Command

public function handle(): int
{
$this->info('Checking your Horizon configs...');

$horizonConfigs = config('horizon.environments.production');
$default = config('horizon.defaults');
$queueConfigs = config('queue.connections');
@@ -27,6 +25,17 @@ public function handle(): int
}
}

$this->checkHorizonConfigs($horizonConfigs, $queueConfigs);
$this->checkDefaultQueuesAreProcessed($horizonConfigs, $queueConfigs);
$this->checkConnectionsAreUsedInHorizon($queueConfigs, $horizonConfigs);

return self::SUCCESS;
}

protected function checkHorizonConfigs(array $horizonConfigs, array $queueConfigs)
{
$this->info('Checking your Horizon configs...');

foreach ($horizonConfigs as $key => $horizonConfig) {
$errors = collect();
$this->info("Checking queue `{$key}`");
@@ -52,6 +61,7 @@ public function handle(): int
if (isset($horizonConfig['timeout']) && $horizonConfig['timeout'] >= $queueConnection['retry_after']) {
$errors[] = "`timeout` of configured horizon queue `{$key}` ({$horizonConfig['timeout']}) in config/horizon.php should be marginally bigger than the `retry_after` option of the queue connection `{$key}` ({$horizonConfig['timeout']}) set in config/queue.php";
}

if ($errors->count()) {
$errors->each(fn ($error) => $this->error("- {$error}"));
} else {
@@ -60,27 +70,60 @@ public function handle(): int

$this->comment('');
}
}

protected function checkConnectionsAreUsedInHorizon(array $queueConfigs, array $horizonConfigs): void
{
$this->info('Running some global checks...');

// check that all queue connections are used in horizon
$redisQueues = collect($queueConfigs)
$redisQueueConnections = collect($queueConfigs)
->filter(fn ($queue) => $queue['driver'] === 'redis')
->keys();
$usedConnectionsInHorizon = collect($horizonConfigs)
->map(fn ($queue) => $queue['connection'])
->unique()
->values();

$diff = $redisQueues->diff($usedConnectionsInHorizon);
$diff = $redisQueueConnections->diff($usedConnectionsInHorizon);

if (count($diff) > 0) {
$diff = $diff->implode(',');
$this->error("- You should consider configuring the following queues in config/horizon.php: {$diff}");
} else {
$this->info('- Everything looks good!');
}
}

return self::SUCCESS;
protected function checkDefaultQueuesAreProcessed(array $horizonConfigs, array $queueConfigs)
{
$this->info('Checking your Queue configs...');

$redisQueueConnections = collect($queueConfigs)
->filter(fn ($queue) => $queue['driver'] === 'redis');

foreach ($redisQueueConnections as $connectionName => $queueConfig) {
$this->info("Checking connection `{$connectionName}`");
$errors = collect();

// check that default queue is processed by horizon
$processedQueuesInHorizon = collect($horizonConfigs)
->map(fn ($queue) => $queue['queue'])
->flatten()
->unique()
->values();
$defaultQueue = $queueConfig['queue'];
if (! $processedQueuesInHorizon->contains($defaultQueue)) {
$errors[] = "Default queue `{$defaultQueue}` of connection `{$connectionName}` will not be processed by any worker set in config/horizon.php";
}

if ($errors->count()) {
$errors->each(fn ($error) => $this->error("- {$error}"));
} else {
$this->info('- Everything looks good!');
}

$this->comment('');
}
}
}

0 comments on commit 7549f5e

Please sign in to comment.