![](/img/trans.png)
[英]Laravel Job executed immediately in browser instead of running in a background queue
[英]Laravel queue is not doing job in the background
我正在尝试使用 Laravel 队列发送批量电子邮件。 到目前为止,我已经写下了逻辑并且它工作正常,但问题是当我在控制器中编写逻辑时需要很多时间,所以我想使用作业,但问题仍然存在。
我的问题
我的问题是,即使我使用队列,我也无法在后台发送电子邮件。
控制器
public function newsletter(Request $request)
{
//dd($request->all());
dispatch(new SendEmail($request));
Session::flash('message', 'Email Sent');
Session::flash('class', 'success');
return redirect()->route('news');
}
工作
public function handle(Request $request)
{
//
$data = array(
'message' => $request->message,
'subject' => $request->subject,
'file' => $request->file("file")
);
$teachingLevel = $request->highest_teaching_level;
$school = $request->school;
$province = $request->province;
$district = $request->district;
$subject = $request->subject;
if ($teachingLevel != "" && $school != "" && $province != "" && $district != "") {
$email = User::where('highest_teaching_level', $teachingLevel)->where('current_school_name', $school)->where('address','LIKE', '%'.$province.'%')->where('address','LIKE', '%'.$district.'%')->pluck('email');
}else{
$email = User::pluck('email');
}
foreach($email as $e)
{
Mail::to($e)->send(new NewsLetter($data, $subject));
}
}
电子邮件已发送,但不会在后台发生。 也许这与我在handle()
函数中传递$request
变量的方式有关。
任何帮助将不胜感激。 谢谢!
以下是我在项目中使用 Laravel 作业的方式:
示例作业.php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Services\SampleService;
class SampleJob implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// if you omit this value, you'll be in trouble, believe me
public $tries = 1;
private $param;
public function __construct($param) {
$this->param = $param;
}
public function handle(SampleService $service) {
// do something with $this->param
// Also, notice that you can type hint classes in handle function declaration for DI
$service->doSomething($this->param);
}
}
示例控制器.php
namespace App\Http\Controllers;
use App\Jobs\SampleJob;
class SampleController extends Controller {
public function sampleMethod(Request $request) {
$param = $request->input('param');
SampleJob::dispatch($param); // $param will be passed to SampleJob constructor
// ...
}
}
值得注意的几点是:
php artisan queue:table && php artisan migrate
php artisan make:job Sample
php artisan queue:work
。 让它在后台运行: sudo nohup php artisan queue:work > ./storage/logs/queue-worker.log &
php artisan queue:work
在后台运行常见的陷阱:
$tries
参数,并且您的工作以某种方式抛出错误,laravel 将尝试一次又一次地重试该工作,直到您的数据库关闭:(Log
,十次有九次你会面临storage
目录的权限问题。 为避免此问题,请将'permission' => '0666'
到config/logging.php
日志通道设置php artisan queue:restart
启动队列工作器。我的 Laravel 版本:5.8
如果您打算使用“数据库”连接来运行下一次迁移:
php artisan queue:table
php artisan migrate
还有一个事件和一个实现“ShouldQueue”接口的侦听器,最后在“providers/EventProvider.php”路径和“EventProvider.php”文件中注册与一个或多个侦听器关联的事件,添加你的事件和侦听器以下一个符号为例:
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
了解与以下 queue:restart 命令相关的几点很重要
php artisan queue:restart
为此,您需要运行队列侦听器:
php artisan queue:listen
参考: https : //medium.com/ariel-mejia-dev/run-queues-in-the-background-on-development-in-laravel-8d697d812f29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.