Support the ongoing development of Laravel.io →
Article Hero Image

Laravel Advanced: Top 5 Scheduler Functions You Might Not Know About

17 Jun, 2024 2 min read

Photo by Jazmin Quaynor on Unsplash

In this article series, we go a little deeper into parts of Laravel we all use, to uncover functions and features that we can use in our next projects... if only we knew about them! Our first article in the series is about the Laravel Scheduler - which helps run scheduled tasks (aka cron jobs).

Let's explore a few lesser-known scheduler functions:

1. skip() & when()

If you want your scheduled task to execute only when some condition is true, use when() to set such conditions inline:

$schedule->command('your:command')->when(function () {
    return some_condition();
});

skip() is the exact opposite of the when() method. If the skip method returns true, the scheduled task will not be executed:

$schedule->command('emails:send')->daily()->skip(function(){
    return Calendar::isHolidauy();
});

2. withoutOverlapping()

You may be running a critical job that should only have one instance running at a time. That's where withoutOverlapping() ensures that a scheduled task won't overlap, preventing potential conflicts.

$schedule->command('your:command')->withoutOverlapping();

3. thenPing()

After executing a task, you might want to ping a URL to notify another service or trigger another action. thenPing() lets you do just that seamlessly.

$schedule->command('your:command')->thenPing('http://example.com/webhook');

4. runInBackground()

If you want your scheduled task to run in the background without holding up other processes. runInBackground() will help you do this:

$schedule->command('your:command')->runInBackground();

5. evenInMaintenanceMode()

You can guess what it does by its name. You can execute scheduled tasks even when your application is in maintenance mode.

$schedule->command('your:command')->evenInMaintenanceMode();

That's all for now, folks! Give them a try; use these scheduler functions in your task automation and make your code much easier. All of the above have been previously shared on our Twitter, one by one. Follow us on Twitter; You'll ❤️ it.

Keep exploring, keep coding, and keep pushing the boundaries of what you can achieve with Laravel. Until next time, happy scheduling! 🚀

Last updated 1 month ago.

driesvints, marcelzutphen, fosterushka, saltyfishh9, issacnguyentx2022, azzazkhan liked this article

6
Like this article? Let the author know and give them a clap!

Other articles you might like

Article Hero Image December 13th 2024

How to add WebAuthn Passkeys To Backpack Admin Panel

Want to make your Laravel Backpack admin panel more secure with a unique login experience for your a...

Read article
Article Hero Image December 13th 2024

Quickest way to setup PHP Environment (Laravel Herd + MySql)

Setting up a local development environment can be a time taking hassle—whether it's using Docker or...

Read article
Article Hero Image December 5th 2024

How to set up Laravel Magic Link?

User authentication is crucial for making web applications secure and easy to use. Traditionally, pa...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.