侧边栏壁纸
  • 累计撰写 168 篇文章
  • 累计创建 71 个标签
  • 累计收到 35 条评论

目 录CONTENT

文章目录

Laravel 8 Cron 任务调度教程

神兵小将
2021-10-06 / 0 评论 / 0 点赞 / 1,428 阅读 / 3,509 字 / 正在检测是否收录...

创建新命令

在这个 Cron 调度任务的第一步中,我们将创建一个 Laravel 自定义命令。这个自定义命令将帮助我们运行我们的后台任务。
phpartisancommandlaravelcrontaskschedulertutorial.webp

所以要创建自定义命令,我们使用artisan命令。

$ php artisan make:command TestCron --command=test:cron

当我们运行此命令时,它会在/app/Console 中创建一个Commands文件夹(如果它不存在)。在此文件夹中,我们将有一个名为/app/Console/Commands/TestCron.php 的文件
截图_选择区域_20211008184518.png

设置 Cron 任务
当你打开 TestCron.php 文件时,你会发现一些受保护的变量,它们实际上将你的命令引入 laravel artisan 面板。

protected $signature = 'test:cron';

protected $description = 'This is a sample cron to log message';

在那个文件中,最重要的方法是handle()。此方法在 cron 触发时运行。在这里,我们有更新的代码。将给定的代码复制并粘贴到您的 TestCron.php 文件中。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;


class TestCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is a sample cron to log message';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // Simple Cron to log message
        Log::info("Successfully, cron is running");

        //return 0;
    }
}

在此 handle() 方法中,您可以添加任何类型的工作任务,例如发送电子邮件、将数据插入数据库、进行数据转储等。

让我们将此 cron 安排到任务调度程序。

注册为任务计划程序

接下来,要注册 Cron 作业,请转到/app/Console并找到一个名为Kernel.php的文件。我们需要在我们想要触发的任何时间在这里注册我们创建的 cron 命令。

这里是我们可用于任务调度的触发时间(调度频率)列表。
截图_选择区域_20211008185134.png

方法描述
->cron('* * * * *');按自定义 cron 计划运行任务
->everyMinute();每分钟运行一次任务
->everyTwoMinutes();每两分钟运行一次任务
->everyThreeMinutes();每三分钟运行一次任务
->everyFourMinutes();每四分钟运行一次任务
->everyFiveMinutes();每五分钟运行一次任务
->everyTenMinutes();每十分钟运行一次任务
->everyFifteenMinutes();每十五分钟运行一次任务
->everyThirtyMinutes();每三十分钟运行一次任务
->hourly();每小时运行一次任务
->hourlyAt(17);每小时 17 分钟后运行一次任务
->everyTwoHours();每两小时运行一次任务
->everyThreeHours();每三个小时运行一次任务
->everyFourHours();每四小时运行一次任务
->everySixHours();每六小时运行一次任务
->daily();每天午夜运行任务
->dailyAt('13:00');每天13:00运行任务
->twiceDaily(1, 13);每天 1:00 和 13:00 运行任务
->weekly();每周日 00:00 运行任务
->weeklyOn(1, '8:00');每周一 8:00 运行任务
->monthly();每月第一天 00:00 运行任务
->monthlyOn(4, '15:00');每月4日15:00运行任务
->twiceMonthly(1, 16, '13:00');每月1日16日13:00运行任务
->lastDayOfMonth('15:00');每月最后一天 15:00 运行任务
->quarterly();每季度第一天00:00运行任务
->yearly();在每年的第一天 00:00 运行任务
->yearlyOn(6, 1, '17:00');每年6月1日17:00运行任务
->timezone('America/New_York');设置任务的时区

这是 Kernel.php 文件的更新代码。

<?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 = [
        Commands\TestCron::class,
    ];

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

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

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


成功,我们已经将 Cron 命令注册到 Kernel.php

测试作业调度任务

打开终端并输入此命令

$ php artisan schedule:run

当我们运行时,它会执行我们添加到 Kernel.php 文件中的命令。所以我们有一个。该 cron 会将一些日志添加到日志文件中。

您将在 /storage/logs/laravel.log中找到日志文件,在该文件中 cron 将添加语句Successfully, cron is running。

当一次又一次地运行 artisan schedule 命令时,它会去并添加到文件中。现在让我们使用 crontab 控制台命令将其设置为永久。

在 Crontab 中设置 Cron 任务
打开终端并输入命令

$ crontab -l

此命令将列出您添加的所有 cron。现在添加新的 cron 任务,再次返回终端并输入命令

$ crontab -e

它将为您的终端打开一个界面以注册或添加新任务。所以让我们添加我们创建的 cron

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
          • *表示我们的任务将每分钟运行一次
  • /path-to-your-project这是laravel应用项目的路径
  • php artisan schedule:run这里我们正在运行我们的 cron

我们希望本文能帮助您以非常详细的方式了解 Laravel 8 Cron 任务调度教程。

0

评论区