liebre 0.1.14 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 014ac3efcc92a40a6fe079b207ef4fd1aaa20c18
4
- data.tar.gz: 26e318d49913b320bacba4f1e449f108ab8d64c3
3
+ metadata.gz: 4f381b98ce6cd5cce5fbab983cc33835d3cb00b3
4
+ data.tar.gz: af785cc0a08bd0f0f50b837f3784377fdfb3e96c
5
5
  SHA512:
6
- metadata.gz: 93dfa35403a31835cf96bf12d12c8473f310bce257a1a7675053f46dd073fe5f2f32467555eecb4234e1f5b752268393a8650e3a33d5991b53cc4833c3a5c3eb
7
- data.tar.gz: d504a8b11eb7668610ce3feeb5a99e6bebd497c7b0c6f4db149d0cd130e8c53117c4d286b6ae41ad792e4ac58d958e32aa8c2c58ec8267884da011529ce4c26c
6
+ metadata.gz: d5ca6a8a9998ce0304141a4ba9ea8a0fba34af5b3ab4f203bdf16479a47094ebfa8cdb31839f5e5896e90e5539d153a7676165590788bc7e7f37641f8ecc406f
7
+ data.tar.gz: f63a7728f1897337ab44ae4465c2614152795dd348226d76cc2a728ef51ea91003e3dbd4e7bd196ec7014358b3444c85e4cf0a4319c3a34890de58dce59a4bca
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- liebre (0.1.13)
4
+ liebre (0.1.15)
5
5
  bunny (~> 2.5, >= 2.5.1)
6
6
 
7
7
  GEM
@@ -5,7 +5,11 @@ module Liebre
5
5
  @@mutex = Mutex.new
6
6
 
7
7
  def self.mutex_sync
8
- @@mutex.synchronize do
8
+ unless @@mutex.locked?
9
+ @@mutex.synchronize do
10
+ yield
11
+ end
12
+ else
9
13
  yield
10
14
  end
11
15
  end
@@ -8,7 +8,7 @@ module Liebre
8
8
  def enqueue message, options = {}
9
9
  with_connection do
10
10
  exchange = exchange_for default_channel
11
- logger.debug "Liebre: Publishing '#{message}' with '#{options}' to exchange: #{exchange.name}"
11
+ logger.debug "Liebre# Publishing '#{message}' with '#{options}' to exchange: #{exchange.name}"
12
12
  exchange.publish message, options
13
13
  end
14
14
  end
@@ -22,12 +22,12 @@ module Liebre
22
22
  reply_queue.subscribe(:block => false) do |delivery_info, meta, payload|
23
23
  if meta[:correlation_id] == correlation_id
24
24
  result = payload
25
- logger.debug "Liebre: Received response '#{result}'"
25
+ logger.debug "Liebre# Received response '#{result}'"
26
26
  channel.consumers[delivery_info.consumer_tag].cancel
27
27
  end
28
28
  end
29
29
  exchange = exchange_for channel
30
- logger.debug "Liebre: Publishing '#{message}' with '#{options}' to exchange: #{exchange.name}"
30
+ logger.debug "Liebre# Publishing '#{message}' with '#{options}' to exchange: #{exchange.name}"
31
31
  exchange.publish message, options
32
32
  begin
33
33
  Timeout.timeout(Liebre.config.rpc_request_timeout) do
@@ -57,9 +57,12 @@ module Liebre
57
57
 
58
58
  def with_rpc_channel
59
59
  with_connection do
60
- channel = connection_manager.get(connection_name).create_channel
61
- yield(channel)
62
- channel.close
60
+ Common::Utils.mutex_sync do
61
+ channel = connection_manager.get(connection_name).create_channel
62
+ channel.prefetch 1
63
+ yield(channel)
64
+ channel.close
65
+ end
63
66
  end
64
67
  end
65
68
 
@@ -28,26 +28,30 @@ module Liebre
28
28
 
29
29
  def do_shutdown
30
30
  Thread.start do
31
- logger.info("Liebre: Closing AMQP connection...")
31
+ logger.info("Liebre# Closing AMQP connection...")
32
+ consumers.stop
32
33
  connection_manager.stop
33
- logger.info("Liebre: AMQP connection closed")
34
+ logger.info("Liebre# AMQP connection closed")
34
35
  end.join
