Using SQS with Shoryuken in Rails (basics)

Adel Saleh
CodeX

--

Background jobs is the processes that run in the background without affecting the normal operations in the system, and return a quick response to the user, it’s used to reduce the manual effort and to automate the process.

One of best ways to Setting up queueing for background jobs is Amazon Simple Queue Service (SQS) it’s is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. read more about Benefits in : https://aws.amazon.com/sqs/

Shoryuken is ruby gem that help us to work easily with SQS, and it is a super-efficient Amazon SQS thread-based message processor, support for continuously polling and thread based processing.

let’s Getting started

Firstly we need to add those gems to gem file and run the bundle install :

SetUp the Shoryuken configuration

Either you can specify Shoryuken.sqs_client with credentials in initializers like this :

Or Define it in shoryuken.yml file like this :

Now let’s define our worker like this :

and make sure queue name same like queue name in AWS or you can add it by Shoryuken : configuring-queues-in-Shoryuken .

Now we can call our worker and Enqueue a message and send it to SQS like this HelloWorker.perform_async('test sqs')

HelloWorker.perform_async(‘test sqs’)`
HelloWorker.perform_async(‘test sqs’)

Or if we need add delay to run the worker we can use like this :

HelloWorker.perform_in(interval(delay_seconds), 'test sqs')

HelloWorker.perform_in(3, test sqs)

Start Shoryuken

shoryuken -q queue_name_in_aws -R

read more for options in : Shoryuken-options

By running the shoryuken it will consuming and receiving the messages from SQS and start process them by calling the perform method and passing the sqs_msg and body.

That’s it 🤷‍♂️ , you can go deep in references and shoryuken gem and customize it with your needed .

I hope this article helpful to start with SQS. Have a nice day ☀️.

--

--

Adel Saleh
CodeX
Writer for

Software Engineer | Ruby || Ruby On Rails | Systems Integration