Job with ShouldBeUnique never dispatched

Viewed 2612

I'm trying to dispatch a job which implements shouldBeUnique but it doesn't work, my job is never in the queue. If I remove ShouldBeUnique, it works as expected.

I am using Redis.

Do you guys have an idea what's happening?

Here is my code :

<?php

namespace App\Jobs;

use Exception;
use Throwable;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Foundation\Bus\Dispatchable;

class VerifyJob implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $releaseDelay = 10;
    public $tries = 3;

    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function uniqueId()
    {
        return $this->user->id;
    }

    public function handle()
    {
        \Log::debug('verify 0');

        return;
    }

    public function failed(Throwable $exception)
    {
        \Log::debug('verify 999');
    }
}

1 Answers

It's because laravel failed to unlock the unique id, and that is why it was able to dispatch again after you use another unique id.

the reason is the file laravel uses to store the unique id doesn't have right permissions. laravel needs to delete it to unlock the id. It's under the storage/framework/cache/data diretory

Related