[英]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.