Skip to content

Instantly share code, notes, and snippets.

@ulcuber
Created October 26, 2022 15:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ulcuber/c96c283ad7c0899dadc248e10cc60b90 to your computer and use it in GitHub Desktop.
Save ulcuber/c96c283ad7c0899dadc248e10cc60b90 to your computer and use it in GitHub Desktop.
Laravel Horizon. Retry all of the failed jobs like one in the horizon dashboard
<?php
namespace App\Console\Commands\Horizon;
use Illuminate\Console\Command;
use Illuminate\Queue\Failed\FailedJobProviderInterface;
use Laravel\Horizon\Contracts\JobRepository;
use Laravel\Horizon\Jobs\RetryFailedJob;
class RetryAllFailedJobsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'horizon:retry-all-failed-jobs
{--failer : Check standard failed jobs. Memory consuming}
{--dry : Only show counts}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Retry all of the failed jobs like one in the horizon dashboard';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$checkFailer = $this->option('failer');
$jobsRepository = $this->jobs();
$failer = $this->failer();
$queueFailedKey = config('queue.failed.driver')
. ':' . config('queue.failed.database')
. ':' . config('queue.failed.table');
$failedCount = $jobsRepository->countFailed();
$this->info('Horizon not expired failed: ' . $failedCount);
$this->info('Horizon total failed: ' . $jobsRepository->totalFailed());
if ($checkFailer) {
// could consume too much memory
$this->info("Count '{$queueFailedKey}' failed: " . count($failer->all()));
}
if ($this->option('dry')) {
return Command::SUCCESS;
}
$this->info('');
$this->withProgressBar($failedCount, function ($bar) use ($jobsRepository, $failer) {
// only 50 last
$failedHorizonJobs = $jobsRepository->getFailed();
while (count($failedHorizonJobs) > 0) {
foreach ($failedHorizonJobs as $failedJob) {
$id = $failedJob->id;
// @see Laravel\Horizon\Http\Controllers\RetryController
dispatch(new RetryFailedJob($id));
$jobsRepository->deleteFailed($id);
$failer->forget($id);
$bar->advance();
}
$failedHorizonJobs = $jobsRepository->getFailed();
}
});
$totalFailed = $jobsRepository->totalFailed();
$this->info('');
$this->info('');
$this->info('Horizon not expired failed: ' . $jobsRepository->countFailed());
$this->info('');
$this->info('Horizon total failed: ' . $totalFailed);
if ($totalFailed > 0) {
$this->info('To delete all of the expired horizon jobs consider running:');
$this->comment('php artisan horizon:clear');
}
if ($checkFailer) {
$this->info('');
// could consume too much memory
$queueFailedCount = count($failer->all());
$this->info("Count '{$queueFailedKey}' failed: " . $queueFailedCount);
if ($queueFailedCount > 0) {
$this->info('To flush all of the failed queue jobs consider running:');
$this->comment('php artisan queue:flush');
}
}
return Command::SUCCESS;
}
protected function jobs(): JobRepository
{
return app(JobRepository::class);
}
protected function failer(): FailedJobProviderInterface
{
return app('queue.failer');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment