我正在尝试使用Laravel database
队列发送我的发送电子邮件操作
但是这个过程仍然在我的浏览器中继续而不是在后面工作。
这是我的控制器
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'));
});
}
}
然而,结果似乎最终不会延迟或排队任何事情。 与此同时,我的浏览器也会处理,而不是将进程放在后面。答案 0 :(得分:1)
你的工作构造函数不应该调用handle()方法;它应该只设置handle方法所需的属性。由队列工作人员调用handle方法。
如果您要为每个请求设置区域设置,则您对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'));
});
}
}
答案 1 :(得分:0)
您可以通过以下方式重试(我假设您在 Laravel 文档中做了说明,但总有一天它不起作用):
jobs
。php artisan migrate
php artisan queue:work