multiple_man 0.5.9 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45d1f3a68bbb6908b822a5b57a3c176152851b57
4
- data.tar.gz: bf316745d372637203f5a976f14e07c50fe9d213
3
+ metadata.gz: 55f92c313a41df73f660aeb11219df30699b05d2
4
+ data.tar.gz: 363a8538cf04a8dc30a3092a6ce83cb4f9c81ab4
5
5
  SHA512:
6
- metadata.gz: caba00db5b03de4f14b6e473e765bfc1096b5dd1a06f60fd84ec31a3692fafbce3aa3893fc75a97582767f4640787a3fff1b82b36d22d4e58c5cfa02af9044b4
7
- data.tar.gz: e6b17cff6eb2fc1b90287969218fbf4b228dd59b499a1ed87787149997c6090dd64782af35d6526dd0bc8e3ac27b90822d83f450a8984507f48c0304f431e8e0
6
+ metadata.gz: b9255e20caedf780c92afc54c206b0bafed00b49dc0e6ba0e4646946344ba80356127e56b91bc911f1eb9f0511a08597b9d64972ccac610aa5d1354028b02b8d
7
+ data.tar.gz: d3aef199d466696ff5978d7057a4c86b799c6dbc8f2f5384068f5daccea2dc252831c8e213eacf774f85e75f1150d761323a90fbefc9cfa6b7fa937af339a467
@@ -0,0 +1,30 @@
1
+ module MultipleMan
2
+ class AsyncModelPublisher < ModelPublisher
3
+
4
+ def publish(records, operation=:create)
5
+ return unless MultipleMan.configuration.enabled
6
+
7
+ if records.respond_to?(:pluck)
8
+ return unless records.any?
9
+ ids = records.pluck(:id)
10
+ klass = records.first.class.name
11
+ else
12
+ return if records.nil?
13
+ ids = [records.id]
14
+ klass = records.class.name
15
+ end
16
+
17
+ ModelPublisherJob.perform_async(klass, ids, options, operation)
18
+ end
19
+
20
+ end
21
+
22
+ class ModelPublisherJob
23
+ include Sidekiq::Worker
24
+
25
+ def perform(record_type, ids, options, operation)
26
+ records = Kernel.const_get(record_type).where(id: ids)
27
+ ModelPublisher.new(options).publish(records, operation)
28
+ end
29
+ end
30
+ end
@@ -7,6 +7,7 @@ module MultipleMan
7
7
  self.enabled = true
8
8
  self.channel_pool_size = 5
9
9
  self.worker_concurrency = 1
10
+ self.reraise_errors = true
10
11
  end
11
12
 
12
13
  def logger
@@ -18,7 +19,7 @@ module MultipleMan
18
19
  end
19
20
 
20
21
  attr_accessor :topic_name, :app_name, :connection, :enabled, :channel_pool_size, :error_handler,
21
- :worker_concurrency
22
+ :worker_concurrency, :reraise_errors
22
23
  attr_writer :logger
23
24
  end
24
25
 
@@ -4,18 +4,36 @@ require 'active_support/core_ext/module'
4
4
 
5
5
  module MultipleMan
6
6
  class Connection
7
+ @mutex = Mutex.new
8
+
9
+ def self.connection
10
+ @mutex.synchronize do
11
+ @connection ||= begin
12
+ connection = Bunny.new(MultipleMan.configuration.connection)
13
+ MultipleMan.logger.debug "Connecting to #{MultipleMan.configuration.connection}"
14
+ connection.start
15
+ connection
16
+ end
17
+ end
18
+ end
19
+
7
20
  def self.connect
8
- connection = new
9
- yield connection if block_given?
21
+ reconnect unless connection.open?
22
+
23
+ channel = connection.create_channel
24
+ yield new(channel) if block_given?
10
25
  ensure
11
- connection.close! if connection
26
+ channel.close if channel
27
+ end
28
+
29
+ def self.reconnect
30
+ connection.start
12
31
  end
13
32
 
14
33
  attr_reader :topic
15
34
 
16
- def initialize
17
- init_connection!
18
- init_channel!
35
+ def initialize(channel)
36
+ self.channel = channel
19
37
  self.topic = channel.topic(topic_name)
20
38
  end
21
39
 
@@ -23,25 +41,11 @@ module MultipleMan
23
41
  MultipleMan.configuration.topic_name
