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

Commit

Permalink
Merge pull request #18 from pipefy/eh-main/feature/consumer
Browse files Browse the repository at this point in the history
Eh main/feature/consumer
  • Loading branch information
eHattori authored May 4, 2022
2 parents 4bf3cfc + dbe13de commit a30e740
Show file tree
Hide file tree
Showing 39 changed files with 1,103 additions and 488 deletions.
4 changes: 4 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export AWS_ACCESS_KEY_ID=foo
export AWS_SECRET_ACCESS_KEY=bar
export AWS_ENDPOINT="http://localhost:4566"
export ENABLE_AWS_CLIENT_CONFIG=true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ build-iPhoneSimulator/
# .rubocop-https?--*

.idea
.rspec_status
.vscode
.rspec_status
12 changes: 11 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
AllCops:
TargetRubyVersion: 2.6
NewCops: enable

Style/StringLiterals:
Enabled: true
Expand All @@ -13,4 +14,13 @@ Layout/LineLength:
Max: 120

Metrics/BlockLength:
IgnoredMethods: ['describe', 'context']
IgnoredMethods: ["describe", "context"]

Lint/MissingSuper:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/AbcSize:
Enabled: false
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ group :development, :test do
gem "pry-doc"
gem "rspec", "~> 3.0"
gem "rubocop", "~> 1.21"
gem "solargraph"
end
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,27 @@ GEM
aws-sigv4 (~> 1.1)
aws-sigv4 (1.4.0)
aws-eventstream (~> 1, >= 1.0.2)
backport (1.2.0)
benchmark (0.2.0)
coderay (1.1.3)
concurrent-ruby (1.1.9)
diff-lcs (1.3)
e2mmap (0.1.0)
i18n (1.10.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.4)
jmespath (1.5.0)
kramdown (2.3.2)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
logger (1.3.0)
method_source (1.0.0)
minitest (5.15.0)
nokogiri (1.13.3-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.13.3-x86_64-linux)
racc (~> 1.4)
parallel (1.21.0)
parser (3.0.2.0)
ast (~> 2.4.1)
Expand All @@ -47,9 +59,12 @@ GEM
pry-doc (1.3.0)
pry (~> 0.11)
yard (~> 0.9.11)
racc (1.6.0)
rainbow (3.0.0)
rake (13.0.6)
regexp_parser (2.1.1)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rspec (3.8.0)
rspec-core (~> 3.8.0)
Expand All @@ -76,7 +91,23 @@ GEM
rubocop-ast (1.12.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
solargraph (0.41.2)
backport (~> 1.1)
benchmark
bundler (>= 1.17.2)
e2mmap
jaro_winkler (~> 1.5)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.1)
parser (~> 3.0)
reverse_markdown (>= 1.0.5, < 3)
rubocop (>= 0.52)
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
thor (1.2.1)
thread_safe (0.3.6)
tilt (2.0.10)
tzinfo (1.2.9)
thread_safe (~> 0.1)
unicode-display_width (2.1.0)
Expand All @@ -85,6 +116,7 @@ GEM
webrick (~> 1.7.0)

PLATFORMS
ruby
x86_64-darwin-20
x86_64-linux

Expand All @@ -99,6 +131,7 @@ DEPENDENCIES
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)
solargraph

BUNDLED WITH
2.3.9
79 changes: 57 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# PipefyMessage

This project it's a gem who provides a simple way to produce and consume messages for async processing.
This project is a gem that provides a simple way to produce and consume messages for async 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

Expand Down Expand Up @@ -31,16 +33,57 @@ Or install it yourself as:

## Usage

### Development
### 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:

```ruby
require "pipefy_message"

To test changes without install this dependency on your application, on your terminal go to the project root and execute:
##
# 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:

```ruby
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

ConsumerExampleClass.process_message
```

### Development - Test

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

```console
export AWS_ACCESS_KEY_ID=foo
export AWS_SECRET_ACCESS_KEY=bar
export AWS_ENDPOINT="http://localhost:4566"
export ENABLE_AWS_CLIENT_CONFIG=true
make build-app
make build-app-infra
```
Expand All @@ -61,28 +104,20 @@ On the irb console:

* Publish a message
```ruby
require 'pipefy_message'
message = PipefyMessage::Test.new
message.publish
require_relative 'lib/samples/my_awesome_publisher.rb'
publisher = MyAwesomePublisher.new
publisher.publish
```

* Consume a message
```ruby
require 'pipefy_message'
message = PipefyMessage::Test.new
message.consume
```

* Publish and Consume a message
```ruby
require 'pipefy_message'
message = PipefyMessage::Test.new
message.publish_and_consume
require_relative 'lib/samples/my_awesome_consumer.rb'
MyAwesomeConsumer.process_message
```

## Project Stack

- [Aws SDK Ruby - SNS & SQS](https://github.com/aws/aws-sdk-ruby)
- [Aws SDK Ruby - SNS & SQS](https://github.com/aws_client/aws-sdk-ruby)
- [Bundler](https://bundler.io/)
- Docker-compose
- [GitHub Actions](https://docs.github.com/en/actions)
Expand All @@ -92,7 +127,7 @@ On the irb console:

## Brokers Documentation

* [SNS & SQS User guide](https://github.com/pipefy/pipefy_message/tree/main/lib/pipefy_message/broker/aws/README.md)
* [SNS & SQS User guide](https://github.com/pipefy/pipefy_message/tree/main/lib/pipefy_message/broker/aws_client/README.md)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
command: >
"
# Wait for localstack boot
sleep 10
sleep 30
# Creating SNS Topics
aws --endpoint-url=http://localstack:4566 sns create-topic --name pipefy-local-topic
Expand Down
40 changes: 12 additions & 28 deletions lib/pipefy_message.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
# frozen_string_literal: true

require_relative "pipefy_message/version"
require_relative "pipefy_message/broker/aws/configuration"
require_relative "pipefy_message/broker/aws/sns/publisher"
require_relative "pipefy_message/base_consumer"
require_relative "pipefy_message/base_publisher"
require_relative "pipefy_message/logger"
require_relative "pipefy_message/logging"
require_relative "pipefy_message/publisher"
require_relative "pipefy_message/consumer"
require_relative "pipefy_message/providers/errors"

require "logger"
require "json"
require "benchmark"
require "active_support"
require "active_support/core_ext/string/inflections"

##
# PipefyMessage abstraction async process
##
module PipefyMessage
# Simple Test class to validate the project
class Test
def initialize
@log = PipefyMessage::CustomLogger.new
end

def publish
payload = { foo: "bar" }
puts Publisher::BasePublisher.new.publish(payload, "pipefy-local-topic")
end

def consume
@log.info("Starting the consumer process")
consumer = BaseConsumer.new("http://localhost:4566/000000000000/pipefy-local-queue")
@log.info("Creating new instance of consumer #{consumer}")
consumer.consume_message
end

def publish_and_consume
publish
consume
end
end
end
15 changes: 0 additions & 15 deletions lib/pipefy_message/base_consumer.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/pipefy_message/base_publisher.rb

This file was deleted.

Loading

0 comments on commit a30e740

Please sign in to comment.