I am using queues for the first time and I don't seem to get it to work and laravel does not seem to throw any errors.
I am trying to queue mail on user registration, that is user should be redirected to dashboard immediately and the email should be queued. How do I know if queue is not working? on clicking register I have to wait for 8 seconds before I see dashboard, I get the email on registration but queue seems to fail.
I dont see any queue jobs on mysql jobs table at the time of registration.
Below are my settings:
jobs and jobs-failed table created using below command:
php artisan queue:table
php artisan queue:failed-table
php artisan migrate
.env
QUEUE_DRIVER=database
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=dummy@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME="App name"
RegisterController create() function
at the end of create function just before return $user;
dispatch(new NewRegisteredUser($user));
jobs file: NewRegisteredUser
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
use App\User;
use App\Mail\RegisteredUserWelcome;
class NewRegisteredUser implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new RegisteredUserWelcome($this->user);
Mail::to($this->user->email)->queue($email);
}
}
Mail file: RegisteredUserWelcome
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class RegisteredUserWelcome extends Mailable
{
use Queueable, SerializesModels;
protected $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->markdown('emails.registered')
->with([
'firstname' => $this->user->firstname,
'lastname' => $this->user->lastname,
]);
}
}
Am I missing something here? I dont get error and mail is sent but no queue jobs observed in mysql and long wait time on registration.
I am also confused how this: https://laravel.com/docs/5.4/mail#queueing-mail fits with this https://laravel.com/docs/5.4/queues Why do we need queues separately when we already have queuing in mail?
What should I use and how? All I need to do is reduce the wait-time of users and send mails behind the scene. Also, I am sending multiple emails in some cases. I guess this would help if I queue the jobs of multiple emails.