dev-resources.site
for different kinds of informations.
Laravel queues: Skip job if no longer required
Published at
12/26/2024
Categories
laravel
tutorial
Author
Sergio Peris
While working on a Laravel project, we might dispatch a job to a queue.
But what if the job is no longer required when the Laravel queue worker is ready to process it?
For example, a user might cancel a subscription, and we no longer need to send a reminder email.
In this case, we can skip the job if it's no longer required.
To achieve this, we can use the Skip
middleware in the job class.
...
use Illuminate\Queue\Middleware\Skip;
class SendReminderEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(private readonly User $user) {}
/**
* Get the middleware the job should pass through.
*/
public function middleware(): array
{
return [
Skip::when(fn () => $this->user->subscription->isCancelled()),
];
}
/**
* Handle the job.
*/
public function handle(): void
{
$this->user->sendReminderEmail();
}
}
As shown in the example above, we can use the Skip
middleware to skip the job if the user's subscription is cancelled.
This way, we can avoid processing unnecessary jobs and save resources.
Articles
12 articles in total
Change keyboard to Spanish distribution on Ubuntu Server
read article
SQL Server: Get database port
read article
Laravel queues: Skip job if no longer required
currently reading
How to check your battery health
read article
Disable password expiration in Windows
read article
Add a sidebar with options in a Filament resource
read article
Enable GRUB menu
read article
Add Windows 10 to GRUB OS list
read article
Docker Compose: Remove all containers, networks, volumes and images from a project
read article
Add Enter after barcode with Honeywell PDA
read article
How to deploy Tileserver GL
read article
Reset unknown Windows' administrator password
read article
Featured ones: