logo
podcast Podcast
get help Get Unstuck

Being explicit when something should not queue

A simple way to clearly communicate intent

Joel Clermont
Joel Clermont
2024-01-31

Laravel provides a simple interface you can add to a listener to make sure it runs on the queue, and not within the current request. By marking your listener as implementing Illuminate\Contracts\Queue\ShouldQueue, it wires it up for you automatically.

Most of the time, queueing a listener is a sensible default. Every once in a while, though, you might need a listener to complete its work synchronously within the request.

If you just remove the ShouldQueue interface, another developer in the future might wonder if it was an oversight, or if it was intentional. You could add a comment in the listener, but one simple technique I like is to create your own empty interface called ShouldNotQueue.

Then it's clear to anyone looking at the listener that it is intentionally not queued.

Here's the entire contents of that empty interface, there's not much to it:

namespace App\Contracts;

/**
 * This particular Listener should absolutely NOT queue
 */
interface ShouldNotQueue {}

Here to help,

Joel

P.S. We're full of good ideas like this. We packaged a few popular tips in a free eBook you can download.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.