snapsuzun/yii2-sqs

Yii2 Amazon SQS watcher extension for handle messages from SQS queues

Installs: 182

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 3

Forks: 5

Open Issues: 0

Type:yii2-extension

dev-master 2019-02-21 09:31 UTC

This package is auto-updated.

Last update: 2024-04-21 21:03:37 UTC


README

An extension for receiving and handling messages from Amazon SQS.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist snapsuzun/yii2-sqs

or add

"snapsuzun/yii2-sqs": "dev-master"

to the require section of your composer.json file.

Configuration

To use this extension, simply add the following code in your application configuration:

return [
    'bootstrap' => ['sqsWatcher']
    //....
    'components' => [
        'sqsClient' => function () {
            return new \snapsuzun\sqs\SqsClient([
                'cridentials => [
                    'key' => 'Api key of Amazon AWS',
                    'secret' => 'Secret key of Amazon AWS'
                ],
                'region' => 'Region of Amazon SQS',
                'version' => 'latest',
                'accountId' => 'ID of account in Amazon AWS',
                'queueNameAliases' => [
                    'aliasForQueue' => 'Real name of queue in Amazon SQS',
                    'test_queue' => 'TestQueue.fifo'
                ]
            ]);
        },
        'sqsWatcher' => [
            'class' => '\snapsuzun\sqs\watcher\Watcher',
            'queueName' => 'queue',
            'handler' => function (array $messages, array &$receiptHandlers, \snapsuzun\sqs\SqsClient $client) {
                // handle messages                
            }
        ],
    ],
];

Message Handler

Handling messages from SQS queue may be by callback function or object which implements \snapsuzun\sqs\watcher\HandlerInterface.

Example of object what implements \snapsuzun\sqs\watcher\HandlerInterface:

class ExampleHandler implements \snapsuzun\sqs\watcher\HandlerInterface 
{
        public $db = 'db';
   
        /**
         * @param array $messages
         * @param array $receiptHandlers
         * @param \snapsuzun\sqs\SqsClient $client
         */
        public function handleMessages(array $messages, array &$receiptHandlers, \snapsuzun\sqs\SqsClient $client)
        {
            // handle messages
        }
}

Usage ExampleHandler:

return [
    //.......
    'components' => [
        'sqsWatcher' => [
            //.....
            'handler' => ExampleHandler::class
        ]
    ]
];

Or:

return [
    //.......
    'components' => [
        'sqsWatcher' => [
            //.....
            'handler' => [
                'class' => ExampleHandler::class,
                'db' => 'db2'
            ]
        ]
    ]
];

When messages handling you can remove receipt handler from $receiptHandlers and message will not removed from SQS queue.

Console commands:

To listen queue you can use command:

./yii sqs-watcher/listen --isolate=1

If you want only handle messages in queue and not want to wait a new messages, you can use command:

./yii sqs-watcher/run --isolate=1

Option isolate enable creating a fork process for each batch of messages.