35
36
  end
36
37
 
37
38
  def start_consumers
38
- consumers = Consumers.new(connection_manager)
39
39
  consumers.start_all
40
40
  end
41
41
 
42
42
  def log_and_wait e
43
43
  logger.warn(e)
44
44
  sleep(retry_interval)
45
- logger.warn("Liebre: Retrying connection")
45
+ logger.warn("Liebre# Retrying connection")
46
46
  end
47
47
 
48
48
  def logger
49
49
  Liebre.logger
50
50
  end
51
+
52
+ def consumers
53
+ @consumers ||= Consumers.new(connection_manager)
54
+ end
51
55
 
52
56
  def connection_manager
53
57
  @connection_manager ||= ConnectionManager.instance
@@ -4,6 +4,7 @@ module Liebre
4
4
 
5
5
  def initialize connection_manager
6
6
  @connection_manager = connection_manager
7
+ @threads = []
7
8
  end
8
9
 
9
10
  def consumer_names
@@ -11,7 +12,15 @@ module Liebre
11
12
  end
12
13
 
13
14
  def start_all
14
- consumer_names.each { |name| start(name) }
15
+ consumer_names.each do |name|
16
+ start(name)
17
+ end
18
+ end
19
+
20
+ def stop
21
+ threads.each do |starter|
22
+ starter.stop
23
+ end
15
24
  end
16
25
 
17
26
  def start name
@@ -19,7 +28,8 @@ module Liebre
19
28
  num_threads = params.fetch("num_threads", 1)
20
29
  num_threads.times do
21
30
  starter = Starter.new(connection_manager, params)
22
- starter.call
31
+ starter.start
32
+ threads << starter
23
33
  end
24
34
  end
25
35
 
@@ -29,7 +39,7 @@ module Liebre
29
39
  Liebre.config.consumers
30
40
  end
31
41
 
32
- attr_reader :connection_manager
42
+ attr_reader :connection_manager, :threads
33
43
 
34
44
  end
35
45
  end
@@ -11,8 +11,12 @@ module Liebre
11
11
  @config = config
12
12
  end
13
13
 
14
- def call
15
- consumer_class.new(connection, config).call
14
+ def start
15
+ @consumer = consumer_class.new(connection, config).start
16
+ end
17
+
18
+ def stop
19
+ @consumer.cancel
16
20
  end
17
21
 
18
22
  private
@@ -10,31 +10,60 @@ module Liebre
10
10
  @config = config
11
11
  end
12
12
 
13
- def call
13
+ def start
14
14
  initialize_error_queue
15
15
  initialize_queue
16
16
  end
17
+
18
+ def stop
19
+ if @consumer
20
+ @consumer.cancel
21
+ channel.close
22
+ end
23
+ end
17
24
 
18
25
  private
19
26
 
20
27
  def initialize_queue
21
- queue.subscribe(:manual_ack => true) do |info, meta, payload|
28
+ @consumer = queue.subscribe(:manual_ack => true) do |info, meta, payload|
22
29
  response = :reject
30
+ debug_string = ""
31
+ elapsed_time = nil
23
32
  begin
24
- logger.debug "Liebre: Received message for #{klass.name}: #{payload} - #{meta}"
33
+ debug_string = "Liebre# Received message for #{klass.name}(#{queue.name}): #{payload} - #{meta}"
34
+ start_at = Time.now
25
35
  consumer = klass.new(payload, meta)
26
36
  response = consumer.call
27
- rescue => e
37
+ elapsed_time = (Time.now - start_at).to_f * 1000
38
+ rescue StandardError => e
28
39
  response = :error
29
- logger.error "Liebre: Error while processing #{klass.name}: #{payload} - #{meta}"
30
- logger.error e.inspect
31
- logger.error e.backtrace.join("\n")
40
+ logger.error error_string(e, payload, meta)
41
+ rescue Exception => e
42
+ response = :error
43
+ logger.error error_string(e, payload, meta)
44
+ handler.respond response, info
45
+ raise e
32
46
  ensure
33
- logger.debug "Liebre: Responding with #{response}"
47
+ debug_string += "\nLiebre# Responding with #{response}"
48
+ log_result debug_string, elapsed_time
34
49
  handler.respond response, info
