songkick_queue 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -5
- data/lib/songkick_queue.rb +1 -1
- data/lib/songkick_queue/consumer.rb +7 -1
- data/lib/songkick_queue/producer.rb +16 -1
- data/lib/songkick_queue/version.rb +1 -1
- data/lib/songkick_queue/worker.rb +1 -1
- data/spec/songkick_queue/consumer_spec.rb +12 -0
- data/spec/songkick_queue/producer_spec.rb +25 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6dc1bf4e0b25fb8f6ed6bb49a7423a4c52649ad
|
4
|
+
data.tar.gz: 93d50d6793741d2f61c1a0be13408b116bdbcc22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e174cceaeee589c2ddaa3e9573dea75f0f7a971ad2f3d47365795e7123a832ebfbe5bba8d1e2ab14640c07b2dbb7052d0ce9ca88115ef5425ce06fc9b3d137f3
|
7
|
+
data.tar.gz: c3f9ff40394a52f35f91d1dee21e7ba89b33d98edb1d6da8ccc29fc08d5e9f6e77954a26cbed5fd51ec527d47155fc011087a0ca6e3a3249cdb14abab32453e5
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
A gem for processing tasks asynchronously, powered by RabbitMQ.
|
4
4
|
|
5
|
+
[![Rubygems](https://badge.fury.io/rb/songkick_queue.svg)](https://rubygems.org/gems/songkick_queue)
|
5
6
|
[![Build status](https://travis-ci.org/songkick/queue.svg?branch=master)](https://travis-ci.org/songkick/queue)
|
6
7
|
|
7
8
|
## Dependencies
|
@@ -33,18 +34,23 @@ You must define both the AMQP URL and a logger instance:
|
|
33
34
|
|
34
35
|
```ruby
|
35
36
|
SongkickQueue.configure do |config|
|
36
|
-
config.amqp = 'amqp://localhost:5672'
|
37
|
-
config.logger = Logger.new(STDOUT)
|
37
|
+
config.amqp = 'amqp://localhost:5672' # required
|
38
|
+
config.logger = Logger.new(STDOUT) # required
|
39
|
+
config.queue_namespace = ENV['RACK_ENV'] # optional
|
38
40
|
end
|
39
41
|
```
|
40
42
|
|
43
|
+
You can also optionally define a namespace to apply to your queue names automatically when
|
44
|
+
publishing and consuming. This can be useful to isolate environments that share the same RabbitMQ
|
45
|
+
instance.
|
46
|
+
|
41
47
|
### Creating consumers
|
42
48
|
|
43
49
|
To create a consumer simply construct a new class and include the `SongkickQueue::Consumer`
|
44
50
|
module.
|
45
51
|
|
46
52
|
Consumers must declare a queue name to consume from (by calling `consume_from_queue`) and
|
47
|
-
|
53
|
+
define a `#process` method which receives a message.
|
48
54
|
|
49
55
|
For example:
|
50
56
|
|
@@ -104,10 +110,10 @@ NB. The `songkick_queue` process does not daemonize. We recommend running it usi
|
|
104
110
|
To publish messages for consumers, call the `#publish` method on `SongkickQueue`, passing in the
|
105
111
|
name of the queue to publish to and the message to send.
|
106
112
|
|
107
|
-
The queue name must match one declared in a consumer
|
113
|
+
The queue name must match one declared by `consume_from_queue` in a consumer.
|
108
114
|
|
109
115
|
The message can be any primitive Ruby object that can be serialized into JSON. Messages are
|
110
|
-
serialized whilst enqueued and deserialized
|
116
|
+
serialized whilst enqueued and deserialized before being passed to the `#process` method in your
|
111
117
|
consumer.
|
112
118
|
|
113
119
|
```ruby
|
data/lib/songkick_queue.rb
CHANGED
@@ -9,7 +9,7 @@ require 'songkick_queue/worker'
|
|
9
9
|
require 'songkick_queue/cli'
|
10
10
|
|
11
11
|
module SongkickQueue
|
12
|
-
Configuration = Struct.new(:amqp, :logger)
|
12
|
+
Configuration = Struct.new(:amqp, :logger, :queue_namespace)
|
13
13
|
ConfigurationError = Class.new(StandardError)
|
14
14
|
|
15
15
|
# Retrieve configuration for SongkickQueue
|
@@ -14,8 +14,14 @@ module SongkickQueue
|
|
14
14
|
#
|
15
15
|
# @raise [NotImplementedError] if queue name was not already defined
|
16
16
|
def queue_name
|
17
|
-
@queue_name
|
17
|
+
@queue_name or fail(NotImplementedError, 'you must declare a queue name to consume from ' +
|
18
18
|
'by calling #consume_from_queue in your consumer class. See README for more info.')
|
19
|
+
|
20
|
+
[config.queue_namespace, @queue_name].compact.join('.')
|
21
|
+
end
|
22
|
+
|
23
|
+
def config
|
24
|
+
SongkickQueue.configuration
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
@@ -12,13 +12,28 @@ module SongkickQueue
|
|
12
12
|
def publish(queue_name, message)
|
13
13
|
payload = JSON.generate(message)
|
14
14
|
|
15
|
+
routing_key = [config.queue_namespace, queue_name].compact.join('.')
|
16
|
+
|
15
17
|
client
|
16
18
|
.default_exchange
|
17
|
-
.publish(payload, routing_key:
|
19
|
+
.publish(payload, routing_key: routing_key)
|
20
|
+
|
21
|
+
logger.info "Published message to #{routing_key}"
|
18
22
|
end
|
19
23
|
|
20
24
|
private
|
21
25
|
|
26
|
+
# Retrieve the logger defined in the configuration
|
27
|
+
#
|
28
|
+
# @raise [ConfigurationError] if not defined
|
29
|
+
def logger
|
30
|
+
config.logger || fail(ConfigurationError, 'No logger configured, see README for more details')
|
31
|
+
end
|
32
|
+
|
33
|
+
def config
|
34
|
+
SongkickQueue.configuration
|
35
|
+
end
|
36
|
+
|
22
37
|
attr_reader :client
|
23
38
|
end
|
24
39
|
end
|
@@ -65,7 +65,7 @@ module SongkickQueue
|
|
65
65
|
# @param consumer_class [Class] to subscribe to
|
66
66
|
def subscribe_to_queue(consumer_class)
|
67
67
|
queue = channel.queue(consumer_class.queue_name, durable: true,
|
68
|
-
arguments: {'x-ha-policy' => 'all'})
|
68
|
+
arguments: { 'x-ha-policy' => 'all' })
|
69
69
|
|
70
70
|
queue.subscribe(manual_ack: true) do |delivery_info, properties, payload|
|
71
71
|
process_message(consumer_class, delivery_info, properties, payload)
|
@@ -20,6 +20,18 @@ module SongkickQueue
|
|
20
20
|
|
21
21
|
expect(ExampleConsumer.queue_name).to eq 'app.examples'
|
22
22
|
end
|
23
|
+
|
24
|
+
it "should add the configured namespace to the queue name" do
|
25
|
+
class ExampleConsumer
|
26
|
+
include SongkickQueue::Consumer
|
27
|
+
|
28
|
+
consume_from_queue 'app.examples'
|
29
|
+
end
|
30
|
+
|
31
|
+
allow(ExampleConsumer).to receive(:config) { double(queue_namespace: 'test-env') }
|
32
|
+
|
33
|
+
expect(ExampleConsumer.queue_name).to eq 'test-env.app.examples'
|
34
|
+
end
|
23
35
|
end
|
24
36
|
|
25
37
|
describe "#initialize" do
|
@@ -10,9 +10,34 @@ module SongkickQueue
|
|
10
10
|
client = instance_double(Client, default_exchange: exchange)
|
11
11
|
allow(producer).to receive(:client) { client }
|
12
12
|
|
13
|
+
logger = double(:logger, info: true)
|
14
|
+
allow(producer).to receive(:logger) { logger }
|
15
|
+
|
13
16
|
expect(exchange).to receive(:publish)
|
14
17
|
.with('{"example":"message","value":true}', routing_key: 'queue_name')
|
15
18
|
|
19
|
+
expect(logger).to receive(:info).with('Published message to queue_name')
|
20
|
+
|
21
|
+
producer.publish(:queue_name, { example: 'message', value: true })
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should publish with a routing key using the configured queue namespace" do
|
25
|
+
producer = Producer.new
|
26
|
+
|
27
|
+
exchange = double(:exchange, publish: :published)
|
28
|
+
client = instance_double(Client, default_exchange: exchange)
|
29
|
+
allow(producer).to receive(:client) { client }
|
30
|
+
|
31
|
+
logger = double(:logger, info: true)
|
32
|
+
allow(producer).to receive(:logger) { logger }
|
33
|
+
|
34
|
+
expect(exchange).to receive(:publish)
|
35
|
+
.with('{"example":"message","value":true}', routing_key: 'test-env.queue_name')
|
36
|
+
|
37
|
+
expect(logger).to receive(:info).with('Published message to test-env.queue_name')
|
38
|
+
|
39
|
+
allow(producer).to receive(:config) { double(queue_namespace: 'test-env') }
|
40
|
+
|
16
41
|
producer.publish(:queue_name, { example: 'message', value: true })
|
17
42
|
end
|
18
43
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: songkick_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Lucraft
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-03-
|
12
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|