簡體   English   中英

如何處理隊列超時的大量郵件發送

[英]How to handle mass mail sending with queue timeout

我有 10 萬多封電子郵件的表格,我想每天發送一些 email:

我在 app\Console\Kernel.php 中添加了時間表:

$schedule->job(new SendDailyEmails)->dailyAt('09:00');

內部工作我有:

$users = User::all();
foreach($users as $user){
    Maill:to($user->email)->send(new DailyMail($user));
    $status = 'sent';
    if( in_array($user->email, Mail::failures()) ){
        $status = 'failed';
        Log::error($user->email . ' was not sent.');
    }else{
        Log::info($user->email . ' was sent.');
    }
    SentMail::create([
        'email' => $user->email,
        'status' => $status
    ]);

}

這工作正常,但一段時間后這可能會因為作業超時而停止。 在 failed_jobs 表中,我收到MaxAttemptsExceededException消息,提示 Job 嘗試了太多次或運行時間過長。 由於我在主管中將隊列嘗試最多設置為 3,因此它應該只執行 go 3 次。 通過測試它並沒有嘗試再次嘗試,因為我收到了一封郵件而不是 3 封。

所以到了超時,我不確定默認值是什么,但這有關系嗎,因為我不知道發送所有電子郵件需要多少時間?

我應該將郵件分成 50 個一組並為每個組調用單獨的作業實例嗎?

有人對此有很好的工作答案嗎?

如果您查看官方文檔,您會發現每封郵件都可以排隊。

所以,你應該從

Mail:to($user->email)->send(new DailyMail($user));

Mail:to($user->email)->queue(new DailyMail($user));

通過這種方式,您將工作生成的每封郵件都推入隊列。 我建議您創建一個特定的隊列並使用像laravel 地平線這樣的系統,以便更好地監控。

還要記住分塊和延遲發送,因為您的應用程序可能會運行超時錯誤,而且如果像mailgun這樣的提供商看到異常活動,他們可能會阻止您的發送。

與其嘗試在 class 中一次發送 100k+ 封電子郵件,不如將 100k+ 作業實例分派給隊列工作者

$users = User::all();
foreach($users as $user){
  $schedule->job(new SendDailyEmails($user))->dailyAt('09:00');
}

現在Laravel將在隊列中堆疊 100k+ 個作業,並嘗試一次為每個用戶發送一個 email

class SendDailyEmails implements ShouldQueue
{
  public $user;

  public function __construct(User $user)
  {
    $this->user = $user;
  }

  Maill:to($this->user->email)->send(new DailyMail($user));
    $status = 'sent';
    if( in_array($this->user->email, Mail::failures()) ){
        $status = 'failed';
        Log::error($this->user->email . ' was not sent.');
    }else{
        Log::info($this->user->email . ' was sent.');
    }
    SentMail::create([
        'email' => $this->user->email,
        'status' => $status
    ]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM