Laravel 11: Introducing Schedule Grouping

laravel
schedule
tasks
Nabil Hassen
Nabil Hassen
Nov 21, 2024
Laravel Schedule Grouping
Last updated on Nov 21, 2024
Table of contents:

Introduction

A recent addition in Laravel 11 authored by Istiak Tridip is Schedule Srouping. A powerful way to manage scheduled tasks in a more organized and intuitive manner.

The Problem

Before this feature, managing multiple scheduled tasks that shared common characteristics required repetitive configuration. For example, if several tasks needed to run every minute or in the background, developers had to specify these attributes for each task individually, leading to redundant and less maintainable code.

Schedule::command('command-one')->everyMinute()->runInBackground()->withoutOverlapping();
Schedule::command('command-two')->everyMinute()->runInBackground()->withoutOverlapping();
Schedule::command('command-three')->everyMinute()->runInBackground()->withoutOverlapping();

The Solution

The new schedule grouping feature introduces a syntax similar to the Route::group method, allowing developers to group related tasks with shared configurations. Using a new group method, you can define common attributes like ->everyMinute() or ->runInBackground() once for a group, and apply them to all tasks within.

Here’s an example of how it works:

Schedule::group()
->everyMinute()
->runInBackground()
->withoutOverlapping()
->schedules(function () {
Schedule::command('command-one');
Schedule::command('command-two');
Schedule::command('command-three');
});

You can even nest groups:

Schedule::group()
->runInBackground()
->withoutOverlapping()
->schedules(function () {
Schedule::group()->everyMinute()->schedules(function () {
Schedule::command('command-one');
Schedule::command('command-two');
});
 
Schedule::group()->everyTenMinutes()->schedules(function () {
Schedule::command('command-three');
Schedule::command('command-four');
});
});

You can also override configurations for specific schedules as needed:

Schedule::group()
->everyMinute()
->runInBackground()
->withoutOverlapping()
->schedules(function () {
Schedule::command('command-one');
Schedule::command('command-two');
 
// Override the group's cron expression
Schedule::command('command-three')->everyTenMinutes();
});

This approach drastically improves code readability and eliminates redundancy.

Supported Laravel Versions

This feature is officially supported starting from Laravel 11.

Conclusion

Schedule grouping is a game-changer for developers managing complex scheduling requirements. By reducing boilerplate code and improving readability, it exemplifies Laravel's commitment to developer-friendly features.

Explore this feature and more in Laravel 11 to streamline your application development.

For more details, check out the pull request.

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts