ElmerFudd 0.0.18 → 0.0.20

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: 74999b7aa0290bc49ca4edce1f54520a4f851410
4
- data.tar.gz: 52f441ed43519db2b3d7f8f3780b79d02442e42a
3
+ metadata.gz: d5b2d5bbfbc2a104f309373c4b03ff275f2e14fd
4
+ data.tar.gz: 554fc9c97e7014ad997351adf4df84f121a5ad69
5
5
  SHA512:
6
- metadata.gz: 7e245ccf3bcc2d609f391bbae6848235a3d50270d85e039501cf2d46ebcc68f6bf0e8bd625ce366f353e6109e57eed9fea19de10bb32f9c8ec20c83bccf21945
7
- data.tar.gz: 72fc4d7efc690f659b7a6ae09e85f53ca1ae54a7fb133053135a54afe47b3d6cbd10905900277c1d58c7a01cc8b112906be6ac902c84b1dbea70d7c7bfc75e28
6
+ metadata.gz: 630f1c3c7a20700f6f286fccd8b68760ab206d6cadd11b20490276e8916402cdc2fbfda9cfff66e4657a539b8d889b11c728594b15e411a49f90e72102e80e9a
7
+ data.tar.gz: 7605b4d6d3f539ff5025d6ef472cc822debaf35d962063df9610c7becb06b4c89860cde4dcafdb94ff77dfcfda18e009c1e4a0e6cd52cb34aea93a84aa498193
@@ -1,3 +1,3 @@
1
1
  module ElmerFudd
2
- VERSION = "0.0.18"
2
+ VERSION = "0.0.20"
3
3
  end
data/lib/ElmerFudd.rb CHANGED
@@ -5,32 +5,30 @@ module ElmerFudd
5
5
  class Publisher
6
6
  def initialize(connection, uuid_service: -> { rand.to_s })
7
7
  @connection = connection
8
- @channel = @connection.create_channel
9
- @x = @channel.default_exchange
10
- @rpc_reply_queue = @channel.queue("", exclusive: true)
11
8
  @uuid_service = uuid_service
12
9
  @topic_x = {}
13
10
  end
14
11
 
15
12
  def notify(topic_exchange, routing_key, payload)
16
- @topic_x[topic_exchange] ||= @channel.topic(topic_exchange)
13
+ @topic_x[topic_exchange] ||= channel.topic(topic_exchange)
17
14
  @topic_x[topic_exchange].publish payload.to_s, routing_key: routing_key
18
15
  nil
19
16
  end
20
17
 
21
18
  def cast(queue_name, payload)
22
- @x.publish(payload.to_s, routing_key: queue_name)
19
+ x.publish(payload.to_s, routing_key: queue_name)
23
20
  nil
24
21
  end
25
22
 
26
23
  def call(queue_name, payload, timeout: 10)
27
- @x.publish(payload.to_s, routing_key: queue_name, reply_to: @rpc_reply_queue.name,
28
- correlation_id: correlation_id = @uuid_service.call)
24
+ x.publish(payload.to_s, routing_key: queue_name, reply_to: rpc_reply_queue.name,
25
+ correlation_id: correlation_id = @uuid_service.call)
29
26
  response = nil
30
27
  consumer_tag = @uuid_service.call
28
+ puts rpc_reply_queue.name
31
29
  Timeout.timeout(timeout) do
32
- @rpc_reply_queue.subscribe(block: true, consumer_tag: consumer_tag) do |delivery_info, properties, payload|
33
- if properties[:correlation_id] == correlation_id
30
+ rpc_reply_queue.subscribe(manual_ack: false, block: true, consumer_tag: consumer_tag) do |delivery_info, properties, payload|
31
+ if p(properties[:correlation_id]) == correlation_id
34
32
  response = payload
35
33
  delivery_info.consumer.cancel
36
34
  end
@@ -38,9 +36,33 @@ module ElmerFudd
38
36
  end
39
37
  response
40
38
  rescue Timeout::Error
41
- @channel.consumers[consumer_tag].cancel
39
+ reply_channel.consumers[consumer_tag].cancel
42
40
  raise
43
41
  end
42
+
43
+ private
44
+
45
+ def connection
46
+ @connection.tap do |c|
47
+ c.start unless c.connected?
48
+ end
49
+ end
50
+
51
+ def x
52
+ @x ||= channel.default_exchange
53
+ end
54
+
55
+ def channel
56
+ @channel ||= connection.create_channel
57
+ end
58
+
59
+ def reply_channel
60
+ @reply_channel ||= connection.create_channel
61
+ end
62
+
63
+ def rpc_reply_queue
64
+ @rpc_reply_queue ||= reply_channel.queue("", exclusive: true)
65
+ end
44
66
  end
45
67
 
46
68
  class JsonPublisher < Publisher
@@ -59,7 +81,7 @@ module ElmerFudd
59
81
 
60
82
  class Worker
61
83
  Message = Struct.new(:delivery_info, :properties, :payload, :route)
62
- Env = Struct.new(:channel, :logger)
84
+ Env = Struct.new(:channel, :logger, :worker_class)
63
85
  Route = Struct.new(:exchange_name, :routing_keys, :queue_name)
64
86
 
65
87
  def self.handlers
@@ -89,24 +111,38 @@ module ElmerFudd
89
111
 
90
112
  def initialize(connection, concurrency: 1, logger: Logger.new($stdout))
91
113
  @connection = connection
92
- channel = connection.create_channel.tap { |c| c.prefetch(concurrency) }
93
- @env = Env.new(channel, logger)
114
+ @concurrency = concurrency
115
+ @logger = logger
94
116
  end
95
117
 
96
118
  def start
97
119
  self.class.handlers.each do |handler|
98
- handler.queue(@env).subscribe(manual_ack: true, block: false) do |delivery_info, properties, payload|
120
+ handler.queue(env).subscribe(manual_ack: true, block: false) do |delivery_info, properties, payload|
99
121
  message = Message.new(delivery_info, properties, payload, handler.route)
100
122
  begin
101
- handler.call(@env, message)
102
- @env.channel.acknowledge(message.delivery_info.delivery_tag)
123
+ handler.call(env, message)
124
+ env.channel.acknowledge(message.delivery_info.delivery_tag)
103
125
  rescue Exception => e
104
- @env.logger.fatal("Worker blocked: %s, %s:" % [e.class, e.message])
105
- e.backtrace.each { |l| @env.logger.fatal(l) }
126
+ env.logger.fatal("Worker blocked: %s, %s:" % [e.class, e.message])
127
+ e.backtrace.each { |l| env.logger.fatal(l) }
106
128
  end
107
129
  end
108
130
  end
109
131
  end
132
+
133
+ private
134
+
135
+ def env
136
+ @env ||= Env.new(channel, @logger, self.class)
137
+ end
138
+
139
+ def connection
140
+ @connection.tap { |c| c.start unless c.connected? }
141
+ end
142
+
143
+ def channel
144
+ @channel ||= connection.create_channel.tap { |c| c.prefetch(@concurrency) }
145
+ end
110
146
  end
111
147
 
112
148
  module Filter
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.18
4
+ version: 0.0.20
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: 2014-12-16 00:00:00.000000000 Z
12
+ date: 2015-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny