Search code examples
phplaravel-8jobslumen

Lumen prevent duplicate job in a queue


Using lumen 8.2.3 I am only trying to dispatch a unique job to a queue. In app/Console/Kernel I have sent a schedule to $schedule->job(new myJob(), 'high')->everyMinute(); this runs every minutes. In the job itself I have added the ShouldBeUnique interface class in myJob class I even added

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

when my cron job runs for php artisan schedule:run, this is still creating multiple jobs in the queue causing my 3 workers to pick up both jobs at the same time and causing issues.

https://laravel.com/docs/8.x/queues#unique-jobs clearly says

"Sometimes, you may want to ensure that only one instance of a specific job is on the queue at any point in time. You may do so by implementing the ShouldBeUnique interface on your job class. This interface does not require you to define any additional methods on your class:"

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Throwable;

class myJob implements ShouldQueue, ShouldBeUnique
{
    use InteractsWithQueue, Queueable, SerializesModels;
    
    private $process;

    public function __construct($process){
       $this->process=$process;
    }
    public function uniqueId() { 
       return $this->process->id();
    }
    
    
    public function handle()
    {
       //some code here
    }

    public function failed(Throwable $exception)
    {
    // Send user notification of failure, etc...
    }
  }

Is the no way to prevent this? thank you


Solution

  • odd how this got fixed but what I did was change the version from 8.2.4 to 8.3.4 and shouldBeUnique work looks like a bug was introduced in 8.3.4