LoginSignup
118
129

More than 5 years have passed since last update.

laravelでバッチ作ってcronで動かしてみた

Last updated at Posted at 2015-08-16

まずはバッチクラスを作る

php artisan make:console GetItemFeed --command="getitem"

このコマンドを実行すると、Console command created successfullyと表示され、下記のようなクラスが自動で作られます。

このartisanってなんなのかよく理解出来てないけど、勝手に必要なクラスが作られるのは便利。
てか、まじでartisanってなにww
こういうやり方ってフレームワークだと一般的なんでしょうか!?
誰か教えて下さいー!!

app/Console/Commands/GetItemFeed.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GetItemFeed extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'getitem';

    /**
     * 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()
    {
        //
    }
}

コメントを見た感じ、handleメソッドにメインの処理を書いていくっぽいです。

早速実行してみた

$ php artisan getitem

  [InvalidArgumentException]         
  Command "getitem" is not defined.  

getitemは未定義だよ!と怒られました。

app/Console/Kernel.phpに今回つくったコマンドのクラス定義を追加する必要があるみたいです。

app/Console/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 = [
        \App\Console\Commands\Inspire::class,
        \App\Console\Commands\GetItemFeed::class, // ←今回の追加
    ];

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

追加したので、先ほどのクラスのhandleメソッドにhello!と出力するだけの処理を書いて実行してみた。

$ php artisan getitem
hello

よし出来た!

cron登録してみる

laravelではcrontabに1つ1つのバッチを登録せずとも、
下記のようにcrontabを書いておけば、app/Console/Kernel.phpのscheduleメソッドに書かれたバッチを指定された時間に動作させることが可能。

* * * * * php /path/to/project/artisan schedule:run 1>> /dev/null 2>&1
app/Console/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 = [
        \App\Console\Commands\Inspire::class,
        \App\Console\Commands\GetItemFeed::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('getitem')
                 ->hourly(); // <- 新たに追加!

    }
}

これで1時間おきにgetitemが動くっぽい!
よし!

スケジュールの設定方法について

laravelの公式サイトに色々書いてあった
http://laravel.com/docs/master/scheduling

118
129
3

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
118
129