35
50
  end
36
51
  end
37
52
  end
53
+
54
+ def log_result debug_string, elapsed_time
55
+ time_string = "\nLiebre# Elapsed time #{elapsed_time} ms"
56
+ if logger.debug?
57
+ logger.debug debug_string + time_string
58
+ else
59
+ logger.info "Liebre# Received message for #{klass.name}(#{queue.name})" + time_string
60
+ end
61
+ end
62
+
63
+ def error_string error, payload, meta
64
+ "Liebre# Error while processing #{klass.name}(#{queue.name}): #{payload} - #{meta}" +
65
+ error.inspect + error.backtrace.join("\n")
66
+ end
38
67
 
39
68
  def initialize_error_queue
40
69
  Resources.new(connection, error_config).queue
@@ -3,15 +3,23 @@ module Liebre
3
3
  class Starter
4
4
  class RPC < Consumer
5
5
 
6
- def call
7
- queue.subscribe(:manual_ack => false) do |_info, meta, payload|
6
+ def start
7
+ @consumer = queue.subscribe(:manual_ack => false) do |_info, meta, payload|
8
8
  begin
9
- logger.debug "Liebre: Received message for #{klass.name}: #{payload} - #{meta}"
9
+ debug_string = "Liebre# Received message for #{klass.name}(#{queue.name}): #{payload} - #{meta}"
10
+ start_at = Time.now
10
11
  consumer = klass.new(payload, meta, callback(meta))
11
12
  consumer.call
12
- rescue => e
13
- logger.error e.inspect
14
- logger.error e.backtrace.join("\n")
13
+ elapsed_time = (Time.now - start_at).to_f * 1000
14
+ log_result debug_string, elapsed_time
15
+ rescue StandardError => e
16
+ response = :error
17
+ logger.error error_string(payload, meta)
18
+ rescue Exception => e
19
+ response = :error
20
+ logger.error error_string(payload, meta)
21
+ handler.respond response, info
22
+ raise e
15
23
  end
16
24
  end
17
25
  end
@@ -26,7 +34,7 @@ module Liebre
26
34
  }
27
35
 
28
36
  lambda do |response|
29
- logger.debug "Liebre: Responding with #{response}"
37
+ logger.debug "Liebre# Responding with #{response}"
30
38
  exchange.publish(response, opts)
31
39
  end
32
40
  end
@@ -1,5 +1,5 @@
1
1
  module Liebre
2
2
 
3
- VERSION = "0.1.14"
3
+ VERSION = "0.1.19"
4
4
 
5
5
  end
@@ -65,6 +65,7 @@ RSpec.describe Liebre::Publisher do
65
65
  before do
66
66
  expect(connection_manager).to receive(:get).with(:default).and_return connection
67
67
  expect(connection).to receive(:create_channel).and_return channel
68
+ expect(channel).to receive(:prefetch).with 1
68
69
  expect(channel).to receive(:queue).with(reply_queue_name, :exclusive => true, :auto_delete => true).
69
70
  and_return reply_queue
70
71
  expect(channel).to receive(:close)
@@ -49,8 +49,8 @@ RSpec.describe Liebre::Runner::Consumers do
49
49
  expect(Liebre::Runner::Starter).to receive(:new).
50
50
  with(conn, second_expected_config).and_return(second_starter)
51
51
 
52
- expect(first_starter ).to receive(:call).exactly(3).times
53
- expect(second_starter).to receive(:call)
52
+ expect(first_starter ).to receive(:start).exactly(3).times
53
+ expect(second_starter).to receive(:start)
54
54
 
55
55
  subject.start_all
56
56
  end
@@ -74,9 +74,10 @@ RSpec.describe Liebre::Runner::Starter::Consumer do
74
74
  end
75
75
 
76
76
  let(:exchange) { double 'exchange' }
77
- let(:queue) { double 'queue' }
77
+ let(:queue) { double 'queue', :name => "queue" }
78
78
  let(:error_queue) { double 'error_queue' }
79
79
  let(:channel) { double 'channel' }
80
+ let(:consumer) { double 'consumer' }
80
81
  let(:payload) { "the_payload" }
81
82
 
82
83
  subject { described_class.new(connection, config) }
@@ -120,6 +121,7 @@ RSpec.describe Liebre::Runner::Starter::Consumer do
120
121
 
