Laravelで複数実行したいとき

マルチプロセスで複数に実行してほしい場合は、JOBクラスを利用します。

利用例として、重たい処理や処理終了を待たなくても、次の処理をすぐに実行したいときに利用します。

コマンドでJobクラスを非同期に実行する。

namespace App\Console\Commands;

use App\Jobs\TestJob;

class Test extends Command
{
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $job = new TestJob();
        $this->dispatch();
    }
}

Jobクラスでは以下のような内容となる。

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class TestJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        //処理内容を記載する。
    }
}

関連記事

コメント

この記事へのコメントはありません。

カテゴリー

お問い合わせはコチラ!