我的應(yīng)用程式託管在共享託管平臺(tái)上,該平臺(tái)限制每小時(shí) 200 封電子郵件。
我的應(yīng)用程式正在執(zhí)行資料庫(kù)連線(xiàn)驅(qū)動(dòng)程式,且作業(yè)表中有 3000 個(gè)作業(yè)。
我想限制此佇列,使其每 30 秒或 1 分鐘僅發(fā)送 1 封電子郵件,以確保我的託管不會(huì)出現(xiàn)問(wèn)題。
研究: 我嘗試過(guò)本教學(xué)的延遲、此問(wèn)題的速率限制但沒(méi)有回應(yīng)、此 laravel 文件的延遲作業(yè),但沒(méi)有任何效果。
問(wèn)題:有沒(méi)有辦法像在redis佇列連接中那樣限制資料庫(kù)佇列連線(xiàn)中的佇列,即
// Allow only 1 email every 30 seconds Redis::throttle('any_key')->allow(1)->every(30)->then(function () { Mail::to($this->email)->send(new NotificationEmail($this->data) ); Log::info('Emailed order ' . $this->email); }, function () { // Could not obtain lock; this job will be re-queued return $this->release(2); });
我的實(shí)作:只延遲第一個(gè)作業(yè),然後立即發(fā)送其他作業(yè)
public function sendEmailNotification($email,$data) { //Send email to user and to admin $email_job = (new ProcessEmailNotificationJob($email,$data))->delay(now()->addSeconds(30)); if($this->dispatch($email_job)){ return true; } else{ return false; } }
**ENV 檔案:**
BROADCAST_DRIVER=log CACHE_DRIVER=file QUEUE_CONNECTION=database SESSION_DRIVER=file SESSION_LIFETIME=120 MEMCACHED_HOST=127.0.0.1
你運(yùn)行php artisanqueue:listen了嗎,如果是,請(qǐng)檢查我下面的程式碼也許會(huì)有幫助
在控制器內(nèi):
#$mail = ( [ 'data' => $EmailData, 'userName' => $userData->first_name, 'userMail' => $userData->email, 'subject' => $subject ]); SendMailJob::dispatch($mail) ->delay(now()->addSeconds($waitSec)); $waitSec += 30; //seconds interval
SendMailJob 類(lèi)別
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use App\Mail\ClientRegistrationNotification; use Mail; class SendMailJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $bulkMail, $mail; public function __construct($Mail) { $this->mail=$Mail; } public function handle() { try { Mail::to($this->mail['userMail']) ->queue(new ClientRegistrationNotification($this->mail['data'], $this->mail['userName'], $this->mail['userMail'], $this->mail['subject'])); } catch (\Throwable $exception) { $this->fail(); } } }