ssuhat
ssuhat

Reputation: 7656

Laravel Event Listeners and Manual Release

I'm using laravel docs using Event Listener with Manually Accessing The Queue.

here is my code:

$user = $event->user;

    if ($user->first_name == 'User1') {
        $this->release(30);
    }

    $this->mailer->send('emails.user.welcome', ['user' => $user], function ($m) use ($user) {
        $m->subject('Thank you for registering at ' . env('APP_NAME'))->to($user->email);
    });

The problem is the email is send before 30 secs and after 30 secs it send again. I've got problem understanding it.

Isn't it suppose to release at 30 secs (only once)?

Update: What I want try to achieve is, send the welcome mail after 30secs if the user name is user1.

Thanks.

Updated Code:

 if ($user->first_name == 'Stefen'  && $this->attempts() === 0){
        var_dump('this will send later');
        $this->release(10);
    } else{
        $this->mailer->send('emails.user.welcome', ['user' => $user], function ($m) use ($user) {
            $m->subject('Thank you for registering at ' . env('APP_NAME'))->to($user->email);
        });
    }

Upvotes: 0

Views: 676

Answers (1)

Josh
Josh

Reputation: 3288

The release method doesn't terminate the function, it simply pushes the job back n seconds into the queue, then continues with the rest of that handler. Try this instead:

    if ($user->first_name == 'User1' && $this->attempts() === 1)
    {
        $this->release(30);
    }
    else
    {
        // Mail time.
    }

More on queues:
https://laravel.com/docs/5.1/queues

Upvotes: 1

Related Questions