ElmerFudd 0.0.28 → 0.0.29

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: ead68fdd2a7c88055137223d4f8a4a1090431918
4
- data.tar.gz: a6aa285df35b27bb7c12521d7e35afb5a3ca54bd
3
+ metadata.gz: b32982e1ce69bcab0be8687c22d1df997dffd45c
4
+ data.tar.gz: 6f9b93f425a1ed1cba71d734f89dcf9b1c3947b1
5
5
  SHA512:
6
- metadata.gz: 212d15a99a833ec4c2ee0d0c18dd401ad4bc98364605ec7ef5260420844c55402e00f3f6291c75982474e21230f6298a2bf8054306779caefc67a6259963bc92
7
- data.tar.gz: c563791441eb264c2fc8f5f96b85962e9c5ad100eef7722f383fc416b9f02d3d73be9cbafdfe3587e7620997f6b72c658ff71495f4bba686430befc88af2b913
6
+ metadata.gz: 780c794e122c9b0cd61b767dd05974714d6da222a944e3874e6d83f4950ba5dff805c938a96ec7bd35e4a8a99d826a2d8529bf7cd1f86de3d052690902eb7fcd
7
+ data.tar.gz: d8bb2088f67c164cb8e649ef107d7a25c7de8abe8042a056b06ee629dfa7f7503319f0915ab7826ad434b2b40e2481fc1c482c580faf464a739aef89c5c2d632
@@ -3,22 +3,29 @@ module ElmerFudd
3
3
  include Filter
4
4
  attr_reader :route
5
5
 
6
- def initialize(route, callback, filters)
6
+ def initialize(route, callback, filters, durable:)
7
7
  @route = route
8
8
  @callback = callback
9
9
  @filters = filters
10
+ @durable = durable
10
11
  end
11
12
 
12
13
  def queue(env)
13
- env.channel.queue(@route.queue_name, durable: true, exclusive: is_exclusive_queue).tap do |queue|
14
+ ensure_that_queue_exists(env)
15
+ env.channel.queue(@route.queue_name, durable: @durable, exclusive: is_exclusive_queue).tap do |queue|
14
16
  unless @route.exchange_name == ""
15
17
  Array(@route.routing_keys).each do |routing_key|
16
18
  queue.bind(exchange(env), routing_key: routing_key)
17
19
  end
18
20
  end
21
+ @route.queue_name = queue.name
19
22
  end
20
23
  end
21
24
 
25
+ def ensure_that_queue_exists(env)
26
+ env.channel.queue_declare(@route.queue_name, durable: @durable, exclusive: is_exclusive_queue)
27
+ end
28
+
22
29
  def exchange(env)
23
30
  env.logger.debug "ElmerFudd Handler.exchange queue_name: #{@route.queue_name}, exchange_name: #{@route.exchange_name}, filters: #{filters_names}"
24
31
  env.channel.direct(@route.exchange_name)
@@ -1,3 +1,3 @@
1
1
  module ElmerFudd
2
- VERSION = "0.0.28"
2
+ VERSION = "0.0.29"
3
3
  end
@@ -4,6 +4,11 @@ module ElmerFudd
4
4
  Env = Struct.new(:channel, :logger, :worker_class)
5
5
  Route = Struct.new(:exchange_name, :routing_keys, :queue_name)
6
6
 
7
+ class << self
8
+ attr_writer :durable_queues
9
+ def durable_queues; @durable_queues.nil? ? true : @durable_queues; end
10
+ end
11
+
7
12
  def self.handlers
8
13
  @handlers ||= []
9
14
  end
@@ -18,15 +23,18 @@ module ElmerFudd
18
23
  end
19
24
 
20
25
  def self.handle_event(route, filters: [], handler: nil, &block)
21
- handlers << TopicHandler.new(route, handler || block, (@filters + filters + [DiscardReturnValueFilter]).uniq)
26
+ handlers << TopicHandler.new(route, handler || block, (@filters + filters + [DiscardReturnValueFilter]).uniq,
27
+ durable: durable_queues)
22
28
  end
23
29
 
24
30
  def self.handle_cast(route, filters: [], handler: nil, &block)
25
- handlers << DirectHandler.new(route, handler || block, (@filters + filters + [DiscardReturnValueFilter]).uniq)
31
+ handlers << DirectHandler.new(route, handler || block, (@filters + filters + [DiscardReturnValueFilter]).uniq,
32
+ durable: durable_queues)
26
33
  end
27
34
 
28
35
  def self.handle_call(route, filters: [], handler: nil, &block)
29
- handlers << RpcHandler.new(route, handler || block, (@filters + filters).uniq)
36
+ handlers << RpcHandler.new(route, handler || block, (@filters + filters).uniq,
37
+ durable: false)
30
38
  end
31
39
 
32
40
  # Helper allowing to use any method taking hash as a handler
@@ -55,21 +63,33 @@ module ElmerFudd
55
63
 
56
64
  def start
57
65
  self.class.handlers.each do |handler|
58
- handler.queue(env).subscribe(manual_ack: true, block: false) do |delivery_info, properties, payload|
59
- message = Message.new(delivery_info, properties, payload, handler.route)
60
- begin
61
- handler.call(env, message)
62
- env.channel.acknowledge(message.delivery_info.delivery_tag)
63
- rescue Exception => e
64
- env.logger.fatal("Worker blocked: %s, %s:" % [e.class, e.message])
65
- e.backtrace.each { |l| env.logger.fatal(l) }
66
- end
67
- end
66
+ subscribe_handler(handler)
68
67
  end
69
68
  end
70
69
 
71
70
  private
72
71
 
72
+ def subscribe_handler(handler)
73
+ handler.queue(env).subscribe(manual_ack: true, block: false,
74
+ on_cancellation: ->(*) { on_consumer_cancellation(handler) }
75
+ ) do |delivery_info, properties, payload|
76
+ message = Message.new(delivery_info, properties, payload, handler.route)
77
+ begin
78
+ handler.call(env, message)
79
+ env.channel.acknowledge(message.delivery_info.delivery_tag)
80
+ rescue Exception => e
81
+ env.logger.fatal("Worker blocked: %s, %s:" % [e.class, e.message])
82
+ e.backtrace.each { |l| env.logger.fatal(l) }
83
+ end
84
+ end
85
+ end
86
+
87
+ def on_consumer_cancellation(handler)
88
+ unless self.class.durable_queues
89
+ handler.ensure_that_queue_exists(env)
90
+ end
91
+ end
92
+
73
93
  def env
74
94
  @env ||= Env.new(channel, @logger, self.class)
75
95
  end
@@ -79,7 +99,11 @@ module ElmerFudd
79
99
  end
80
100
 
81
101
  def channel
82
- @channel ||= connection.create_channel.tap { |c| c.prefetch(@concurrency) }
102
+ @channel ||= connection.create_channel
103
+ @channel.recover_cancelled_consumers!
104
+ @channel.tap do |c|
105
+ c.prefetch(@concurrency)
106
+ end
83
107
  end
84
108
  end
85
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ElmerFudd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrzej Sliwa
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-18 00:00:00.000000000 Z
12
+ date: 2015-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  requirements: []
125
125
  rubyforge_project:
126
- rubygems_version: 2.4.7
126
+ rubygems_version: 2.4.6
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: RabbitMQ in OTP way