Last active
March 26, 2024 09:16
-
-
Save sentiasa/b7cd43cc909b54944dcadc79f5e05264 to your computer and use it in GitHub Desktop.
Command for retrying failed horizon jobs
This file contains 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 | |
use Laravel\Horizon\Contracts\JobRepository; | |
use Laravel\Horizon\Jobs\RetryFailedJob; | |
class RetryFailedJobsViaHorizon extends Command | |
{ | |
protected $signature = 'horizon:retry-failed-jobs'; | |
protected $description = 'Retry failed jobs via horizon'; | |
public function __construct(JobRepository $jobs) | |
{ | |
parent::__construct(); | |
$this->jobs = $jobs; | |
} | |
public function handle() | |
{ | |
$failedJobs = $this->jobs->getFailed(); | |
// You can create a progress bar: | |
$bar = $this->output->createProgressBar($failedJobs->count()); | |
foreach ($failedJobs as $failedJob) { | |
$uuid = $failedJob->uuid; | |
$payload = json_decode($failedJob->payload); | |
// Dispatch failed job via Horizon | |
dispatch(new RetryFailedJob($uuid)); | |
// Delete it in database (if it fails again, it will create a new record) | |
\DB::table('failed_jobs')->where('uuid', $uuid)->delete(); | |
$bar->advance(); | |
} | |
$bar->finish(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment