Home  >  Q&A  >  body text

Laravel, limit database queue to 1 email per minute

My application is hosted on a shared hosting platform which has a limit of 200 emails per hour.

My application is running Database Connection Driver and I have 3000 jobs in the jobs table.

I want to limit this queue to only send 1 email every 30 seconds or 1 minute to ensure there are no issues with my hosting.

Research: I tried this tutorial's delay, this question's rate limiting but no response, this laravel documentation's delay job, but nothing worked.

Question: Is there a way to limit the queues in database queue connection like in redis queue connection , i.e.

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

My implementation: Delay only the first job and then send the others immediately

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 file:**

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

P粉310931198P粉310931198334 days ago593

reply all(1)I'll reply

  • P粉448130258

    P粉4481302582023-12-14 09:36:35

    Have you run php artisanqueue:listen? If so, please check my code below maybe it will help

    In the controller:

    $mail = (
        [
           'data' => $EmailData,
           'userName' => $userData->first_name,
           'userMail' => $userData->email,
           'subject' => $subject
        ]);
                        
    SendMailJob::dispatch($mail)
        ->delay(now()->addSeconds($waitSec));
    $waitSec += 30; //seconds interval

    SendMailJob Class

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

    reply
    0
  • Cancelreply