My application is hosted on a shared hosting platform that limits to 200 emails per hour.
My application is running database connection driver and I have 3000 jobs in the jobs table.
I would like to throttle this queue to only send 1 email every 30 secs or 1 minute to ensure I don’t run issues with my hosting.
Research:
I have tried delay from this tutorial, rate limiting from this question with no response, delayed jobs from this laravel documention but nothing works.
Question: Is there a way to throttle the queue in database queue connection like they do it 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: Delays only for the first job then sends the others with no delay
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
2
Answers
I'm not sure if this is the best option but the only solution that worked for me was sleeping the queue for 30 seconds after dispatch i.e
Dispatch Method
Sleep Queue Sender
did you run php artisan queue:listen, if yes check my below code maybe it will help
inside your controller:
SendMailJob class