7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
Advertisement

Task Scheduling in Laravel

Read Time: 10 mins

In this article, we’ll go through one of the exciting features of the Laravel web framework—task scheduling. We’ll look at how Laravel allows you to manage scheduled tasks in your application. Moreover, we’ll also end up creating our own custom scheduled tasks for demonstration purposes.

The Laravel framework allows you to set up scheduled tasks so that you don't have to worry about setting them up at the system level. You can get rid of that complex cron syntax while setting up scheduled tasks since Laravel allows you to define them in a user-friendly way.

We’ll start the article with how you are used to setting up traditional cron jobs, and following that we’ll explore the Laravel way of achieving it. In the latter half of the article, we’ll give it a try by creating a couple of custom scheduled tasks that should provide hands-on insight into the subject.

Traditional Scheduled Task Setup

In your day-to-day application development, you'll often need to execute certain scripts or commands periodically. If you're working with the *nix system, you are probably aware that cron jobs handle these commands. On the other hand, they're known as scheduled tasks on Windows-based systems.

Let's have a quick look at a simple example of the *nix based cron job.

Pretty simple—it runs the statistics.sh file every five minutes!

Although that was a pretty simple example, you'll often need to implement more complex use cases. A complex system requires you to define multiple cron jobs that run at different time intervals.

Let's see some tasks a complex web application has to perform periodically in the back-end.

• Clean up the unnecessary data from the database back-end.
• Update the front-end caching indexes to keep it up-to-date.
• Calculate the site statistics.
• Send emails.
• Back up different site elements.
• Generate reports.
• And more.

So as you can see, there's plenty of stuff out there waiting to be run periodically and also at different time intervals. If you're a seasoned system admin, it's easy to define cron jobs for all these tasks, but sometimes we as developers wish that there was an easier way around.

Luckily, Laravel comes with a built-in task scheduling API which allows you to define scheduled tasks like never before. And yes, the next section is all about that—the basics of Laravel task scheduling.

The Laravel Way

In the earlier section, we went through the traditional way of setting up cron jobs. In this section, we'll go through the specifics of Laravel in the context of the task scheduling API.

Before we go ahead, the important thing to understand is that the scheduling feature provided by Laravel is just like any other feature and won't be invoked automatically. So if you're thinking that you don't need to do anything at the system level then you're out of luck, I'd say.

In fact, the first thing you should do should you wish to use the Laravel scheduling system is to set up the cron job which runs every minute and calls the artisan command shown in the following snippet.

The above artisan command calls the Laravel scheduler, and that in turn executes all the pending cron jobs defined in your application.

Of course, we are yet to see how to define the scheduled tasks in your Laravel application, and that's the very next thing we'll dive into.

It's the schedule method of the App\Console\Kernel class which you need to use should you wish to define application-specific scheduled tasks.

Go ahead and grab the contents of the app/Console/Kernel.php file.

As you can see, the core code itself provides a useful example. In the above example, Laravel runs the inspire artisan command hourly. Don't you think that the syntax is so intuitive in the first place?

In fact, there are a couple of different ways in which Laravel allows you to define scheduled tasks:

• Use the closure/callable.
• Call the artisan command.
• Execute the shell command.

Moreover, there are plenty of built-in scheduling frequencies you could choose from:

• every minute/every five minutes
• hourly/daily/weekly/quarterly/yearly
• at a specific time of the day
• and many more

In fact, I would say that it provides a complete set of routines so that you don't ever need to touch the shell to create your custom cron jobs!

Yes, I can tell that you're eager to know how to implement your custom scheduled tasks, and that is what I also promised at the beginning of the article.

Create Your First Scheduled Task in Laravel

As we discussed, there are different ways in which Laravel allows you to define scheduled tasks. Let's go through each to understand how it works.

The Closure/Callable Method

The scheduling API provides the call method which allows you to execute a callable or a closure function. Let's revise the app/Console/Kernel.php file with the following code.

As you can see, we've passed the closure function as the first argument of the call method. Also, we've set the frequency to every 30 minutes, so it'll execute the closure function every 30 minutes!

In our example, we count the total posts per user and update the statistics table accordingly.

The artisan Command

Apart from the closures or callables, you could also schedule an artisan command which will be executed at certain intervals. In fact, it should be the preferred approach over closures as it provides better code organization and reusability at the same time.

Go ahead and revise the contents of the app/Console/Kernel.php file with the following.

It's the command method which you would like to use should you wish to schedule an artisan command as shown in the above code snippet. You need to pass an artisan command signature as the first argument of the command method.

Of course, you also need to define the corresponding artisan command at app/Console/Commands/UserStatistics.php.

The exec Command

We could say that the methods that we've discussed so far were specific to the Laravel application itself. Moreover, Laravel also allows you to schedule the shell commands so that you could run external applications as well.

Let's go through a quick example which demonstrates how to take a backup of your database every day.

It's apparent from the code that you need to use the exec method of the scheduler, and you need to pass the command you would like to run as its first argument.

Apart from that, we've also used the sendOutputTo method, which allows you to collect the output of the command. On the other hand, there's a method, emailOutputTo, which allows you to email the output contents!

How to Prevent Task Overlaps

In this section, we'll see how you can prevent task overlapping. Let's say you've defined a task, and you want to make sure that if it's already running, Laravel shouldn't run another instance of the same task. By default, Laravel will always start running scheduled tasks even if the previous instance of the same task is already running and hasn't completed yet.

So let's see how you could avoid overlapping your scheduled tasks.

As you can see, you just need to use the withoutOverlapping method to make sure Laravel doesn't overlap the already running task. By default, the lock time is 24 hours before Laravel overlaps a task. If you want to override it, you can do it as shown in the following snippet.

In the above example, Laravel waits for 30 minutes before it clears the overlapping lock.

How to Define Background Tasks

If you schedule multiple tasks at the same time, Laravel runs them sequentially. So if you have a task that takes a very long time to execute, the next scheduled task would have to wait for a very long time. To avoid this, you can execute such tasks in the background.

Let's quickly look at the following example to see how to define a background task.

As you can see, you can use the runInBackground method to define a background task.

Conclusion

Today, we went through the task scheduling API in the Laravel web framework. It was fascinating to see how easily it allows you to manage tasks that need to be run periodically.

At the beginning of the article, we discussed the traditional way of setting up scheduled tasks, and following that we introduced the Laravel way of doing it. In the latter half of the article, we went through a couple of practical examples to demonstrate task scheduling concepts.

I hope that you’ve enjoyed the article, and you should feel more confident about setting up scheduled tasks in Laravel. For those of you who are either just getting started with Laravel or looking to expand your knowledge, site, or application with extensions, we have a variety of things you can study on Envato Market.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.