tritran/sqs-queue-bundle

Simple SQS Queue for Symfony

Installs: 214 288

Dependents: 0

Suggesters: 0

Security: 0

Stars: 27

Watchers: 6

Forks: 19

Open Issues: 8

Type:symfony-bundle

2.1.4 2020-02-19 08:55 UTC

README

This bundle provides an easy way to work with AWS SQS

SensioLabsInsight Latest Stable Version Latest Unstable Version Build Status codecov

Installation

Follow 5 quick steps to setup this bundle.

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require tritran/sqs-queue-bundle

This command requires you to have Composer installed globally

Step 2: Enable the Bundle

Register bundles in app/AppKernel.php:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        return [
            // ...
            new \Aws\Symfony\AwsBundle(),
            new \TriTran\SqsQueueBundle\TriTranSqsQueueBundle(),
        ];
    }

    // ...
}

In a default Symfony application that uses Symfony Flex, bundles are enabled/disabled automatically for you when installing/removing them, so you could ignore this step.

Step 3: Update AWS SQS Credential

This bundle is using AWS SDK for PHP. Full documentation of the configuration options available can be read in the SDK Guide.

Below are sample configuration for AWS Credential in YAML format

# app/config/config.yml

aws:
    version: latest
    region: us-central-1
    credentials:
        key: not-a-real-key
        secret: "@not-a-real-secret"

Step 4: Configure the Queues

Below are sample configuration for some queues in YAML format

# app/config/config.yml

tritran_sqs_queue:
    sqs_queue:
        queues:
            emailpool:
                queue_url: 'https://sqs.eu-central-1.amazonaws.com/49504XX59872/emailpool'
                worker: "@acl.service.emailpool"
                attributes:
                    receive_message_wait_time_seconds: 20
                    visibility_timeout: 30
            reminder:
                queue_url: 'https://sqs.eu-central-1.amazonaws.com/49504XX59872/reminder'
                worker: 'AclBundle\Service\Worker\ReminderWorker'

Full documentation of the queue options available can be read in the Queue Attributes.

Now, you could access to queue emailpool or reminder service via tritran.sqs_queue.emailpool or tritran.sqs_queue.reminder, it's an interface of BaseQueue

Below are a sample implementation of sending a message to a specified queue

namespace AclBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use TriTran\SqsQueueBundle\Service\Message;

/**
 * Class DefaultController
 *
 * @package AclBundle\Controller
 */
class DefaultController extends Controller
{
    public function indexAction()
    {
        // ...
        
        $data = [
            'from' => 'sender@domain.com',
            'to' => 'receiver@domain.com',
            'subject' => 'Greeting Message',
            'body' => 'Congratulation! You have just received a message which was sent from AWS SQS Queue'
        ];
        $this->get('tritran.sqs_queue.emailpool')
            ->sendMessage((new Message())->setBody(serialize($data)));

        // ...
    }
}

For a FIFO queue, you must associate a non-empty MessageGroupId with a message. Otherwise, the action fails.
You may provide a MessageDeduplicationId explicitly. If you aren't able to provide a MessageDeduplicationId and you enable ContentBasedDeduplication for your queue, Amazon SQS uses a SHA-256 hash to generate the MessageDeduplicationId using the body of the message (but not the attributes of the message).
For more information about FIFO queue, please take a look at Amazon SQS FIFO (First-In-First-Out) Queues

Queue Behaviours

Queue Manager Behaviours

You could access QueueManager via service tritran.sqs_queue.queue_manager

Step 5: Setup a worker

Below are a sample implementation of a worker, which will listen to a queue to handle the messages inside.

namespace AclBundle\Service\Worker;

use TriTran\SqsQueueBundle\Service\Message;
use TriTran\SqsQueueBundle\Service\Worker\AbstractWorker;

class ReminderWorker extends AbstractWorker
{
    /**
     * @param Message $message
     *
     * @return boolean
     */
    protected function execute(Message $message)
    {
        echo 'The message is: ' . $message->getBody();

        return true;
    }
}

And then you could make it executed as daemon in console via:

bin/console tritran:sqs_queue:worker reminder

Note: reminder is the name of queue which you configured in the config.yml in step 4.

Appendix: Useful Console Commands

Note: Please using -h for more information for each command.