【发布时间】:2019-06-05 19:52:51
【问题描述】:
我为该网站撰写时事通讯。我想限制每分钟发送的信件(队列)。对于限制,我决定使用队列和 redis::throttle。但是当我运行 php artisan queue:work --tries=2 时,日志中的一些电子邮件丢失了......
// Console command
$mailingList = MailingList::find(1);
dispatch(new SendDailyNewsletter($mailingList));
//App\Jobs\SendDailyNewsletter.php
class SendDailyNewsletter implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
//...
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);
}, function () {
return $this->release(5);
});
});
}
}
输出:
// foreach here all emails good
[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 some emails missed. Why?
[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: +
告诉我为什么脚本会跳过一些数据(电子邮件)?
【问题讨论】: