K
K
Konstantin Eliseev2019-06-05 16:38:54
Laravel
Konstantin Eliseev, 2019-06-05 16:38:54

How to implement a delay when sending emails via 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);
            });
        });
    }
}

I am posting via the website. For the limit, I decided to use queues and redis::throttle. Tell me why the script skips some data?
// foreach тут весь список
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  

//Redis::throttle часть позиций пропускает...
[2019-06-05 13:24:30] local.DEBUG: +  
[2019-06-05 13:24:30] local.DEBUG: [email protected]  
[2019-06-05 13:24:30] local.DEBUG: +  
[2019-06-05 13:24:33] local.DEBUG: +  
[2019-06-05 13:24:35] local.DEBUG: [email protected]  
[2019-06-05 13:24:35] local.DEBUG: +  
[2019-06-05 13:24:38] local.DEBUG: +  
[2019-06-05 13:24:40] local.DEBUG: [email protected]  
[2019-06-05 13:24:40] local.DEBUG: +  
[2019-06-05 13:24:43] local.DEBUG: +

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
maddog670, 2019-06-05
@K0r5hun

https://laravel.com/docs/5.8/queues#delayed-dispatching

E
Eugene, 2017-07-04
@Flashter

date("Y-m-d H:i:s", strtotime("+1 month"))

A
Arman, 2017-07-04
@Arik

In the sense of just adding 1 month to some date?

$date = '2017-07-04';

$dateAt = strtotime('+1 MONTH', strtotime($date));

$newDate = date('Y-m-d', $dateAt);

echo $newDate;

S
Stalker_RED, 2017-07-04
@Stalker_RED

php.net/manual/en/datetime.add.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question