rmsg 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +100 -3
- data/lib/rmsg/task.rb +3 -3
- data/lib/rmsg/topic.rb +4 -4
- data/lib/rmsg/version.rb +1 -1
- data/rmsg.gemspec +1 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd8c186f2b3eb1994af8a571b97014da0b5c6555
|
4
|
+
data.tar.gz: 1082fd88c45cda89d57a806cc795c1f4342f6d46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44647529cd573666348ddc2540eedfdf6eb1ff4ab6b92005f8b9e9ce0c6f4ed457d4bf874083a3a0f974d1bd00c45a18277214210c8abc18dceb98b3ac7e031d
|
7
|
+
data.tar.gz: 1c6834888978b829861c5507f95d254c93044bc0fe52ffacc70d8a59c3d463da5b4b9da974163695167e498267fe70099330920ae5afb31be50873f278ddb7ca
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rmsg
|
2
2
|
|
3
|
-
|
3
|
+
RabbitMQ messaging in Ruby, with topics and tasks. A thin, minimal layer on top of [Bunny](https://github.com/ruby-amqp/bunny).
|
4
4
|
|
5
5
|
## Installation.
|
6
6
|
|
@@ -18,7 +18,100 @@ gem 'rmsg'
|
|
18
18
|
|
19
19
|
## Usage.
|
20
20
|
|
21
|
-
|
21
|
+
Rmsg supports messaging following the pub/sub over topics pattern and the tasks (work queues) pattern. Publishers are non-blocking since the typical scenario is publishing a message from a web app. Topic Subscribers and Task Consumers are blocking, long-running processes. You should run them as multiple independent processes using a supervisor like [runit](http://smarden.org/runit/).
|
22
|
+
|
23
|
+
## Topics.
|
24
|
+
|
25
|
+
Scenario: you have several independent services running. One of them holds users credentials. When an user changes his global API key you want to inform all interested services that the key has changed. Services choose to communicate over the "services" topic. You can have multiple publishers and multiple subscribers for the same topic and routing key.
|
26
|
+
|
27
|
+
### Subscriber.
|
28
|
+
|
29
|
+
A topic subscriber is a service listening to the "services" topic, for messages published with the "users.key_changed" routing key.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
#!/usr/bin/env ruby
|
33
|
+
|
34
|
+
require 'rmsg'
|
35
|
+
|
36
|
+
rabbit = Rmsg::Rabbit.new
|
37
|
+
services = Rmsg::Topic.new(rabbit: rabbit, topic: 'services')
|
38
|
+
|
39
|
+
services.subscribe('users.key_changed') do |message|
|
40
|
+
p message
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
You can also have a subscriber listening to all events related to users, not only to a specific event, by changing the routing key of the subscriber to:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
services.subscribe('users.*') do |message| ...
|
48
|
+
```
|
49
|
+
|
50
|
+
### Publisher.
|
51
|
+
|
52
|
+
A topic publisher is a service publishing the message to inform all other services that the event 'users.key_changed' has just happened.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
#!/usr/bin/env ruby
|
56
|
+
|
57
|
+
require 'rmsg'
|
58
|
+
|
59
|
+
message = {
|
60
|
+
id: 1,
|
61
|
+
key: 'xxxccc'
|
62
|
+
}
|
63
|
+
key = 'users.key_changed'
|
64
|
+
|
65
|
+
rabbit = Rmsg::Rabbit.new
|
66
|
+
services = Rmsg::Topic.new(rabbit: rabbit, topic: 'services')
|
67
|
+
|
68
|
+
services.publish(message, key)
|
69
|
+
|
70
|
+
rabbit.close
|
71
|
+
```
|
72
|
+
|
73
|
+
## Tasks.
|
74
|
+
|
75
|
+
Scenario: you have many email messages coming through a web API and you want to enqueue them as fast as possible, processing them at a later time using distributed consumers. Task publishers and consumers use durable queues and persistent messages, to ensure that tasks will survive a RabbitMQ restart. Consumers will send a manual ack after processing a task, in order to avoid losing tasks if a consumer crashes while processing a task. Tasks will be distributed to consumer processes with a simple round-robin schedule, one task per consumer.
|
76
|
+
|
77
|
+
### Consumer.
|
78
|
+
|
79
|
+
A task consumer is a process that grabs a task from the tasks queue and performs some work with it.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
#!/usr/bin/env ruby
|
83
|
+
|
84
|
+
require 'rmsg'
|
85
|
+
|
86
|
+
rabbit = Rmsg::Rabbit.new
|
87
|
+
messages = Rmsg::Task.new(rabbit: rabbit, queue: 'messages')
|
88
|
+
|
89
|
+
messages.subscribe do |message|
|
90
|
+
sleep 1
|
91
|
+
p message
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
### Publisher.
|
96
|
+
|
97
|
+
A task publisher is a process that publishes a task on a tasks queue as fast as possible.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
#!/usr/bin/env ruby
|
101
|
+
|
102
|
+
require 'rmsg'
|
103
|
+
|
104
|
+
rabbit = Rmsg::Rabbit.new
|
105
|
+
messages = Rmsg::Task.new(rabbit: rabbit, queue: 'messages')
|
106
|
+
|
107
|
+
message = {
|
108
|
+
id: 1,
|
109
|
+
body: 'email body'
|
110
|
+
}
|
111
|
+
messages.publish(message)
|
112
|
+
|
113
|
+
rabbit.close
|
114
|
+
```
|
22
115
|
|
23
116
|
## Tests.
|
24
117
|
|
@@ -29,10 +122,14 @@ TODO.
|
|
29
122
|
bundle exec rake test:spec
|
30
123
|
```
|
31
124
|
|
32
|
-
## Contributing
|
125
|
+
## Contributing.
|
33
126
|
|
34
127
|
1. Fork it ( https://github.com/badshark/rmsg/fork )
|
35
128
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
129
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
130
|
4. Push to the branch (`git push origin my-new-feature`)
|
38
131
|
5. Create a new Pull Request
|
132
|
+
|
133
|
+
## License.
|
134
|
+
|
135
|
+
[MIT](LICENSE.txt)
|
data/lib/rmsg/task.rb
CHANGED
@@ -4,8 +4,8 @@ module Rmsg
|
|
4
4
|
# When initializing a task handler, the queue
|
5
5
|
# will be declared durable, to survive RabbitMQ restarts.
|
6
6
|
# @param params [Hash]
|
7
|
-
#
|
8
|
-
#
|
7
|
+
# @option params :rabbit [Rmsg::Rabbit] Example: Rmsg::Rabbit.new
|
8
|
+
# @option params :queue [String] Example: 'messages'
|
9
9
|
def initialize(params)
|
10
10
|
@rabbit = params[:rabbit]
|
11
11
|
@queue = @rabbit.channel.queue(params[:queue], durable: true)
|
@@ -24,7 +24,7 @@ module Rmsg
|
|
24
24
|
# When receiving INT it will gracefully close.
|
25
25
|
# Consumer processes have a prefetch value of 1 for round-robin distribution.
|
26
26
|
# Consumer processes will send a manual ack after processing, to avoid losing tasks.
|
27
|
-
# @
|
27
|
+
# @yieldparam message [Hash] The message received, to be processed within the block.
|
28
28
|
def subscribe
|
29
29
|
@rabbit.channel.prefetch(1)
|
30
30
|
begin
|
data/lib/rmsg/topic.rb
CHANGED
@@ -3,15 +3,15 @@ module Rmsg
|
|
3
3
|
# to a topic with a key, over RabbitMQ.
|
4
4
|
class Topic
|
5
5
|
# @param params [Hash]
|
6
|
-
#
|
7
|
-
#
|
6
|
+
# @option params :rabbit [Rmsg::Rabbit] Example: Rmsg::Rabbit.new
|
7
|
+
# @option params :topic [String] Example: 'services'
|
8
8
|
def initialize(params)
|
9
9
|
@rabbit = params[:rabbit]
|
10
10
|
@exchange = @rabbit.channel.topic(params[:topic])
|
11
11
|
end
|
12
12
|
|
13
13
|
# Publish a message with a routing key.
|
14
|
-
# @param message [Hash]
|
14
|
+
# @param message [Hash] Message to be published.
|
15
15
|
# @param key [String] Example: 'users.key_changed'
|
16
16
|
# @return [Exchange] The exchange used to publish.
|
17
17
|
def publish(message, key)
|
@@ -23,7 +23,7 @@ module Rmsg
|
|
23
23
|
# It is specifically designed for long running processes.
|
24
24
|
# When receiving INT it will gracefully close.
|
25
25
|
# @param key [String] Example: 'users.key_changed'
|
26
|
-
# @
|
26
|
+
# @yieldparam message [Hash] The message received, to be processed within the block.
|
27
27
|
def subscribe(key)
|
28
28
|
@queue = @rabbit.channel.queue("", :exclusive => true)
|
29
29
|
@queue.bind(@exchange, :routing_key => key)
|
data/lib/rmsg/version.rb
CHANGED
data/rmsg.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rmsg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- badshark
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bunny
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,3 +140,4 @@ test_files:
|
|
126
140
|
- test/spec/rabbit_spec.rb
|
127
141
|
- test/spec/task_spec.rb
|
128
142
|
- test/spec/topic_spec.rb
|
143
|
+
has_rdoc:
|