gpt4 book ai didi

php - Laravel 5.3 队列作业不工作

转载 作者:行者123 更新时间:2023-12-05 04:11:49 25 4
gpt4 key购买 nike

我正在尝试使用 Laravel database 队列发送我的电子邮件操作然而这个过程仍然在我的浏览器中继续而不是在后面工作。

这是我的 Controller

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

最佳答案

您的作业的构造函数不应调用 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'));
});
}
}

关于php - Laravel 5.3 队列作业不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41887447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com