1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel QueueでJobに値を渡すときにハマった話

Posted at

概要

  • Laravel Queueを使って非同期処理をしたい
  • Jobにはテキストデータを渡し、それに従って処理をしたい
  • job っていうプロパティを使ってハマった話

やったこと

  • artisanでjobの雛形を作り
$ php artisan make:job Test
  • 値を受け取れるように変更
$ cat app/Jobs/Test.php

// 抜粋

    protected $job;

    public function __construct($job)
    {
        $this->job = $job;
    }

    public function handle()
    {
        dump($this->job);
    }
  • dispatchする
$ php artisan tinker
Psy Shell v0.9.7 (PHP 7.1.20 — cli) by Justin Hileman
>>> dispatch(new \App\Jobs\Test(1));
  • queue:workすると...
.
.
.
        "blade.compiler" => array:1 [ …1]
      ]
    }
    #encrypter: null
    #connectionName: "redis"
  }
  #job: "{"displayName":"App\\Jobs\\Test","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"App\\Jobs\\Test","command":"O:13:\"App\\Jobs\\Test\":7:{s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:7:\"chained\";a:0:{}}"},"id":"AEi0PsHbYjQaO8lAWqtmjDDCeV73ccnG","attempts":0}"
  #decoded: array:8 [
    "displayName" => "App\Jobs\Test"
    "job" => "Illuminate\Queue\CallQueuedHandler@call"
    "maxTries" => null
    "timeout" => null
    "timeoutAt" => null
    "data" => array:2 [
      "commandName" => "App\Jobs\Test"
      "command" => "O:13:"App\Jobs\Test":7:{s:6:"\x00*\x00job";N;s:10:"connection";N;s:5:"queue";N;s:15:"chainConnection";N;s:10:"chainQueue";N;s:5:"delay";N;s:7:"chained";a:0:{}}"
    ]
    "id" => "AEi0PsHbYjQaO8lAWqtmjDDCeV73ccnG"
    "attempts" => 0
  ]
  #reserved: "{"maxTries":null,"attempts":1,"timeout":null,"job":"Illuminate\\Queue\\CallQueuedHandler@call","id":"AEi0PsHbYjQaO8lAWqtmjDDCeV73ccnG","timeoutAt":null,"data":{"command":"O:13:\"App\\Jobs\\Test\":7:{s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:7:\"chained\";a:0:{}}","commandName":"App\\Jobs\\Test"},"displayName":"App\\Jobs\\Test"}"
  #instance: Illuminate\Queue\CallQueuedHandler {#793
    #dispatcher: Illuminate\Bus\Dispatcher {#792}
  }
  #container: Illuminate\Foundation\Application {#2}
  #deleted: false
  #released: false
  #failed: false
  #connectionName: "redis"
  #queue: "default"
}

QueueにはRedisを使っていたが、 Illuminate/Queue/Jobs/RedisJob オブジェクトが返ってきた。

修正

最初はLaravelのバージョンによって変数の受け取り方が違うのかと仕様をみていたが、どうやら job という名前がかぶっているだけっぽい?
他の名前で変数をうけるようにする

$ cat app/Jobs/Test.php

// 抜粋

    protected $work_id;

    public function __construct($work_id)
    {
        $this->work_id = $work_id;
    }

    public function handle()
    {
        dump($this->work_id);
    }
  • 小一時間はまった。。
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?