Advertisement
  1. Code
  2. PHP
  3. Laravel

Task Scheduling in Laravel

Scroll to top

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.

1
*/5 * * * * /web/statistics.sh

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.

1
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

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.

1
<?php
2
3
namespace App\Console;
4
5
use Illuminate\Console\Scheduling\Schedule;
6
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7
8
class Kernel extends ConsoleKernel
9
{
10
    /**

11
     * The Artisan commands provided by your application.

12
     *

13
     * @var array

14
     */
15
    protected $commands = [
16
        //

17
    ];
18
19
    /**

20
     * Define the application's command schedule.

21
     *

22
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule

23
     * @return void

24
     */
25
    protected function schedule(Schedule $schedule)
26
    {
27
        // $schedule->command('inspire')->hourly();

28
    }
29
30
    /**

31
     * Register the commands for the application.

32
     *

33
     * @return void

34
     */
35
    protected function commands()
36
    {
37
        $this->load(__DIR__.'/Commands');
38
39
        require base_path('routes/console.php');
40
    }
41
}

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.

1
<?php
2
3
namespace App\Console;
4
5
use Illuminate\Console\Scheduling\Schedule;
6
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7
use Illuminate\Support\Facades\DB;
8
9
class Kernel extends ConsoleKernel
10
{
11
    /**
12
     * The Artisan commands provided by your application.
13
     *
14
     * @var array
15
     */
16
    protected $commands = [
17
        //
18
    ];
19
20
    /**
21
     * Define the application's command schedule.

22
     *

23
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule

24
     * @return void

25
     */

26
    protected function schedule(Schedule $schedule)

27
    {

28
        // the call method

29
        $schedule->call(function () {

30
          $posts = DB::table('posts')

31
            ->select('user_id', DB::raw('count(*) as total_posts'))

32
            ->groupBy('user_id')

33
            ->get();

34
     

35
          foreach($posts as $post)

36
          {

37
            DB::table('users_statistics')

38
              ->where('user_id', $post->user_id)

39
              ->update(['total_posts' => $post->total_posts]);

40
          }

41
        })->everyThirtyMinutes();

42
    }

43


44
    /**

45
     * Register the commands for the application.

46
     *

47
     * @return void

48
     */

49
    protected function commands()

50
    {

51
        $this->load(__DIR__.'/Commands');

52


53
        require base_path('routes/console.php');

54
    }

55
}

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.

1
<?php
2
3
namespace App\Console;
4
5
use Illuminate\Console\Scheduling\Schedule;
6
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7
use Illuminate\Support\Facades\DB;
8
9
class Kernel extends ConsoleKernel
10
{
11
    /**

12
     * The Artisan commands provided by your application.

13
     *

14
     * @var array

15
     */
16
    protected $commands = [
17
        'App\Console\Commands\UserStatistics'
18
    ];
19
20
    /**

21
     * Define the application's command schedule.

22
     *

23
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule

24
     * @return void

25
     */
26
    protected function schedule(Schedule $schedule)
27
    {
28
        $schedule->command('statistics:user')->everyThirtyMinutes();
29
    }
30
31
    /**

32
     * Register the commands for the application.

33
     *

34
     * @return void

35
     */
36
    protected function commands()
37
    {
38
        $this->load(__DIR__.'/Commands');
39
40
        require base_path('routes/console.php');
41
    }
42
}

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.

1
<?php
2
namespace App\Console\Commands;
3
 
4
use Illuminate\Console\Command;
5
use Illuminate\Support\Facades\DB;
6
 
7
class UserStatistics extends Command
8
{
9
  /**

10
   * The name and signature of the console command.

11
   *

12
   * @var string

13
   */
14
  protected $signature = 'statistics:user';
15
 
16
  /**

17
   * The console command description.

18
   *

19
   * @var string

20
   */
21
  protected $description = 'Update user statistics';
22
 
23
  /**

24
   * Create a new command instance.

25
   *

26
   * @return void

27
   */
28
  public function __construct()
29
  {
30
    parent::__construct();
31
  }
32
 
33
  /**

34
   * Execute the console command.

35
   *

36
   * @return mixed

37
   */
38
  public function handle()
39
  {
40
    // calculate new statistics

41
    $posts = DB::table('posts')
42
      ->select('user_id', DB::raw('count(*) as total_posts'))
43
      ->groupBy('user_id')
44
      ->get();
45
     
46
    // update statistics table

47
    foreach($posts as $post)
48
    {
49
      DB::table('users_statistics')
50
      ->where('user_id', $post->user_id)
51
      ->update(['total_posts' => $post->total_posts]);
52
    }
53
  }
54
}

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.

1
<?php
2
namespace App\Console;
3
4
use Illuminate\Console\Scheduling\Schedule;
5
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
6
 
7
class Kernel extends ConsoleKernel
8
{
9
  /**

10
   * Define the application's command schedule.

11
   *

12
   * @param  \Illuminate\Console\Scheduling\Schedule  $schedule

13
   * @return void

14
   */
15
  protected function schedule(Schedule $schedule)
16
  {
17
    // exec method

18
    $host = config('database.connections.mysql.host');
19
    $username = config('database.connections.mysql.username');
20
    $password = config('database.connections.mysql.password');
21
    $database = config('database.connections.mysql.database');
22
     
23
    $schedule->exec("mysqldump -h {$host} -u {$username} -p{$password} {$database}")
24
      ->daily()
25
      ->sendOutputTo('/backups/daily_backup.sql');
26
  }
27
}

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.

1
$schedule->command('statistics:user')->everyThirtyMinutes()->withoutOverlapping();

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.

1
$schedule->command('statistics:user')->everyThirtyMinutes()->withoutOverlapping(30);

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.

1
$schedule->command('statistics:user')->daily()->runInBackground();

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.