问题
将 Laravel 5.7 与 Redis 一起使用,我已经使用 Stephen Mudere 在 如何使用 laravel 5 中的队列通过电子邮件发送密码重置链接中描述的方法将电子邮件验证和密码重置通知排队,但我不知道如何评价- 限制那些特定的排队通知。因为我的应用程序会出于各种原因(不仅仅是这两个目的)发送电子邮件,而且我的电子邮件服务器的速率限制为每分钟 30 封电子邮件,所以我需要对“电子邮件”队列中的所有内容进行速率限制。
背景
根据 Laravel 队列文档,在处理方法的作业类中使用
Redis::throttle('key')->allow(10)->every(60)->then(function () {
// Job logic...
}, function () {
// Could not obtain lock...
return $this->release(10);
});
问题是我没有使用工作课程,而是使用通知。例如,对于密码重置,我创建了以下内容
ResetPassword Class
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
class ResetPassword extends ResetPasswordNotification implements ShouldQueue
{
use Queueable;
}
使用以下方法从用户模型中调用它:
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}
方法
我试图通过修改 User 模型中的 sendPasswordResetNotification 函数来解决这个问题:
public function sendPasswordResetNotification($token)
{
Redis::throttle('email')->allow(2)->every(60)->then(function () use($token) {
$this->notify(new ResetPasswordNotification($token));
}, function () {
// Could not obtain lock...
return $this->release(10);
});
}
请注意,出于测试目的,油门的值是人为降低的。这似乎部分起作用。在上面的示例中,如果我尝试两次连续的密码重置,电子邮件会排队并发送。当我尝试发送第三封电子邮件(超过我设置的每分钟 2 封的限制)时,我收到了 BadMethodCallException, "Call to undefined method App\User::release()"
. 我知道这是因为 User 模型没有发布方法,但它又回到了我不确定到底在哪里或如何使用节流逻辑的问题。有没有办法修改它以使其工作,或者我需要采取完全不同的方法来发送这些消息?
更新:由于不同原因而失败的替代方法
我从使用通知切换到使用作业,以便我可以根据文档使用 Redis::throttle。为了设置队列消息的作业,我使用了How to queue Laravel 5.7 "email verification" email shipping中的方法。这对于发送排队的电子邮件效果很好。然后我试图限制进入队列的工作。这是我的完整方法:
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Redis;
class QueuedVerifyEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
Redis::throttle('email')->allow(2)->every(60)->then(function () {
$this->user->notify(new VerifyEmail);
}, function() {
return $this->release(10);
});
}
}
这些进入队列,但随后失败。在堆栈跟踪中如下:
Symfony\Component\Debug\Exception\FatalThrowableError: Class 'App\Jobs\Redis' not found in /home/vagrant/code/myapp/app/Jobs/QueuedVerifyEmail.php:27
当我有一个 use 语句来定义正确的位置时,我无法弄清楚它为什么要在 App\Jobs 中寻找 Redis 外观。