lapine 0.2.0 → 0.2.2

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: ccc8f9935974ce4c4981bc4d43da3f08cdf2c8b2
4
- data.tar.gz: a296e0395e2c43aaf6369a47c921176c2862442e
3
+ metadata.gz: 6ee36980b342bf290005bfe32c3645b60637fd29
4
+ data.tar.gz: 4c45b563a43f1ba82017801c257304779bc87a10
5
5
  SHA512:
6
- metadata.gz: f2992302c858ec6589bc6b5147cd85b332bb6eb83a4ba9ee7df10be251711fbb7bc9e9d4e6d292b33b3a97bc6c7123054553ee3866adbc33de6c17d71f602c39
7
- data.tar.gz: 61271187c14a82e828f3031c0063d8f0ecd237ac1d7dff4cb1ed5ba223dff3d9713fe3cc0c4b66ec1c8e421b6c3aba434af3afd255d5bbac3d54d3a61002f163
6
+ metadata.gz: d6b9b11710eef507aff965297bd94d35fbbaec1ea61d6c49068762c47c362d901b2fdca982217510acc6dcff18ddd140ea1a156f05fe77d7dc2cfdd6d472ae92
7
+ data.tar.gz: 77230701216a54be3410548a282c67e1aa05a33a085001aef88f9d98fb8df26dfd1a24ec505e1b9072be27fa39c096fe1d8195df865a7963097cba2f9d4a00be
data/README.md CHANGED
@@ -2,7 +2,8 @@ Lapine
2
2
  ======
3
3
 
4
4
  Speak to RabbitMQ. This gem serves as a wrapper for publishing messages
5
- to RabbitMQ via the Bunny gem.
5
+ to RabbitMQ via the Bunny gem, and for consuming messages using the AMQP
6
+ gem.
6
7
 
7
8
 
8
9
  ## Configuration
@@ -31,9 +32,9 @@ Lapine.add_exchange 'efrafa',
31
32
  type: 'topic' # required
32
33
  ```
33
34
 
34
- ## Usage
35
+ ## Publisher Usage
35
36
 
36
- Define a class that configures which `exchange` is uses. This class
37
+ Define a class that configures which `exchange` is used. This class
37
38
  must define `#to_hash`
38
39
 
39
40
  ```ruby
@@ -72,6 +73,12 @@ Note that the `#initialize` method and the contents of `#to_hash`
72
73
  are arbitrary.
73
74
 
74
75
 
76
+ ## Consumer Usage
77
+
78
+ Please see the Lapine wiki for documentation on defining and configuring
79
+ consumers.
80
+
81
+
75
82
  ## But... WHY
76
83
 
77
84
  * This should be dead simple, but everything else was either too
@@ -50,6 +50,11 @@ module Lapine
50
50
  long: '--password PASSWORD',
51
51
  description: 'RabbitMQ password (default guest)'
52
52
 
53
+ option :transient,
54
+ long: '--transient',
55
+ description: 'Auto-delete queues when workers stop',
56
+ default: false
57
+
53
58
  option :debug,
54
59
  long: '--debug',
55
60
  description: 'More verbose (and possibly non-threadsafe) log statements',
@@ -89,6 +94,10 @@ module Lapine
89
94
  yaml_config['topics']
90
95
  end
91
96
 
97
+ def transient?
98
+ config[:transient]
99
+ end
100
+
92
101
  def connection_properties
