Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

pipefy/pipefy_message

Repository files navigation

Pipefy Message

This project is a Ruby gem that provides a simple way to produce and consume messages for asynchronous processing.

The current implementation supports AWS SNS for sending messages (by publishing them to topics) and AWS SQS for receiving them (via queue polling).

Requirements

  • Ruby 2.7.6
  • Bundler
  • GNU Make
  • Docker (optional, for tests)
  • Docker Compose (optional, for tests)

Installation

Add this line to your application's Gemfile:

  gem 'pipefy_message'

And then execute:

  bundle install

Or install it yourself as:

  gem install pipefy_message

Usage

Publisher

To use the publisher capabilities it is required to "import our gem" at the desired class, create an instance of the Publisher class and call the publish method on it. See the example below:

require "pipefy_message"

##
# Example publisher class.
class PublisherExampleClass
  def awesomeLogic
    
    ## business logic
    
    payload = { foo: "bar" }
    publisher = PipefyMessage::Publisher.new
    result = publisher.publish(payload, "pipefy-local-topic")
    puts result ## will print some data like the messageID and so on
  end
end

Consumer

To use the consumer capabilities it is required to "import our gem" at your consumer class, include the abstraction, define the perform method and finally call the method process_message on the consumer class (not an instance of it) to start the consuming process, see the example below:

require "pipefy_message"

##
# Example consumer class.
class ConsumerExampleClass
  include PipefyMessage::Consumer
  options queue_name: "pipefy-local-queue"

  def perform(message)
    puts "Received message #{message} from broker"
    ## Fill with your business logic here
  end
end

Start PipefyMessages Consumer

To start consumer inside a Rails applications:

bundle exec pipefymessage -w ConsumerExampleClass -R

Development - Test

Run make help to see a list of all Make targets to help in common development activities.

To avoid the management of multiple Ruby versions installations or in case of problems to install Ruby 2.x, you can use an isolated container for development:

  make run-dev-env

To test changes without installing this dependency on your application, on your terminal go to the project root and execute:

ℹ️ Environment Variables info.

ENABLE_AWS_CLIENT_CONFIG allows connection to localstack in lieu of AWS

ASYNC_APP_ENV specifies the current environment (staging|development)

  export ENABLE_AWS_CLIENT_CONFIG="true"
  export ASYNC_APP_ENV="development" 
    
  make build-app
  make build-app-infra

If you need to recreate the infra (SNS and SQS) run:

  make recreate-app-infra

After that, we are going to test the gem with these commands:

  irb

On the irb console:

  • Publish a message

    require_relative 'lib/samples/my_awesome_publisher.rb'
    publisher = MyAwesomePublisher.new
    publisher.publish
  • Consume a message

    require_relative 'lib/samples/my_awesome_consumer.rb'
    MyAwesomeConsumer.process_message

Project Stack

Brokers Documentation

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pipefy/pipefy_message.

Follow the template while opening a PR