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

Commit

Permalink
feat: implement a perform_async method
Browse files Browse the repository at this point in the history
  • Loading branch information
eHattori committed Apr 19, 2022
1 parent f2cf8a0 commit 7c82f84
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
23 changes: 20 additions & 3 deletions lib/pipefy_message/worker.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
# frozen_string_literal: true

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()
instance_broker = build_instance_broker
obj = self.new
instance_broker.poller do |message|
obj.perform(message)
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
end
end
end
21 changes: 15 additions & 6 deletions spec/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@

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

# class MockBroker < Broker
# def poller
# yield("test")
# end
# end

class TestWorker
include PipefyMessage::Worker
pipefymessage_options broker: "aws"

def perform(body)
$result = body
def perform(message)
$result = message
end
end

describe "#perform" do
it "allow call .perform" do
worker = TestWorker.new
worker.perform("test")

#mock PipefyMessage::Providers::AwsBroker -> MockBroker

it "allow call .perform from instance worker when call perform_async from ClassMethod" do
TestWorker.perform_async
expect($result).to eq "test"
end
end

describe "#options class" do
it "should set options in class" do
TestWorker.new
expect(TestWorker.broker).to eq "aws"
end
end


end

0 comments on commit 7c82f84

Please sign in to comment.