简体   繁体   English

Laravel + SQS消息每秒处理一次,未从队列中删除

[英]Laravel + SQS Messages are processed every second and not removed from queue

I have set up SQS with my Laravel 5 (Spark) app and it is running great (ish) 我已经使用Laravel 5(Spark)应用程序设置了SQS,并且运行良好(ish)

I have an event that is generated each time a new account is created. 每次创建新帐户时都会生成一个事件。 This event triggers a mail class which implements ShoudlQueue. 此事件触发一个实现ShoudlQueue的邮件类。

When running: 运行时:

php artisan queue:listen

The same messages in SQS are shown as processing every second. SQS中的相同消息显示为每秒处理一次。 Is there something I need to do on so that my events mark the queue messages as complete and remove them from the SQS queue? 我需要做些什么以使我的事件将队列消息标记为完成并将其从SQS队列中删除吗?

In my store user function: 在我的商店用户功能中:

event(new UserCreated($user));

This then fires the following event: 然后,将触发以下事件:

class UserCreated
{
    use SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @param User  $user
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

I then have an event subscriber set up: 然后,我设置了一个事件订阅者:

class UserEventSubscriber
{

    public function onUserCreated($event)
    {
        Mail::to($event->user)->queue(new UserCreated($event->user)); 
    }

    public function subscribe($events)
    {
        $events->listen(
            'App\Events\User\UserCreated',
            'App\Listeners\UserEventSubscriber@onUserCreated'
        );
    }
}

And finally the mail class: 最后是邮件类:

namespace App\Mail;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class UserCreated extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.users.test');
    }
}

I've had better luck with... 我的运气更好...

php artisan queue:work --daemon

...when working with Laravel's queues and SQS particularly. ...尤其是在使用Laravel的队列和SQS时。

Also, your UserCreated class is missing implements ShouldQueue . 另外,您的UserCreated类缺少implements ShouldQueue

I'm not sure if you just left it out of your snippet. 我不确定您是否只是将其放在代码段之外。 But you would want to make sure your UserEventSubscriber is registered in your EventServiceProvider class as well. 但是,您还要确保UserEventSubscriber也已在EventServiceProvider类中注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM