Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldBeUnique doesn't work with event listeners #36632

Closed
alielmajdaoui opened this issue Mar 17, 2021 · 4 comments
Closed

ShouldBeUnique doesn't work with event listeners #36632

alielmajdaoui opened this issue Mar 17, 2021 · 4 comments

Comments

@alielmajdaoui
Copy link

  • Laravel Version: 8.33.1
  • PHP Version: 7.4.9
  • Database Driver & Version: MySQL 8.0.15

Description:

Event listeners are always getting added to the queue even with the presence of ShouldBeUnique interface, and uniqueId and uniqueFor methods or properties.

I have done the same implementation for a Job and worked as expected.

Steps To Reproduce:

  1. Create a new event and its listener. (e.g: UserRegistered)
  2. Register the event via the EventServiceProvider $listen array.
  3. Add ShouldBeUnique to the event listener.
  4. Add uniqueId and uniqueFor methods or properties.
  5. The uniqueId value should be hardcoded (say: 123) for the sake of this bug.
  6. Dispatch the event several times. (e.g: UserRegistered::dispatch($userId))
  7. Check the jobs table and you will see many rows of the same dispatched event.
  8. When I tested it with a Job, it creates only 1 job in the jobs table, and also checked Redis cache and found that there's a laravel_unique_job:App\\Jobs\\TestJob key which goes away when I run the queue worker php artisan queue:work

Let me know if you need more details.

@driesvints
Copy link
Member

ShouldBeUnique only works for queued jobs atm so this is more of a feature request. I'll ping the team internally to see if we could add it.

@priithansen
Copy link

Same is true for Notifications that can implement ShouldQueue but doesn't work with ShouldBeUnique which would be very convenient.

@ukenpachi
Copy link

ShouldBeUnique only works for queued jobs atm so this is more of a feature request. I'll ping the team internally to see if we could add it.

Hi, I know this reply is more than a year old but I wanted to ask what do you mean by only works for queued jobs but if I do something like this

class InventoryRecalculationJob implements ShouldQueue, ShouldBeUnique
{

 public function uniqueId() {
        return 123;
    }   

}
//controller
 public function test()
    {
        $x = 1;

        while($x <= 3) {
            $this->dispatch(new newJob());
            $x++;
        }
    }
    
     public function test2()
    {
        $x = 1;

        while($x <= 3) {
            $job = (new newJob())->onQueue('high');
            $this->dispatch($job);
            $x++;
        }
    }

In both cases it still create 3 of the same jobs. So how does this shouldbeUnqiue work? p.s. I am using version 8.2.3

@PDXfoster
Copy link

Was this "enhancement" ever addressed? It seems like if a listener implements the ShouldQueue interface, all the standard queueable interface options should be available, an error should be thrown when implemented on a component that isn't supported, or it should minimally be in the documentation that the shouldbeUnqiue interface doesn't work on listeners.

I'm happy to submit a PR for an update to documentation, but won't spend the time if there's any intention of adding support for ShouldBeUnique inside listeners.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants