Как реализовать задержку при отправке писем через Redis::throttle?

// Консольная команда
$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;

class SendDailyNewsletter 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);
            });
        });
    }
}


Делаю рассылкку через сайт. Для лимита решил использовать очереди и redis::throttle. Подскажите, почему некоторые данные скрипт пропускает?
// foreach тут весь список
[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 часть позиций пропускает...
[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: +
  • Вопрос задан
  • 422 просмотра
Решения вопроса 2
@K0r5hun Автор вопроса
//Command
class SendDailyNewsletterCommand extends Command {
        //...
        $subscriptions->each(function ($subscription) {
            SendDailyNewsletter::dispatch($subscription->email)->delay(10);
        });
}

// Job
class SendDailyNewsletter 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);
        });
    }
}
// Лог файл
//[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
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы