2017-01-27 108 views
2

我想用Laravel發送我的發送郵件的動作database隊列 但是,這個過程仍然在我的瀏覽器中繼續,而不是在後面工作。Laravel 5.3隊列作業不起作用

這是我的控制器

protected function importUserExcel(UploadedFile $file, Request $request){ 

    $user_role = Role::where('name','=','user')->first(); 


    \Excel::load($file, function($reader) use ($user_role) { 
     $excel = $reader->select()->get(); 
     foreach($excel[0] as $line){ 
      $user = User::firstOrnew([ 
       'email' => $line['email']]); 
      $user->email = $line['email']; 
      $user->name = $line['name']; 
      $user->password= bcrypt(srand(15)); 

      $user->town = $line['town']; 
      $user->dealer_code = $line['dealer_code']; 
      $user->type = $line['type']; 
      // $user->save(); 
      $user->sendUserEmail(); 
      //$user->attachRole($user_role); 


     } 
    }); 
} 

這是我的模型功能

public function sendUserEmail() 
{ 
    $delay = Carbon::now()->addMinutes(15); 
    \Log::info("Request Begins"); 
    $user = new SendEmails($this); 
    $user->delay($delay); 
    dispatch($user); 
    \Log::info("Request Ends"); 
} 

,這是我的工作

類SendEmails實現ShouldQueue { 使用InteractsWithQueue,QUEUEABLE,SerializesModels;

/** 
* Create a new job instance. 
* 
* @return void 
*/ 
public function __construct(User $user) 
{ 
    $this->handle($user); 

} 

/** 
* Execute the job. 
* 
* @return void 
*/ 
public function handle(User $user) 
{ 
    $broker = $user->broker; 
    $brokerInstance = \Password::broker($broker); 
    view()->share('locale', app()->getLocale()); 
    $response = $brokerInstance->sendResetLink([ 'email' => $user->email ], function (Message $message) { 
     $message->subject(trans('emails.welcome_subject')); 
    }); 
} 

}

然而結果似乎即將最終沒有延遲或排隊任何東西。同時我的瀏覽器也處理,而不是把流程放在後面。

enter image description here

+0

您可以發佈完整的控制器功能嗎? –

+0

ypdated @VũTuấnAnh –

+0

你的代碼似乎很好,除了你的日誌。嘗試將您的日誌推送到'SendEmails'類。如果您將Logger推入模型中,則無法看到延遲時間。順便說一下,在你的控制器功能應該返回查看發送響應瀏覽器 –

回答

1

你的工作的構造不應該通話手柄()方法;它應該只設置句柄方法所需的屬性。由隊列工作者調用句柄方法。

如果您設置每個請求的區域設置,則您對app() - > getLocale()的調用可能不正確;一項工作是從另一個進程執行的,沒有中間件或相關的http請求。

class SendEmails implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; 
    protected $user; 

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

    public function handle() { 
     $user = $this->user; 
     $broker = $user->broker; 
     $brokerInstance = \Password::broker($broker); 

     view()->share('locale', app()->getLocale()); 
     $response = $brokerInstance->sendResetLink([ 'email' => $user->email ], function (Message $message) { 
      $message->subject(trans('emails.welcome_subject')); 
     }); 
    } 
}