93
102
  {
94
103
  host: '127.0.0.1',
@@ -11,6 +11,10 @@ module Lapine
11
11
  @channel = AMQP::Channel.new(connection)
12
12
  @exchange = AMQP::Exchange.new(channel, :topic, topic, durable: true)
13
13
  end
14
+
15
+ def close!
16
+ @connection.close if @connection.connected?
17
+ end
14
18
  end
15
19
  end
16
20
  end
@@ -23,9 +23,10 @@ module Lapine
23
23
  Consumer::Environment.new(config).load!
24
24
  logger.info 'starting Lapine::Consumer'
25
25
 
26
+ @queue_properties = queue_properties
26
27
  EventMachine.run do
27
28
  topology.each_binding do |q, conn, routing_key, classes|
28
- queue = conn.channel.queue(q).bind(conn.exchange, routing_key: routing_key)
29
+ queue = conn.channel.queue(q, @queue_properties).bind(conn.exchange, routing_key: routing_key)
29
30
  queue.subscribe(ack: true) do |metadata, payload|
30
31
  classes.each do |clazz|
31
32
  Lapine::Consumer::Dispatcher.new(clazz, payload, metadata, logger).dispatch
@@ -60,6 +61,12 @@ module Lapine
60
61
  @logger ||= config.logfile ? Logger.new(config.logfile) : Logger.new(STDOUT)
61
62
  end
62
63
 
64
+ def queue_properties
65
+ {}.tap do |props|
66
+ props.merge!(auto_delete: true) if config.transient?
67
+ end
68
+ end
69
+
63
70
  def handle_signals!
64
71
  Signal.trap('INT') { EventMachine.stop }
65
72
  Signal.trap('TERM') { EventMachine.stop }
@@ -23,6 +23,13 @@ module Lapine
23
23
  end
24
24
  end
25
25
 
26
+ def close!
27
+ return unless @cons
28
+ @cons.values.each do |conn|
29
+ conn.close!
30
+ end
31
+ end
32
+
26
33
  private
27
34
 
28
35
  def get_conn(name)
@@ -1,3 +1,3 @@
1
1
  module Lapine
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -29,7 +29,7 @@ RSpec.describe Lapine::Consumer::Config do
29
29
  end
30
30
 
31
31
  context 'with connection info in file' do
32
- let(:config_from_file) { { 'connection' => { 'host' => '1.1.1.1' } } }
32
+ let(:config_from_file) { {'connection' => {'host' => '1.1.1.1'}} }
33
33
 
34
34
  it 'uses the config file info' do
35
35
  expect(connection_properties[:host]).to eq('1.1.1.1')
@@ -38,7 +38,7 @@ RSpec.describe Lapine::Consumer::Config do
38
38
 
39
39
  context 'with command line arg' do
40
40
  let(:argv) { %w(--host 2.2.2.2 -c /path/to/config.yml) }
41
- let(:config_from_file) { { 'connection' => { 'host' => '1.1.1.1' } } }
41
+ let(:config_from_file) { {'connection' => {'host' => '1.1.1.1'}} }
42
42
 
43
43
  it 'prefers the cli' do
44
44
  expect(connection_properties[:host]).to eq('2.2.2.2')
@@ -52,7 +52,7 @@ RSpec.describe Lapine::Consumer::Config do
52
52
  end
53
53
 
54
54
  context 'with connection info in file' do
55
- let(:config_from_file) { { 'connection' => { 'port' => 5673 } } }
55
+ let(:config_from_file) { {'connection' => {'port' => 5673}} }
56
56
 
57
57
  it 'uses the config file info' do
58
58
  expect(connection_properties[:port]).to eq(5673)
@@ -61,7 +61,7 @@ RSpec.describe Lapine::Consumer::Config do
61
61
 
62
62
  context 'with command line arg' do
63
63
  let(:argv) { %w(--port 5674 -c /path/to/config.yml) }
64
- let(:config_from_file) { { 'connection' => { 'port' => 5673 } } }
64
+ let(:config_from_file) { {'connection' => {'port' => 5673}} }
65
65
 
66
66
  it 'prefers the cli' do
67
67
  expect(connection_properties[:port]).to eq(5674)
@@ -75,7 +75,7 @@ RSpec.describe Lapine::Consumer::Config do
75
75
  end
76
76
 
77
77
  context 'with connection info in file' do
78
- let(:config_from_file) { { 'connection' => { 'ssl' => true } } }
78
+ let(:config_from_file) { {'connection' => {'ssl' => true}} }
79
79
 
80
80
  it 'uses the config file info' do
81
81
  expect(connection_properties[:ssl]).to be(true)
@@ -84,7 +84,7 @@ RSpec.describe Lapine::Consumer::Config do
84
84
 
85
85
  context 'with command line arg' do
86
86
  let(:argv) { %w(--ssl -c /path/to/config.yml) }
87
- let(:config_from_file) { { 'connection' => { 'ssl' => false } } }
87
+ let(:config_from_file) { {'connection' => {'ssl' => false}} }
88
88
 
89
89
  it 'prefers the cli' do
90
90
  expect(connection_properties[:ssl]).to be(true)
@@ -98,7 +98,7 @@ RSpec.describe Lapine::Consumer::Config do
98
98
  end
99
99
 
100
100
  context 'with connection info in file' do
101
- let(:config_from_file) { { 'connection' => { 'vhost' => '/blah' } } }
101
+ let(:config_from_file) { {'connection' => {'vhost' => '/blah'}} }
102
102
 
103
103
  it 'uses the config file info' do
104
104
  expect(connection_properties[:vhost]).to eq('/blah')
@@ -107,7 +107,7 @@ RSpec.describe Lapine::Consumer::Config do
107
107
 
108
108
  context 'with command line arg' do
109
109
  let(:argv) { %w(--vhost /argh -c /path/to/config.yml) }
110
- let(:config_from_file) { { 'connection' => { 'vhost' => '/blah' } } }
110
+ let(:config_from_file) { {'connection' => {'vhost' => '/blah'}} }
111
111
 
112
112
  it 'prefers the cli' do
113
113
  expect(connection_properties[:vhost]).to eq('/argh')
@@ -121,7 +121,7 @@ RSpec.describe Lapine::Consumer::Config do
121
121
  end
122
122
 
123
123
  context 'with connection info in file' do
124
- let(:config_from_file) { { 'connection' => { 'username' => 'Hrairoo' } } }
124
+ let(:config_from_file) { {'connection' => {'username' => 'Hrairoo'}} }
125
125
 
126
126
  it 'uses the config file info' do
127
127
  expect(connection_properties[:username]).to eq('Hrairoo')
@@ -130,7 +130,7 @@ RSpec.describe Lapine::Consumer::Config do
130
130
 
131
131
  context 'with command line arg' do
132
132
  let(:argv) { %w(--username Thlayli -c /path/to/config.yml) }
133
- let(:config_from_file) { { 'connection' => { 'username' => 'Hrairoo' } } }
133
+ let(:config_from_file) { {'connection' => {'username' => 'Hrairoo'}} }
134
134
 
135
135
  it 'prefers the cli' do
136
136
  expect(connection_properties[:username]).to eq('Thlayli')
@@ -144,7 +144,7 @@ RSpec.describe Lapine::Consumer::Config do
144
144
  end
145
145
 
146
146
  context 'with connection info in file' do
147
- let(:config_from_file) { { 'connection' => { 'password' => 'flayrah' } } }
147
+ let(:config_from_file) { {'connection' => {'password' => 'flayrah'}} }
148
148
 
149
149
  it 'uses the config file info' do
150
150
  expect(connection_properties[:password]).to eq('flayrah')
@@ -153,7 +153,7 @@ RSpec.describe Lapine::Consumer::Config do
153
153
 
154
154
  context 'with command line arg' do
155
155
  let(:argv) { %w(--password pfeffa -c /path/to/config.yml) }
156
- let(:config_from_file) { { 'connection' => { 'password' => 'flayrah' } } }
156
+ let(:config_from_file) { {'connection' => {'password' => 'flayrah'}} }
157
157
 
158
158
  it 'prefers the cli' do
159
159
  expect(connection_properties[:password]).to eq('pfeffa')
@@ -161,4 +161,18 @@ RSpec.describe Lapine::Consumer::Config do
161
161
  end
162
162
  end
163
163
  end
164
+
165
+ describe '#transient?' do
166
+ it 'is false by default' do
167
+ expect(config.transient?).to be false
168
+ end
169
+
170
+ context 'with --transient' do
171
+ let(:argv) { %w(--transient -c /path/to/config.yml) }
172
+
173
+ it 'is true' do
174
+ expect(config.transient?).to be true
175
+ end
176
+ end
177
+ end
164
178
  end
@@ -46,7 +46,7 @@ RSpec.describe Lapine::Consumer::Dispatcher do
46
46
  context 'with invalid json' do
47
47
  let(:json) { 'oh boy I am not actually JSON' }
48
48
 
49
- it 'notifies new relic with the raw payload' do
49
+ it 'notifies the error handler with the raw payload' do
50
50
  dispatcher.dispatch
51
51
  expect(caught_errors).to include([an_instance_of(Oj::ParseError), json])
52
52
  end
@@ -55,7 +55,7 @@ RSpec.describe Lapine::Consumer::Dispatcher do
55
55
  context 'with any other error' do
56
56
  before { allow(dispatcher).to receive(:do_dispatch).and_raise(ArgumentError) }
57
57
 
58
- it 'notifies new relic with the parsed json' do
58
+ it 'notifies error handler with the parsed json' do
59
59
  dispatcher.dispatch
60
60
  expect(caught_errors).to include([an_instance_of(ArgumentError), hash])
61
61
  end
@@ -16,7 +16,7 @@ RSpec.describe Lapine::Consumer::Runner do
16
16
  let(:queues) do
17
17
  [
18
18
  {
19
- 'q' => 'testing.test',
19
+ 'q' => '',
20
20
  'topic' => 'testing.topic',
21
21
  'routing_key' => 'testing.update',
22
22
  'handlers' =>
@@ -34,7 +34,8 @@ RSpec.describe Lapine::Consumer::Runner do
34
34
  require: [],
35
35
  queues: queues,
36
36
  topics: ['testing.topic'],
37
- debug?: true) }
37
+ debug?: true,
38
+ transient?: true) }
38
39
  let(:connection_properties) { {host: '127.0.0.1', port: 5672, ssl: false, vhost: '/', username: 'guest', password: 'guest'} }
39
40
  let(:message) { Oj.dump({'pay' => 'load'}) }
40
41
 
@@ -49,10 +50,12 @@ RSpec.describe Lapine::Consumer::Runner do
49
50
  expect(FakerHandler).to receive(:handle_lapine_payload).twice
50
51
  em do
51
52
  subject.run
52
- conn = Lapine::Consumer::Connection.new(config, 'testing.topic')
53
- conn.exchange.publish(message, routing_key: 'testing.update')
54
- conn.exchange.publish(message, routing_key: 'testing.update')
55
- EventMachine.add_timer(2.0) { done }
53
+ EventMachine.add_timer(0.5) {
54
+ conn = Lapine::Consumer::Connection.new(config, 'testing.topic')
55
+ conn.exchange.publish(message, routing_key: 'testing.update')
56
+ conn.exchange.publish(message, routing_key: 'testing.update')
57
+ }
58
+ EventMachine.add_timer(1.0) { done }
56
59
  end
57
60
  end
58
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lapine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Saxby
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-12-01 00:00:00.000000000 Z
12
+ date: 2014-12-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: amqp