Laravel queue redis 发送email 邮件
一、新建queue 任务,目的就是把数据存到redis队列
php artisan make:job SendRegisterVerifyEmail
<?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 Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; class SendPriceEmail implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ protected $sendInfo; public function __construct($sendInfo) { // $this->sendInfo = $sendInfo; } /** * 消费队列发送邮件邮件 * Execute the job. * * @return void */ public function handle() { Log::info("start",['SendPriceEmail queue starting']); Log::info("start", $this->sendInfo); $member_info = $this->sendInfo['member']; $item = $this->sendInfo['item']; if(!filter_var($member_info['email'], FILTER_VALIDATE_EMAIL)){ return; } $subject = $member_info['cas']."/".$member_info['prod_name']."/"."已报价"; Mail::send('new_home.email.email_quote',['member_info'=>$member_info,'item'=>$item],function($message) use ($member_info, $subject){ $message ->to($member_info['email'])->subject($subject); }); } }
二、业务逻辑,存入redis队列
if(InquiryQuotationItem::whereIn("id", $quotation_id_arr)->update(['is_push'=>1])){ Inquiry::where("id","=",$inquiry_id)->update(['status'=>2]); //发送报价提醒给客户 //if($member_info['email'] && $member_info['cas']) $this->sendQuoteEmail($member_info,$res); //已经改成从redis 队列发送邮件,添加数据进SendPriceEmail队列里 $this->dispatch((new SendPriceEmail($sendPriceEmailArr))->onQueue("SendPriceEmail")); return Y::success('推送成功!',["code"=>"SUCCESS"]); }else{ return Y::success('推送失败!',["code"=>"ERROR"]); }
第一步第二步操作完就会把数据存储到redis队列里
三、消费队列数据,发送邮件
php artisan queue:work redis --queue=SendPriceEmail --sleep=3 --tries=3
把这条命令加入到守护进程里面
[program:ProdSeedJob] process_name=%(program_name)s_%(process_num)02d command=php /var/www/html/artisan queue:work redis --queue=SendPriceEmail --sleep=3 --tries=3 autostart=true autorestart=true user=www-data numprocs=3 redirect_stderr=true stdout_logfile=/var/www/html/storage/logs/worker_SendPriceEmail.log
四、发送失败任务时,生成c_failed_jobs 表
php artisan queue:failed-table php artisan migrate
五、job代码修改后,需要执行,才生效
php artisan queue:restart