laravellaravel-queue

Laravel failed job is not retrying


I have a job that has public $tries = 2; but when it fails, it does not retry. In Horizon, I have to retry the job manually.

Here is the job:

class SendEmailMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $message;
    public $tries = 2;

    public function __construct(EmailMessage $message)
    {
        $this->message = $message;
    }

    public function handle(EmailMessageRepository $emailMessageRepository)
    {
        app()->setLocale($this->message->organization->locale);
        $emailMessageRepository->send($this->message);
    }
}

Here is the queue config:

'email' => [
    'driver' => 'redis',
    'connection' => 'default',
    'queue' => 'email',
    'retry_after' => 90,
    'block_for' => null,
],

And the Horizon supervisor config:

'supervisor-2' => [
    'connection' => 'redis',
    'queue' => ['media', 'email', 'sms'],
    'balance' => 'simple',
    'processes' => 1,
    'tries' => 2,
],

Why isn't Horizon retrying the job when it fails? Am I missing something?


Solution

  • Turns out the problem was that the job was being dispatched from within a DB transaction and so the row in the database didn't yet exist.

    Mohamed from Laravel Forge suggested I use the method afterCommit(). See Jobs & Database Transactions for details.

    That resolved the issue. The second try wasn't necessary.