lewis4u
lewis4u

Reputation: 15037

Slow down Laravel worker

I have hooked up Redis with Laravel for queuing emails and all is fine... but in dev environment I use mailtrap.io (free version).

And the problem is that mailtrap allows to receive only 2 emails per second, so I never get all the emails that are queued, because redis sends emails like crazy... maybe 10 per/s

Is there a way somehow to slow down the queue so that it sends max 2 mails per second?

Upvotes: 0

Views: 608

Answers (1)

Luis Josue Uscanga
Luis Josue Uscanga

Reputation: 90

Yes It is a solution and they're called jobs :) you can create a file to send emails exclusively in the Jobs Folder and a class like this

class SendPushNotification extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $pushNotification;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(PushNotification $pushNotification)
    {
        $this->pushNotification = $pushNotification;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->pushNotification->send();
    }
}

And then call the class in the controller

$this->dispatch(new SendPushNotification($pushNotification))->delay(1);

Delay is for seconds you can create a constructor with an array to receive two emails o many you want maybe some var than can be changed by the .env to change the number of emails per second

Upvotes: 1

Related Questions