-
-
Save fhferreira/d2660b660113fb5202b966e0960104c3 to your computer and use it in GitHub Desktop.
Laravel Horizon. Retry all of the failed jobs like one in the horizon dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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