我的應用程式託管在共享託管平台上,該平台限制每小時 200 封電子郵件。
我的應用程式正在執行資料庫連線驅動程式,且作業表中有 3000 個作業。
我想限制此佇列,使其每 30 秒或 1 分鐘僅發送 1 封電子郵件,以確保我的託管不會出現問題。
研究: 我嘗試過本教學的延遲、此問題的速率限制但沒有回應、此 laravel 文件的延遲作業,但沒有任何效果。
問題:有沒有辦法像在redis佇列連接中那樣限制資料庫佇列連線中的佇列,即
// 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); });
我的實作:只延遲第一個作業,然後立即發送其他作業
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
P粉4481302582023-12-14 09:36:35
你運行php artisanqueue:listen了嗎,如果是,請檢查我下面的程式碼也許會有幫助
在控制器內:
#$mail = ( [ 'data' => $EmailData, 'userName' => $userData->first_name, 'userMail' => $userData->email, 'subject' => $subject ]); SendMailJob::dispatch($mail) ->delay(now()->addSeconds($waitSec)); $waitSec += 30; //seconds interval
SendMailJob 類別
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(); } } }