6

I have a job that is somehow getting kicked off multiple times. I want the job to kick off once and only once. If any other attempts to run the job while it's already on the queue, I want those runs to ABORT.

I've read the Laravel 8 documentation and can't figure out if I should use:

I believe the first one aborts subsequent attempts to run the job whereas the second keeps it queued, just makes sure it doesn't run until the first job is finished. Can anyone confirm?

Ben Barreth
  • 1,675
  • 1
  • 15
  • 16

1 Answers1

13

Confirmed locally by attempting to run multiple instances of the same job in a console window.

Implementing the Queue\ShouldBeUnique interface in the class of my job means that subsequent attempts are ABORTED.

Whereas adding ->withoutOverlapping() to the end of my job reference in the app\console\kernel.php file simply prevents it from running simultaneously. It does NOT abort the job if one is already running.

Ben Barreth
  • 1,675
  • 1
  • 15
  • 16
  • 4
    A point to be noted here is that if you're using `->withoutOverlapping()` in the job class itself as a middleware instead of `app\console\Kernel.php`, then you can also append `->dontRelease()` to it and it won't be released back to the queue and will be discarded, making `ShouldBeUnique` and `withoutOverlapping()` kinda same within the job class. I don't think `dontRelease()` can be used in `Kernel.php`. It can only be used in the job middleware. – Mukarram Khalid Apr 04 '22 at 05:42