24
42
  end
25
43
 
26
- def close!
27
- channel.close if channel
28
- connection.close if connection
29
- end
30
-
31
44
  delegate :queue, to: :channel
32
45
 
33
46
  private
34
47
 
35
- def init_connection!
36
- self.connection = Bunny.new(MultipleMan.configuration.connection)
37
- connection.start
38
- end
39
-
40
- def init_channel!
41
- self.channel = connection.create_channel
42
- end
43
-
44
- attr_accessor :channel, :connection
48
+ attr_accessor :channel
45
49
  attr_writer :topic
46
50
 
47
51
  end
@@ -22,7 +22,7 @@ module MultipleMan
22
22
  end
23
23
 
24
24
  def publish(options = {})
25
- self.multiple_man_publisher = ModelPublisher.new(options)
25
+ self.multiple_man_publisher = ModelPublisher.build(options)
26
26
  end
27
27
  end
28
28
  end
@@ -1,8 +1,18 @@
1
+ require 'active_support/core_ext'
2
+
1
3
  module MultipleMan
2
4
  class ModelPublisher
3
5
 
6
+ def self.build(options = {})
7
+ if options[:async] == true
8
+ AsyncModelPublisher.new(options)
9
+ else
10
+ new(options)
11
+ end
12
+ end
13
+
4
14
  def initialize(options = {})
5
- self.options = options
15
+ self.options = options.with_indifferent_access
6
16
  end
7
17
 
8
18
  def publish(records, operation=:create)
@@ -19,7 +19,7 @@ module MultipleMan
19
19
  attr_accessor :klass
20
20
 
21
21
  def operation=(value)
22
- raise "Operation #{value} is not recognized" unless ALLOWED_OPERATIONS.include?(value)
22
+ raise "Operation #{value} is not recognized" unless ALLOWED_OPERATIONS.include?(value.to_sym)
23
23
  @operation = value
24
24
  end
25
25
 
@@ -1,3 +1,3 @@
1
1
  module MultipleMan
2
- VERSION = "0.5.9"
2
+ VERSION = "0.5.10"
3
3
  end
data/lib/multiple_man.rb CHANGED
@@ -10,6 +10,7 @@ module MultipleMan
10
10
  require 'multiple_man/subscribers/registry'
11
11
  require 'multiple_man/configuration'
12
12
  require 'multiple_man/model_publisher'
13
+ require 'multiple_man/async_model_publisher'
13
14
  require 'multiple_man/attribute_extractor'
14
15
  require 'multiple_man/connection'
15
16
  require 'multiple_man/routing_key'
@@ -34,6 +35,7 @@ module MultipleMan
34
35
  def self.error(ex)
35
36
  if configuration.error_handler
36
37
  configuration.error_handler.call(ex)
38
+ raise ex if configuration.reraise_errors
37
39
  else
38
40
  raise ex
39
41
  end
@@ -3,21 +3,19 @@ require 'spec_helper'
3
3
  describe MultipleMan::Connection do
4
4
 
5
5
  let(:mock_bunny) { double(Bunny) }
6
- let(:mock_channel) { double(Bunny::Channel, topic: nil) }
6
+ let(:mock_channel) { double(Bunny::Channel, close: nil) }
7
7
 
8
8
  describe "connect" do
9
9
  it "should open a connection and a channel" do
10
10
  mock_bunny.should_receive(:start)
11
11
  Bunny.should_receive(:new).and_return(mock_bunny)
12
12
  mock_bunny.should_receive(:create_channel).once.and_return(mock_channel)
13
- mock_bunny.should_receive(:close)
14
- mock_channel.should_receive(:close)
15
13
 
16
14
  described_class.connect { }
17
15
  end
18
16
  end
19
17
 
20
- subject { described_class.new }
18
+ subject { described_class.new(mock_channel) }
21
19
 
22
20
  its(:topic_name) { should == MultipleMan.configuration.topic_name }
23
21
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiple_man
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.5.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Brunner
@@ -108,6 +108,7 @@ files:
108
108
  - README.md
109
109
  - Rakefile
110
110
  - lib/multiple_man.rb
111
+ - lib/multiple_man/async_model_publisher.rb
111
112
  - lib/multiple_man/attribute_extractor.rb
112
113
  - lib/multiple_man/configuration.rb
113
114
  - lib/multiple_man/connection.rb