LoginSignup
3
2

More than 3 years have passed since last update.

Laravel Task Schedulerで動的にタスクを実行する

Posted at

Laravelには標準でタスクスケジューラーがあるため指定した時刻に、指定した処理を実行したい場合にはかなり簡単にかける。

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 = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //ここにコマンドを呼び足す処理と、時刻の指定その他条件を書くだけ 
        $schedule->command('backup:clean')->dailyAt('3:00')->environments('production')->onOneServer();
    }

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

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

基本的にはコマンドを作成したりして、毎分実行されるscheduleメソッドの中に追記していけばいいが、困るのはDBなどから設定をとってきてそれらの時刻設定などをもとに実行する処理がある場合。この場合においてもLaravelであればかなり簡単にかける。

Model

単純にcron書式の文字列だけ持つモデルをサンプルとして作成(ie, ['id' => 1, 'cron' => '* * * * *'])

Scheduler.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Scheduler extends Model
{
    protected $fillable = ['cron'];
}

Command

動的に設定された時刻に実行するテスト用のコマンドを生成、単純にログを書き込むだけ。

sample.php
<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       Log::info('scheduler called');
    }
}

動的スケジュール

Kernel.phpにDBからとってきた値に基づいて、コマンドを呼び出すように処理を追記。

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 = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        //DBから取得
        $schedulers = Scheduler::all();
        foreach($schedulers as $scheduler) {
          //普通にコマンドを呼び出すが、時刻設定の部分にcronを使用して、取得したcron書式の文字列を渡す。
          $schedule->command('log:test')->cron($scheduler->cron);
        }
    }

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

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

こんな感じでログ取れていればOK

storage/logs/laravel-2020-10-10.log
[2020-10-11 23:33:14] development.INFO: scheduler called  

まとめ

今回は、汎用性が高そうだったので、時刻指定の部分にcronを使用したが、用件に合わせてdailyAtなどの文字列を渡してもいい。
DBからスケジュールの情報を持ってきてコマンドを呼び出すという処理をschedulerメソッドの中に書いているため、呼び出しメソッドが増えていくと、可読性が下がるし、重くなる。どんな方法でハンドリングしていくのがいいかまでは試せていないので悪しからず。

結構動的に処理しなければいけないものが多かったり、重複実行してしまうのがまずかったりする場合にはwithoutOverlappingを使用したり、本番のみ実行させたい時にはenvironments、単一サーバー上で実行したい場合にはonOneServerなどをチェーンして書くことも可能。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2