john-martins's avatar

Horizon - Jobs with unique ID repeating when using batches

Hi there,

When dispatching a batch with jobs containing the same unique ID, we can see numerous instances of the job in the Pending jobs lists on Laravel Horizon. Our expected behaviour is for jobs with the same unique ID not to be added to the queue at the same time.

We have implemented ShouldBeUnique, defined a unique ID, and used Redis as the cache driver.

It works correctly without using batches:

// Restrics to only one job per uniqueId:
foreach ($tests as $test) {
        TestJob::dispatch();
}

// Repeats jobs with same uniqueId:
$batch = [];
foreach ($tests as $test) {
      $batch[] = new TestJob();
}
Bus::batch($batch)
			->onConnection('redis')
            ->dispatch();

Any advice is much appreciated.

Thanks,

0 likes
5 replies
hupp's avatar

@john-martins Seems althings would be perfect but Please try to do following checklist :

  1. Check Redis Configration and its working fine with Laravel Application or not.
  2. Make sure you have the correct configuration for the should_be_unique_for key in your horizon.php configuration file.
  3. if step 1 & 2 both correct try to run php artisan horizon:terminate tor reastart Horizon and clear any unknow issues.
  4. verify you have implements ShouldQueue, ShouldBeUnique both traits in your class

Let me know your feedback.

john-martins's avatar

Thanks for the quick reply @hupp ! I've updated the post with more information regarding batches vs single dispatches. The issue only happens with batches. I also tried to find more information about "should_be_unique_for" in the horizon config, but couldn't find any information about it.

michaelaerni's avatar

@john-martins Did you find a solution for this issue? I'm running into the same problem right now.

john-martins's avatar

@michaelaerni, apologies for the late reply. Unfortunately, I haven't found a solution yet. It seems like ShouldBeUnique is NotActuallyUnique.

Please or to participate in this conversation.