Home  >  Article  >  Backend Development  >  Task scheduling in Laravel

Task scheduling in Laravel

PHPz
PHPzOriginal
2023-09-01 09:05:061498browse

Task scheduling in Laravel

In this article, we will introduce an exciting feature of the Laravel web framework - task scheduling. We'll look at how Laravel allows you to manage scheduled tasks in your application. Additionally, we will eventually create our own custom scheduled task for demonstration purposes.

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

We'll first cover how to set up a traditional cron job, and then we'll explore the way Laravel implements it. In the second half of this article, we'll try it out by creating a few custom scheduled tasks that should provide practical insight into the topic.

Traditional scheduled task settings

In daily application development, you often need to execute certain scripts or commands regularly. If you use a *nix system, you probably know that cron jobs handle these commands. On the other hand, they are called scheduled tasks on Windows-based systems.

Let’s take a quick look at a simple example of a *nix based cron job.

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

Very simple - it runs the statistics.sh file every five minutes!

While this is a very simple example, you will often need to implement more complex use cases. Complex systems require you to define multiple cron jobs that run at different intervals.

Let’s look at some of the tasks that complex web applications must perform regularly on the backend.

  • Clean unnecessary data in the database backend.
  • Update the front-end cache index to keep it up to date.
  • Calculate website statistics.
  • send email.
  • Back up different website elements.
  • Generate report.
  • there are more.

As you can see, there are a lot of things waiting to be run periodically and at different intervals. If you are an experienced system administrator, you can easily define cron jobs for all these tasks, but sometimes we as developers wish for an easier way.

Fortunately, Laravel comes with a built-in task scheduling API that allows you to define scheduled tasks in ways never before possible. Yes, the next section is about the basics of Laravel task scheduling.

Laravel Way

In the previous section, we covered the traditional way of setting up a cron job. In this section, we will introduce Laravel in detail in the context of the task scheduling API.

Before we continue, an important thing to understand is that the scheduling functionality provided by Laravel is just like any other functionality and is not called automatically. So if you think you don't need to do anything at the system level, I'd say you're out of luck.

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

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

The artisan command above invokes the Laravel scheduler, which then executes all pending cron jobs defined in the application.

Of course, we haven’t seen how to define scheduled tasks in a Laravel application, which is what we’ll discuss in depth next.

This is the schedule method of the App\Console\Kernel class, which you need to use if you wish to define application-specific scheduled tasks.

Continue to obtain the contents of the app/Console/Kernel.php file.

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

As you can see, the core code itself provides a useful example. In the example above, Laravel runs the inspire artisan command every hour. Don't you think the syntax is intuitive to begin with?

In fact, Laravel allows you to define scheduled tasks in many different ways:

  • Use closures/callables.
  • Call the artisan command.
  • Execute shell command.

In addition, there are a large number of built-in scheduling frequencies for you to choose from:

  • Every minute/every five minutes
  • Hourly/Daily/Weekly/Quarterly/Yearly
  • Specific time of day
  • there are more

In fact, I'd say it provides a complete set of routines so you can create custom cron jobs without touching the shell!

Yes, I can see that you are wondering how to implement your custom scheduled tasks, which is what I promised at the beginning of the article.

Create your first scheduled task in Laravel

As we discussed, Laravel allows you to define scheduled tasks in different ways. Let’s see how it works one by one.

Closure/Callable Method

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

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // the call method
        $schedule->call(function () {
          $posts = DB::table('posts')
            ->select('user_id', DB::raw('count(*) as total_posts'))
            ->groupBy('user_id')
            ->get();
     
          foreach($posts as $post)
          {
            DB::table('users_statistics')
              ->where('user_id', $post->user_id)
              ->update(['total_posts' => $post->total_posts]);
          }
        })->everyThirtyMinutes();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

如您所见,我们将闭包函数作为 call 方法的第一个参数传递。另外,我们将频率设置为每 30 分钟一次,因此它将每 30 分钟执行一次关闭函数!

在我们的示例中,我们计算每个用户的帖子总数并相应地更新 statistics 表。

artisan 命令

除了闭包或可调用之外,您还可以安排一个 artisan 命令,该命令将按一定的时间间隔执行。事实上,它应该是优于闭包的首选方法,因为它同时提供了更好的代码组织和可重用性。

