Coder Social home page Coder Social logo

nejdetkadir / sidekiq-aws-sqs Goto Github PK

View Code? Open in Web Editor NEW
24.0 1.0 5.0 20 KB

Sidekiq extension that provides an easy way to poll and process messages from AWS SQS (Simple Queue Service) queues within a Sidekiq worker

License: MIT License

Ruby 98.74% Shell 1.26%
aws sidekiq sqs sqs-consumer sqs-poller sqs-queue sqs-queues worker ruby ruby-gem

sidekiq-aws-sqs's Introduction

Gem Version test rubocop Ruby Style Guide Ruby Version

Sidekiq::AWS::SQS

sidekiq-aws-sqs is a Sidekiq extension that simplifies the integration of AWS SQS queues with Sidekiq workers, by abstracting away the details of polling, processing and error handling of SQS messages. It aims to provide a reliable and flexible way to consume messages from SQS, while allowing the user to customize the polling behavior and SQS client options as needed.

At its core, sidekiq-aws-sqs uses the SafePoller gem to implement a background thread that polls messages from the specified SQS queue at a specified interval. The polling logic is encapsulated in a poll method that is meant to be called from a Sidekiq worker, passing a block of code that will process each message received. The poll method returns a handle to the SafePoller instance, which can be used to start, stop, pause or resume the polling process as needed. The polling process can also be configured to stop after a specified time or date, or to stop automatically when Sidekiq shuts down or terminates.

To use sidekiq-aws-sqs in a Sidekiq worker, you simply need to include the Sidekiq::AWS::SQS module and call the poll method with the desired options and block of code. You can also customize the SQS client and polling options by setting class-level variables, or by passing them as options to the poll method.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add sidekiq-aws-sqs

Or add the following line to the application's Gemfile:

gem 'sidekiq-aws-sqs', github: 'nejdetkadir/sidekiq-aws-sqs', branch: 'main'

If bundler is not being used to manage dependencies, install the gem by executing:

gem install sidekiq-aws-sqs

Usage

require 'sidekiq/aws/sqs'

class MyWorker
  include Sidekiq::Worker
  include Sidekiq::AWS::SQS::Worker

  sqs_options queue_url: 'https://sqs.foo.amazonaws.com/123/bar',
              wait_time_seconds: 20, # optional, default: 20
              destroy_on_received: true, # optional, default: false
              max_number_of_messages: 10, # optional, default: 10
              client: Aws::SQS::Client.new # optional if global config is set to Sidekiq::AWS::SQS.config.sqs_client

  def perform(message)
    parsed_message = JSON.parse(message, symbolize_names: true)

    puts "Received message: #{parsed_message[:body]}"
  end
end

Configuration

You can configure the global options for all sqs workers by creating an initializer file in config/initializers/sidekiq_aws_sqs.rb and setting the options as shown below.

# config/initializers/sidekiq_aws_sqs.rb

require 'sidekiq/aws/sqs'
require 'aws-sdk-sqs'

Sidekiq::AWS::SQS.configure do |config|
  config.sqs_client = Aws::SQS::Client.new # global SQS client for all sqs workers

  # you must set the your sqs workers here for registering them to sidekiq aws sqs
  config.sqs_workers = [
    MyWorker
  ]

  # global polling options for all sqs workers
  config.wait_time_seconds = 20 # optional, default: 20
  config.destroy_on_received = true # optional, default: false
  config.max_number_of_messages = 10 # optional, default: 10
  config.logger = Sidekiq.logger # optional, default: Sidekiq.logger
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nejdetkadir/sidekiq-aws-sqs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Sidekiq::Aws::Sqs project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

sidekiq-aws-sqs's People

Contributors

nejdetkadir avatar petergoldstein 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

Watchers

 avatar

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.