Skip to content

Instantly share code, notes, and snippets.

@sentiasa
Last active March 26, 2024 09:16
Show Gist options
  • Save sentiasa/b7cd43cc909b54944dcadc79f5e05264 to your computer and use it in GitHub Desktop.
Save sentiasa/b7cd43cc909b54944dcadc79f5e05264 to your computer and use it in GitHub Desktop.
Command for retrying failed horizon jobs
<?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