首页  >  问答  >  正文

Laravel,将数据库队列限制为每分钟 1 封电子邮件

我的应用程序托管在共享托管平台上,该平台限制每小时 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粉310931198P粉310931198335 天前597

全部回复(1)我来回复

  • P粉448130258

    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();
                 }
        }
    }

    回复
    0
  • 取消回复