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

Commit

Permalink
feat: adjust erros to aws_broker
Browse files Browse the repository at this point in the history
  • Loading branch information
eHattori committed Apr 19, 2022
1 parent 0b1db3f commit 4d7dd50
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 51 deletions.
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ Layout/LineLength:
Max: 120

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

Lint/MissingSuper:
Exclude:
- "/**/*"
28 changes: 15 additions & 13 deletions lib/pipefy_message/providers/aws_broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@
module PipefyMessage
module Providers
class AwsBroker < Broker
@@default_options = {
access_key_id: (ENV["AWS_ACCESS_KEY_ID"] || "foo"),
secret_access_key: (ENV["AWS_SECRET_ACCESS_KEY"] || "bar"),
endpoint: (ENV["AWS_ENDPOINT"] || "http://localhost:4566"),
region: (ENV["AWS_REGION"] || "us-east-1"),
stub_responses: (ENV["AWS_CLI_STUB_RESPONSE"] || false)
}

def initialize(queue_name)
@config = AwsBroker.config_options
@config = build_options
Aws.config.update(@config)

@sqs = Aws::SQS::Client.new
# require 'pry'; binding.pry
queue_url = @sqs.get_queue_url({ queue_name: queue_name }).queue_url
@poller = Aws::SQS::QueuePoller.new(queue_url)

@wait_time_seconds = 10
super
rescue (Aws::SQS::Errors::NonExistentQueue) => e
rescue Aws::SQS::Errors::NonExistentQueue, Seahorse::Client::NetworkingError => e
raise PipefyMessage::Providers::Errors::ResourceError, e.message
end

def self.config_options
@@default_options
def default_options
{
access_key_id: (ENV["AWS_ACCESS_KEY_ID"] || "foo"),
secret_access_key: (ENV["AWS_SECRET_ACCESS_KEY"] || "bar"),
endpoint: (ENV["AWS_ENDPOINT"] || "http://localhost:4566"),
region: (ENV["AWS_REGION"] || "us-east-1"),
stub_responses: (ENV["AWS_CLI_STUB_RESPONSE"] == "true")
}
end

def build_options
default_options.merge({})
end

def poller
Expand Down
17 changes: 9 additions & 8 deletions lib/pipefy_message/providers/errors.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true
module PipefyMessage
module Providers
module Errors
class ResourceError < StandardError
def initialize(msg="ResourceError")
super
end
end
module Providers
module Errors
class ResourceError < RuntimeError
def initialize(msg = "ResourceError")
super
end
end
end
end
end
end
38 changes: 18 additions & 20 deletions lib/pipefy_message/worker.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
# frozen_string_literal: true

require 'singleton'
require "singleton"

module PipefyMessage
# ClassMethods
module Worker
def self.included(base)
base.extend(ClassMethods)
end

# ClassMethods
module ClassMethods
def pipefymessage_options(opts = {})
module ClassMethods
def pipefymessage_options(opts = {})
options_hash = PipefyMessage.default_worker_options.merge(opts.transform_keys(&:to_s))
options_hash.each do |k, v|
singleton_class.class_eval { attr_accessor k }
send("#{k}=", v)
end
end

def perform_async()
begin
obj = self.new
build_instance_broker.poller do |message|
obj.perform(message)
end
rescue Exception => exception
# TODO: Implement retry
raise exception

def perform_async
obj = new
build_instance_broker.poller do |message|
obj.perform(message)
end
rescue Exception => e
# TODO: Implement retry
raise e
end
end

def build_instance_broker()
map = {"aws" => "PipefyMessage::Providers::AwsBroker"}
require_relative "providers/#{self.broker}_broker"

map[self.broker].constantize.new(self.queue)
end
def build_instance_broker
map = { "aws" => "PipefyMessage::Providers::AwsBroker" }
require_relative "providers/#{broker}_broker"

map[broker].constantize.new(queue)
end
end
end
13 changes: 11 additions & 2 deletions spec/providers/aws_broker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
RSpec.describe PipefyMessage::Providers::AwsBroker do
context "#AwsBroker" do
before do
ENV["AWS_CLI_STUB_RESPONSE"] = "true"
stub_const("ENV", ENV.to_hash.merge("AWS_CLI_STUB_RESPONSE" => "true"))
end

describe "should raise Errors" do
it "QueueNonExistError" do
allow_any_instance_of(Aws::SQS::Client)
.to receive(:get_queue_url)
.with({ queue_name: "my_queue" })
.and_raise(
Aws::SQS::Errors::NonExistentQueue.new(
double(Aws::SQS::Client),
"The specified queue my_queue does not exist for this wsdl version"
)
)

expect do
PipefyMessage::Providers::AwsBroker.new("my_queue")
end.to raise_error(PipefyMessage::Providers::Errors::ResourceError,
Expand Down
12 changes: 5 additions & 7 deletions spec/worker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# frozen_string_literal: true

RSpec.describe PipefyMessage::Worker do

RSpec.describe PipefyMessage::Worker do
$result =~ nil

class MockBroker < PipefyMessage::Providers::Broker
def poller
yield("test")
end
end

class MockBrokerFail < PipefyMessage::Providers::Broker
def poller
raise PipefyMessage::Providers::Errors::ResourceError
end
end

class TestWorker
include PipefyMessage::Worker
pipefymessage_options broker: "aws", queue: "pipefy-local-queue"
Expand All @@ -28,14 +28,14 @@ def perform(message)
describe "#perform" do
it "should call #perform from child instance when call #perform_async with success" do
allow(TestWorker).to receive(:build_instance_broker).and_return(MockBroker.new)

TestWorker.perform_async
expect($result).to eq "test"
end

it "should call #perform from child instance when call #perform_async with fail(raise a ResourceError)" do
allow(TestWorker).to receive(:build_instance_broker).and_return(MockBrokerFail.new)
expect{ TestWorker.perform_async }.to raise_error(PipefyMessage::Providers::Errors::ResourceError)
expect { TestWorker.perform_async }.to raise_error(PipefyMessage::Providers::Errors::ResourceError)
end
end

Expand All @@ -44,6 +44,4 @@ def perform(message)
expect(TestWorker.broker).to eq "aws"
end
end


end

0 comments on commit 4d7dd50

Please sign in to comment.