继续使用以下内容修改 app/Console/Kernel.php 文件的内容。

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\UserStatistics'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('statistics:user')->everyThirtyMinutes();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

这是 command 方法,如果您希望安排 artisan 命令,如上面的代码片段所示,您可以使用该方法。您需要传递 artisan 命令签名作为 command 方法的第一个参数。

当然,你还需要在app/Console/Commands/UserStatistics.php处定义相应的artisan命令。

<?php
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
 
class UserStatistics extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'statistics:user';
 
  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Update user statistics';
 
  /**
   * Create a new command instance.
   *
   * @return void
   */
  public function __construct()
  {
    parent::__construct();
  }
 
  /**
   * Execute the console command.
   *
   * @return mixed
   */
  public function handle()
  {
    // calculate new statistics
    $posts = DB::table('posts')
      ->select('user_id', DB::raw('count(*) as total_posts'))
      ->groupBy('user_id')
      ->get();
     
    // update statistics table
    foreach($posts as $post)
    {
      DB::table('users_statistics')
      ->where('user_id', $post->user_id)
      ->update(['total_posts' => $post->total_posts]);
    }
  }
}

exec 命令

我们可以说到目前为止我们讨论的方法是特定于 Laravel 应用程序本身的。此外,Laravel 还允许您安排 shell 命令,以便您也可以运行外部应用程序。

让我们通过一个简单的示例来演示如何每天备份数据库。

<?php
namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 
class Kernel extends ConsoleKernel
{
  /**
   * Define the application's command schedule.
   *
   * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
   * @return void
   */
  protected function schedule(Schedule $schedule)
  {
    // exec method
    $host = config('database.connections.mysql.host');
    $username = config('database.connections.mysql.username');
    $password = config('database.connections.mysql.password');
    $database = config('database.connections.mysql.database');
     
    $schedule->exec("mysqldump -h {$host} -u {$username} -p{$password} {$database}")
      ->daily()
      ->sendOutputTo('/backups/daily_backup.sql');
  }
}

从代码中可以明显看出,您需要使用调度程序的 exec 方法,并且需要将要运行的命令作为其第一个参数传递。

除此之外,我们还使用了 sendOutputTo 方法,它允许您收集命令的输出。另一方面,有一个方法,emailOutputTo,它允许您通过电子邮件发送输出内容!

如何防止任务重叠

在本节中,我们将了解如何防止任务重叠。假设您已经定义了一个任务,并且您想要确保如果它已经在运行,Laravel 不应该运行同一任务的另一个实例。默认情况下,Laravel 将始终开始运行计划任务,即使同一任务的前一个实例已经在运行但尚未完成。

那么让我们看看如何避免重叠计划任务。

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

正如你所看到的,你只需要使用 withoutOverlapping 方法来确保 Laravel 不会与已经运行的任务重叠。默认情况下,Laravel 重叠任务之前的锁定时间为 24 小时。如果您想覆盖它,可以按照以下代码片段所示进行操作。

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

在上面的示例中,Laravel 等待 30 分钟才清除重叠锁。

如何定义后台任务

如果您同时安排多个任务,Laravel 会按顺序运行它们。因此,如果您有一个需要很长时间才能执行的任务,则下一个计划任务将不得不等待很长时间。为了避免这种情况,您可以在后台执行此类任务。

让我们快速看一下下面的示例,了解如何定义后台任务。

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

如您所见,您可以使用 runInBackground 方法来定义后台任务。

结论

今天,我们了解了 Laravel Web 框架中的任务调度 API。看到它如何轻松地管理需要定期运行的任务真是令人着迷。

在文章的开头,我们讨论了传统的设置计划任务的方式,接下来我们介绍了 Laravel 的设置方式。在本文的后半部分,我们通过几个实际示例来演示任务调度概念。

我希望您喜欢这篇文章,并且您应该对在 Laravel 中设置计划任务更有信心。对于那些刚刚开始使用 Laravel 或希望通过扩展来扩展您的知识、网站或应用程序的人,我们在 Envato Market 上提供了多种您可以学习的内容。

The above is the detailed content of Task scheduling in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP FTPNext article:PHP FTP