【原创】Laravel 定时计划任务

定时计划任务文件

在 Console/Commands 下创建计划任务文件,例如:DeleteMatchCrontab.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class DeleteMatchCrontab extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'crontab:delete-match';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '删除匹配记录';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 处理业务内容
    }
}

注册计划任务

Kernel.php

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            Artisan::call("crontab:delete-match");
        })->dailyAt('1:30');
    }

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

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

发表回复

电子邮件地址不会被公开。必填项已用 * 标注