How to implement a delay when sending messages via Redis::throttle?

// Console command
$mailingList = MailingList::find(1);
dispatch(new SendDailyNewsletter($mailingList));

//SendDailyNewsletter
namespace App\Jobs;
use App\Entity\Subscription\MailingList;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Mail;

SendDailyNewsletter class implements ShouldQueue
{
 use InteractsWithQueue, Queueable, SerializesModels;

 protected $mailing_list;

 public function __construct(MailingList $mailingList)
{
 $this->mailing_list = $mailingList;
}

 public function handle()
{
 $subscriptions = DB::table('mail as m')->select(['m.email'])->where('m.id', $this->mailing_list->id)->get();

 $subscriptions->each(function ($subscription) {
logger($subscription->email);
});

 $subscriptions->each(function ($subscription) {
logger('+');
 Redis::throttle('key')->allow(1)->every(5)->then(function () use ($subscription) {
logger($subscription->email);
 //Mail::to($subscription->email)->queue(new DailyNewsletterMail($subscription->name));
 }, function () {
 return $this->release(5);
});
});
}
}


Make rassylku through the site. For a limit I decided to use a queue and redis::throttle. Tell me why some of these script skips?
// here the foreach list
[2019-06-05 13:24:30] local.DEBUG: korwru@example.com 
[2019-06-05 13:24:30] local.DEBUG: test@example.com 
[2019-06-05 13:24:30] local.DEBUG: jackson33@example.com 
[2019-06-05 13:24:30] local.DEBUG: hollie.emmerich@example.com 
[2019-06-05 13:24:30] local.DEBUG: nbrakus@example.com 
[2019-06-05 13:24:30] local.DEBUG: estrella.christiansen@example.com 
[2019-06-05 13:24:30] local.DEBUG: elinor.frami@example.com 

//Redis::throttle position of the skips...
[2019-06-05 13:24:30] local.DEBUG: + 
[2019-06-05 13:24:30] local.DEBUG: korwru@example.com 
[2019-06-05 13:24:30] local.DEBUG: + 
[2019-06-05 13:24:33] local.DEBUG: + 
[2019-06-05 13:24:35] local.DEBUG: jackson33@example.com 
[2019-06-05 13:24:35] local.DEBUG: + 
[2019-06-05 13:24:38] local.DEBUG: + 
[2019-06-05 13:24:40] local.DEBUG: nbrakus@example.com 
[2019-06-05 13:24:40] local.DEBUG: + 
[2019-06-05 13:24:43] local.DEBUG: +
March 23rd 20 at 19:13
2 answers
March 23rd 20 at 19:15
Solution
A Redis::throttle for the same purpose? - Kobe14 commented on March 23rd 20 at 19:18
@Kobe14, not quite this number of requests in some period of time - celestin commented on March 23rd 20 at 19:21
March 23rd 20 at 19:17
Solution
//Command
class SendDailyNewsletterCommand extends Command {
//...
 $subscriptions->each(function ($subscription) {
SendDailyNewsletter::dispatch($subscription->email)->delay(10);
});
}

// Job
SendDailyNewsletter class implements ShouldQueue
{
 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

 protected $email;

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

 public function handle()
{
 Redis::throttle('key')->allow(1)->every(5)->then(function () {
 // send email to subscriber
logger($this->email);
 }, function () {
 // could not obtain lock, retry this job in 5 seconds.
 return $this->release(5);
});
}
}
// Log file
//[2019-06-25 09:13:11] local: sdfsdf@korwru.ru 
//[2019-06-25 09:13:16] local: gusin.matvei@example.net 
//[2019-06-25 09:13:21] local: subbotin.elvira@example.com 
//[2019-06-25 09:13:26] local: faina92@example.org 
//[2019-06-25 09:13:31] local: lidiy04@example.com 
//[2019-06-25 09:13:36] local: sdfsdf@wolq.net 
//[2019-06-25 09:13:41] local: polina51@example.net

Find more questions by tags Laravel