Friday, July 15, 2016

Laravel - AWS SQS Queuing

There are many different drivers for Laravel for the queuing service, the default is "sync". We can use queues on external service like Amazon SQS.Documentation is based on the SQS messaging service.
  • Add the following dependency in the composer.json file
"require": {
        "aws/aws-sdk-php": "^3.14"
}
  • Run the following command to install the package
composer update
  • Configuration for the queue.
Configuration file is stored in config/queue.php. Make the following changes.
  • Configure the connection parameters for SQS

'sqs' => [
'driver' => 'sqs',
'key' => <Access Key ID>,
'secret' => <Secret Access Key>,
'prefix' => 'https://<Region>.amazonaws.com/<Your Account Id>',[eg:https://sqs.eu-west-1.amazonaws.com/11111111111]
'queue' => <Queue Name>[eg:halo-renewals],
'region' => <region>[eg:eu-west-1],
],
  • Configure the default driver (.env file)
QUEUE_DRIVER=sqs
  • Creating Job classes (We are creating two job classes to send email to the user.Welcome mail and the reminder mail). Issue the following commands from the terminal
 php artisan make:job SendWelcomeEmail
php artisan make:job SendReminderEmail
          Jobs will be stored in the app/jobs directory
    • Make the following changes in your job files.
SendWelcomeEmail.php
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Mail\Mailer;

class SendWelcomeEmail extends Job implements ShouldQueue
{


use InteractsWithQueue, SerializesModels;

protected $emailAddress;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($emailAddress)
{

$this->emailAddress = $emailAddress;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Mailer $mailer)
{

$mailer->send('emails.welcome', [], function($message) {
$message->from('<From email id>', 'Queue Test Mail');
$message->to($this->emailAddress)->subject('Welcome Email');

});
}
}

SendReminderEmail.php

<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Mail\Mailer;

class SendReminderEmail extends Job implements ShouldQueue
{


use InteractsWithQueue, SerializesModels;

protected $emailAddress;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($emailAddress)
{

$this->emailAddress = $emailAddress;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Mailer $mailer)
{

$mailer->send('emails.reminder', [], function($message) {
$message->from('<From email id>', 'Queue Test Mail');
$message->to($this->emailAddress)->subject('Reminder Email');

});
}
}
  • Create following template files for the email content in the resources/views/emails folder
  • welcome.blade.php
  • reminder.blade.php
  • Create the controller to push the jobs in the queue.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Jobs\SendReminderEmail;
use App\Jobs\SendWelcomeEmail;

class MyController extends Controller
{

public function Pushjobs(){
$hardCodedEmail = '<To Email Address>';
$job = (new SendWelcomeEmail($hardCodedEmail));

$this->dispatch($job);
$job->delete();

$hardCodedEmail = '<To Email Address>';
$job = (new SendReminderEmail($hardCodedEmail));

$this->dispatch($job);
$job->delete();

}
}
Pushjobs method will push two jobs into the AWS SQS.(Welcome email and Reminder email)
  • Run the jobs as they are pushed into the queue.Run the following command from the terminal.
php artisan queue:listen
Laravel will pull the jobs from the SQS and process it.

1 comment:

  1. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating AWS Online Traning

    ReplyDelete