Implement Laravel Queue with AWS SQS

Introduction

Here is about this article:

  1. Send Email with Laravel Queue
  2. Use AWS SQS

So why do we need to use queue?
When we execute some jobs that require longer time, like sending Emails, or uploading photos or videos, let users to wait until the jobs are completed is just not so practical.
So when users request some time consuming jobs, we need to use queue to line them, and execute them on the background, and so the users could be released from current request immediately.

Apply AWS SQS service

  • Firstly, you need an AWS account
  • Apply SQS on AWS
  • Refer to official document to complete the setting.
  • Keep the information below for further usage.
  • On up-right corner, click your account, and choose My Security Credentials

  • Click Users to create a new user

  • Give user id, and check Programmic access, and next

  • Click Create group as photo below

  • Add the user we just created into this newly created group

  • Then Add tags is optional, you could skip it

  • Now we have Access key ID and Secret access key. If you are afraid of forgetting them, you could download them. By the way, Secret access key will only be accessible this time, you will need to reproduce it if you forget it.

Implement Laravel Queue

Configure AWS SQS

  • The following operation is referred to official document
  • Install AWS official SDK as referred in official document,under the repository:
    composer require aws/aws-sdk-php
  • Configure .env file
    QUEUE_CONNECTION=sqs
    SQS_KEY=theKeyWeGotAbove
    SQS_SECRET=theSecretKeyWeGotAbove
    SQS_QUEUE=testSQS
    SQS_REGION=ap-northeast-1
    SQS_PREFIX=theURLAboveWithoutQueueName

Create jobs

php artisan make:job ProcessPodcast
  • job example:
    <?php

    namespace App\Jobs;

    use App\Helpers;
    use Illuminate\Bus\Queueable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;

    class SendMailWhenOrderPlaced implements ShouldQueue
    {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $job;

    // The maximum attempts of this job
    public $tries = 5;

    /**
    * Create a new job instance.
    *
    * @return void
    */
    public function __construct($order, $FB_email, $Local_email)
    {
    $this->order = $order;
    $this->FB_email = $FB_email;
    $this->Local_email = $Local_email;
    $this->job = Helpers::mailWhenOrderPlaced($order, $FB_email, $Local_email);
    }

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

The example above is to execute a function of sending Email called mailWhenOrderPlaced with queue in Ray’s project.

Use dispatch

  • Wherever we want to execute this job, we use dispatch
    SendMailWhenOrderPlaced::dispatch($order, $FB_email, $Local_email);

Execute queue

  • Under the project
    php artisan queue:work

Test

  • Now, when the dispatch is executed, it will send Email with queue

Conclusion

It’s so easy, isn’t it?
By the way, because we use queue, so we have to make sure that qeeue works well. In this case, if the queue fails, then the Email function will fail too.
To guarantee that queue works well, and automatically restart after fails, we need to use Supervisor to help us monitor and manage processes.
If you are interested in Supervisor, you could take a look on Ray’s another article here
If you are also interested in how to use Laravel Mail and AWS SES, to send mail, you could take a look on Ray’s another article here as well

Use `Laravel` `template` and `blade` Deploy Supervisor on AWS and MacOS

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×