Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away