Giter Club home page Giter Club logo

sqs-queue-bundle's Introduction

Simple AWS SQS Queue for Symfony

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' => '[email protected]',
            'to' => '[email protected]',
            '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

Behaviour Arguments Description
sendMessage Message $message
int $delay = 0
Delivers a message to the specified queue.
receiveMessage int $limit = 1 Retrieves one or more messages (up to 10), from the specified queue. Using the WaitTimeSeconds parameter enables long-poll support. For more information, see Amazon SQS Long Polling in the Amazon SQS Developer Guide.
deleteMessage string $receiptHandle Deletes the specified message from the specified queue. You specify the message by using the message's receipt handle and not the MessageId you receive when you send the message. Even if the message is locked by another reader due to the visibility timeout setting, it is still deleted from the queue. If you leave a message in the queue for longer than the queue's configured retention period, Amazon SQS automatically deletes the message.
purge Deletes the messages in a queue specified by the QueueURL parameter. Note: you can't retrieve a message deleted from a queue.

Queue Manager Behaviours

You could access QueueManager via service tritran.sqs_queue.queue_manager

Behaviour Arguments Description
listQueue string $prefix = '' Returns a list of your queues. The maximum number of queues that can be returned is 1,000. If you specify a value for the optional prefix parameter, only queues with a name that begins with the specified value are returned.
createQueue string $queueName
array $queueAttribute
Creates a new standard or FIFO queue. You can pass one or more attributes in the request.
deleteQueue string $queueUrl Deletes the queue specified by the QueueUrl, regardless of the queue's contents. If the specified queue doesn't exist, Amazon SQS returns a successful response.
setQueueAttributes string $queueUrl
array $queueAttribute
Sets the value of one or more queue attributes. When you change a queue's attributes, the change can take up to 60 seconds for most of the attributes to propagate throughout the Amazon SQS system
getQueueAttributes string $queueUrl Gets attributes for the specified queue.

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

Behaviour Description
tritran:sqs_queue:create Creates a new standard or FIFO queue. You can pass one or more attributes in the request.
tritran:sqs_queue:update Update queue attribute based on its configuration which shown in config.yml
tritran:sqs_queue:delete Delete a queue by url and all its messages
tritran:sqs_queue:attr Retrieve the attribute of a specified queue
tritran:sqs_queue:purge Deletes the messages in a queue specified by the QueueURL parameter.
tritran:sqs_queue:worker Start a worker that will listen to a specified SQS queue
tritran:sqs_queue:ping Send a simply message to a queue, for DEBUG only

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

sqs-queue-bundle's People

Contributors

n-m avatar petehouston avatar trandangtri avatar wcomnisky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sqs-queue-bundle's Issues

Sending Emails

Hi,

I am trying to send an email from worker but the email is not send. Swiftmailer is working correctly, because i can send emails from other commands.

Make the worker configuration optional

While configuring the bundle, a worker MUST be defined in queue configuration. It should be possible to use the bundle just for publishing messages without the worker.

Consider the use-case:
App A, B, and C populate the queue for a microservice app D. The apps A, B, and C do not need/have the worker; only the publisher. In order to use this bundle in publisher apps, they have to create a fake queue worker.

Add ability to reset `$error`

In case the worker is run as a CLI command and an exception is thrown, the $error property storing the message can never be reset leading to false reports in case in case you use, say LoggerInterface::error() to report it.

Add a method to reset the $error property, like a setter or make the property's visibility to protected.
At the moment, the only way to "reset" it, is to use reflection and set the property to accessible.

Error Handling in AbstractWorker::process

Hi,
I'm having difficulty finding a way to log the exception messages that may occur while processing the message:

    try {
        $result = $this->execute($message);
    } catch (\Exception $e) {
        return false;
    }

Maybe remove the try/catch and let the developer catch his own exceptions?

Thanks.

Cannot run phpunit

PHP version: 7.1.15
Debug logs:
PHP Fatal error: Cannot redeclare static Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::$container as non static TriTran\SqsQueueBundle\Tests\app\KernelTestCase::$container in /app-vendor/tritran/sqs-queue-bundle/Tests/app/KernelTestCase.php on line 16 PHP Stack trace: PHP 1. {main}() /usr/local/bin/phpunit:0 PHP 2. PHPUnit\TextUI\Command::main() /usr/local/bin/phpunit:574 PHP 3. PHPUnit\TextUI\Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:153 PHP 4. PHPUnit\TextUI\Command->handleArguments() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:169 PHP 5. PHPUnit\Util\Configuration->getTestSuiteConfiguration() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:849 PHP 6. PHPUnit\Util\Configuration->getTestSuite() phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php:924 PHP 7. PHPUnit\Framework\TestSuite->addTestFiles() phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php:1022 PHP 8. PHPUnit\Framework\TestSuite->addTestFile() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:418 PHP 9. PHPUnit\Util\FileLoader::checkAndLoad() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:340 PHP 10. PHPUnit\Util\FileLoader::load() phar:///usr/local/bin/phpunit/phpunit/Util/FileLoader.php:49 PHP 11. include_once() phar:///usr/local/bin/phpunit/phpunit/Util/FileLoader.php:63 PHP 12. spl_autoload_call() /app-vendor/tritran/sqs-queue-bundle/Tests/Functional/Command/QueueAttCommandTest.php:13 PHP 13. Composer\Autoload\ClassLoader->loadClass() /app-vendor/tritran/sqs-queue-bundle/Tests/Functional/Command/QueueAttCommandTest.php:13 PHP 14. Composer\Autoload\includeFile() /app-vendor/tritran/sqs-queue-bundle/vendor/composer/ClassLoader.php:322 PHP 15. include() /app-vendor/tritran/sqs-queue-bundle/vendor/composer/ClassLoader.php:444

Failed to createQueue

AWS version:
"aws/aws-sdk-php-symfony": "~2.0"
Logs:
Found 1 error while validating the input provided for the CreateQueue operation: [Attributes][ContentBasedDeduplication] must be a string or an object that implements __toString(). Found /app-vendor/aws/aws-sdk -php/src/functions.php:252: bool(false)
Solution: set 'ContentBasedDeduplication' => 'false' in TriTran\SqsQueueBundle\Service\QueueManager

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.