2015-06-22 26 views
4

我想在我的控制器中排隊函數的一部分,主要是因爲它訪問第三方API並從所述請求計算某些信息。我也想這樣做來增加我對隊列的瞭解!Laravel - 將函數的一部分轉換爲排隊語句

,我想排隊的代碼是:

,將需要與此if statement$postcode$clinic ID(這是上面的語句,想通了)推動的唯一變量。

if($clinic->postcode != $postcode) 
    { 

    $client = new Client([ 'base_uri' => 'https://api.postcodes.io/','timeout' => 2.0, 'verify' => false ]); 
    $response = $client->get('postcodes/'.$postcode)->getBody(); 

    $input = json_decode($response); 

    $clinic->latitude = $input->result->latitude; 
    $clinic->longitude = $input->result->longitude; 
    $clinic->save(); 
} 

到目前爲止,我已經創建了queue表和遷移它。

我然後跑了命令:php artisan make:job GetClinicLatAndLongPoints --queued

我的問題是,我怎麼可以把這個功能GetClinicLatAndLongPoints裏面包括通過這兩個變量在這樣做呢?

我到目前爲止有:

public function handle(Clinic $clinic, $postcode) 
    { 

    } 

但我不能確定如何佈置的事情了!任何指導將非常感激。

+0

你使用的是什麼版本的laravel,你同時標記了兩個!大聲笑 – Luceos

+0

哎呀,對不起!我已經刪除了Laravel 4.我正在使用最新的Laravel 5.1。*! – Ben

+0

您是否嘗試將它粘貼到您的句柄函數中?還要確保你的隊列正在運行('php artisan queue:listen') – Luceos

回答

1

你可以通過你的Clinic模型的實例和郵政編碼,以你的工作的構造函數,可以沿着

namespace App\Jobs; 

use App\Clinic; 
use App\Jobs\Job; 
use GuzzleHttp\Client; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Bus\SelfHandling; 
use Illuminate\Contracts\Queue\ShouldQueue; 

class GetClinicLatAndLongPoints extends Job implements SelfHandling, ShouldQueue 
{ 
    use InteractsWithQueue, SerializesModels; 

    private $clinic; 
    private $postcode; 

    public function __construct(Clinic $clinic, $postcode) 
    { 
     $this->clinic = $clinic; // Laravel will automatically deserialize the model instance 
     $this->postcode = $postcode; 
    } 

    public function handle() 
    { 
     $coordinates = $this->getCoordinates($this->postcode); 

     if (! is_null($coordinates)) { 
      // You may want to add error handling 
      $this->clinic->latitude = $coordinates['latitude']; 
      $this->clinic->longitude = $coordinates['longitude']; 
      $this->clinic->save(); 
     } 
    } 

    private function getCoordinates($postcode) 
    { 
     $client = new Client(['base_uri' => 'https://api.postcodes.io/','timeout' => 2.0, 'verify' => false]); 
     $response = json_decode($client->get('postcodes/'.$postcode)->getBody()->getContents()); 
     if (! $response || json_last_error() !== JSON_ERROR_NONE) { 
      return null; 
     } 
     if ($response->status == 200 && 
      isset($response->result->latitude) && 
      isset($response->result->longitude)) { 
      return [ 
       'latitude' => $response->result->latitude, 
       'longitude' => $response->result->longitude 
      ]; 
     } 
     return null; 
    } 
} 

在控制器的線條看起來你發送你的工作

if ($clinic->postcode != $postcode) { 
    $this->dispatch(new GetClinicLatAndLongPoints($clinic, $postcode)); 
} 

附註:雖然Laravel帶有數據庫隊列驅動程序,但在生產中使用它並不是一個好主意。最好使用job queues之一,即beanstalkd

+0

多麼美妙的答案,非常清楚!非常感謝您的幫助@peterm!作爲答案的補充,我不得不在Controller中添加'使用App \ Jobs \ GetClinicLatAndLongPoints'。 我現在遇到的唯一問題是GetClinicLatAndLongPoints腳本中出現以下錯誤 - GetClinicLatAndLongPoints.php中的FatalErrorException第13行:找不到類'App \ Jobs \ Job'。 GetClinicLat ...不在'Job'文件夾內,也就是說,只是「Jobs」文件夾。我不確定這是爲什麼? – Ben

+0

事實證明,我沒有默認的'Job.php'應該與Laravel一起出現!複製這一切使一切正常工作。非常感謝。 – Ben