121
122
  expect(queue).to receive :subscribe do |&block|
122
123
  block.call(deliver_info, meta, payload)
124
+ consumer
123
125
  end
124
126
 
125
127
  expect(consumer_class).to receive :new do |given_payload, given_meta|
@@ -133,7 +135,11 @@ RSpec.describe Liebre::Runner::Starter::Consumer do
133
135
 
134
136
  expect(handler).to receive(:respond).with ack, deliver_info
135
137
 
136
- subject.call
138
+ subject.start
139
+
140
+ expect(consumer).to receive(:cancel)
141
+ expect(channel).to receive(:close)
142
+ subject.stop
137
143
  end
138
144
 
139
145
  end
@@ -29,8 +29,9 @@ RSpec.describe Liebre::Runner::Starter::RPC do
29
29
  end
30
30
 
31
31
  let(:exchange) { double 'exchange' }
32
- let(:queue) { double 'queue' }
32
+ let(:queue) { double 'queue', :name => "queue" }
33
33
  let(:channel) { double 'channel', :default_exchange => exchange }
34
+ let(:consumer) { double 'consumer' }
34
35
  let(:payload) { "the_payload" }
35
36
  let(:response) { "the_response" }
36
37
 
@@ -76,6 +77,7 @@ RSpec.describe Liebre::Runner::Starter::RPC do
76
77
 
77
78
  expect(queue).to receive :subscribe do |&block|
78
79
  block.call(deliver_info, meta, payload)
80
+ consumer
79
81
  end
80
82
 
81
83
  expect(rpc_class).to receive :new do |given_payload, given_meta, callback|
@@ -88,7 +90,11 @@ RSpec.describe Liebre::Runner::Starter::RPC do
88
90
 
89
91
  expect(rpc_instance).to receive(:call)
90
92
 
91
- subject.call
93
+ subject.start
94
+
95
+ expect(consumer).to receive(:cancel)
96
+ expect(channel).to receive(:close)
97
+ subject.stop
92
98
  end
93
99
 
94
100
  end
@@ -3,7 +3,8 @@ require 'spec_helper'
3
3
  RSpec.describe Liebre::Runner::Starter do
4
4
 
5
5
  let(:connection_manager) { double 'connection_manager' }
6
- let(:connection) { double 'connection' }
6
+ let(:connection) { double 'connection' }
7
+ let(:consumer) { double 'consumer' }
7
8
 
8
9
  let(:rpc) { true }
9
10
 
@@ -20,14 +21,19 @@ RSpec.describe Liebre::Runner::Starter do
20
21
 
21
22
  let(:rpc_instance) { double 'rpc_instance' }
22
23
 
23
- describe '#call' do
24
+ describe '#start and #stop' do
24
25
 
25
26
  it 'calls the rpc_class with a callback' do
26
27
  expect(connection_manager).to receive(:get).with(:rpc).and_return connection
27
28
  expect(described_class::RPC).to receive(:new).with(connection, config).and_return rpc_instance
28
- expect(rpc_instance).to receive(:call)
29
+ expect(rpc_instance).to receive(:start).and_return consumer
29
30
 
30
- subject.call
31
+ subject.start
32
+
33
+ expect(consumer).to receive(:stop)
34
+
35
+ subject.stop
36
+
31
37
  end
32
38
  end
33
39
  end
@@ -44,14 +50,19 @@ RSpec.describe Liebre::Runner::Starter do
44
50
  }
45
51
  end
46
52
 
47
- describe '#call' do
53
+ describe '#start and #stop' do
48
54
 
49
55
  it 'calls the rpc_class with a callback' do
50
56
  expect(connection_manager).to receive(:get).with(:default).and_return connection
51
57
  expect(described_class::Consumer).to receive(:new).with(connection, config).and_return consumer
52
- expect(consumer).to receive(:call)
58
+ expect(consumer).to receive(:start).and_return consumer
53
59
 
54
- subject.call
60
+ subject.start
61
+
62
+ expect(consumer).to receive(:stop)
63
+
64
+ subject.stop
65
+
55
66
  end
56
67
  end
57
68
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liebre
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - jcabotc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-01-23 00:00:00.000000000 Z
12
+ date: 2017-01-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny