dispatch-rider 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +110 -183
- data/lib/dispatch-rider/dispatcher.rb +9 -1
- data/lib/dispatch-rider/dispatcher/named_process.rb +13 -0
- data/lib/dispatch-rider/version.rb +1 -1
- metadata +5 -4
data/README.md
CHANGED
@@ -37,21 +37,21 @@ All configuration can be loaded from a hash instead of being done like the examp
|
|
37
37
|
eg:
|
38
38
|
|
39
39
|
```ruby
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
}
|
40
|
+
publisher = DispatchRider::Publisher.new
|
41
|
+
publisher.configure({
|
42
|
+
notification_services: {
|
43
|
+
file_system: {}
|
44
|
+
},
|
45
|
+
destinations: {
|
46
|
+
file_foo: {
|
47
|
+
service: :file_system,
|
48
|
+
channel: :foo,
|
49
|
+
options: {
|
50
|
+
path: "test/channel",
|
52
51
|
}
|
53
52
|
}
|
54
|
-
}
|
53
|
+
}
|
54
|
+
})
|
55
55
|
```
|
56
56
|
|
57
57
|
You can load this configuration hash from a YAML file or something, whatever works
|
@@ -62,113 +62,106 @@ well for your environment.
|
|
62
62
|
To publish using the filesystem register the path where to publish the message files.
|
63
63
|
|
64
64
|
```ruby
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
}})
|
78
|
-
|
65
|
+
publisher = DispatchRider::Publisher.new
|
66
|
+
|
67
|
+
publisher.register_notification_service(:file_system)
|
68
|
+
publisher.register_destination(:local_message_queue, :file_system, :dev_channel, :path => "tmp/news-updates")
|
69
|
+
|
70
|
+
publisher.publish(:destinations => :local_message_queue, :message => {
|
71
|
+
:subject => "read_news",
|
72
|
+
:body => {"headlines" => [
|
73
|
+
"April 29, 2013: Rails 4.0.0.rc1 is released.",
|
74
|
+
"May 14, 2013: Ruby 2.0.0-p195 is released"
|
75
|
+
]
|
76
|
+
}})
|
79
77
|
```
|
80
78
|
|
81
79
|
To publish using ```AWS::SNS``` make sure ```AWS.config``` has been setup.
|
82
80
|
It's then as easy as providing the configuration details of the topic to the publisher.
|
83
81
|
|
84
82
|
```ruby
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
}})
|
102
|
-
|
83
|
+
publisher = DispatchRider::Publisher.new
|
84
|
+
|
85
|
+
publisher.register_notification_service(:aws_sns)
|
86
|
+
publisher.register_destination(:sns_message_queue, :aws_sns, :dev_channel, {
|
87
|
+
:account => 777,
|
88
|
+
:region => 'us-east-1',
|
89
|
+
:topic => 'RoR'
|
90
|
+
})
|
91
|
+
|
92
|
+
publisher.publish(:destinations => :sns_message_queue, :message => {
|
93
|
+
:subject => "read_news",
|
94
|
+
:body => {"headlines" => [
|
95
|
+
"April 29, 2013: Rails 4.0.0.rc1 is released.",
|
96
|
+
"May 14, 2013: Ruby 2.0.0-p195 is released"
|
97
|
+
]
|
98
|
+
}})
|
103
99
|
```
|
104
100
|
|
105
101
|
To publish to multiple destinations:
|
106
102
|
|
107
103
|
```ruby
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
}})
|
116
|
-
|
104
|
+
publisher.publish(:destinations => [:local_message_queue, :sns_message_queue], :message => {
|
105
|
+
:subject => "read_news",
|
106
|
+
:body => {"headlines" => [
|
107
|
+
"April 29, 2013: Rails 4.0.0.rc1 is released.",
|
108
|
+
"May 14, 2013: Ruby 2.0.0-p195 is released"
|
109
|
+
]
|
110
|
+
}})
|
117
111
|
```
|
118
112
|
|
119
113
|
Sample Rails publisher:
|
120
114
|
|
121
115
|
```ruby
|
116
|
+
# app/publishers/news_update
|
117
|
+
class NewsPublisher
|
118
|
+
@publisher = DispatchRider::Publisher.new
|
122
119
|
|
123
|
-
|
124
|
-
class NewsPublisher
|
125
|
-
@publisher = DispatchRider::Publisher.new
|
120
|
+
amazon_config = YAML.load_file("#{Rails.root}/config/amazon.yml")
|
126
121
|
|
127
|
-
|
122
|
+
@publisher.register_notification_service(:aws_sns)
|
123
|
+
@publisher.register_destination(:sns_message_queue, :aws_sns, :dev_channel, {
|
124
|
+
:account => amazon_config[:account],
|
125
|
+
:region => amazon_config[:region],
|
126
|
+
:topic => "news-updates-#{Rails.env}"
|
127
|
+
})
|
128
128
|
|
129
|
-
|
130
|
-
@publisher.register_destination(:sns_message_queue, :aws_sns, :dev_channel, {
|
131
|
-
:account => amazon_config[:account],
|
132
|
-
:region => amazon_config[:region],
|
133
|
-
:topic => "news-updates-#{Rails.env}"
|
134
|
-
})
|
129
|
+
@destinations = [:sns_message_queue]
|
135
130
|
|
136
|
-
|
131
|
+
class << self
|
132
|
+
attr_reader :publisher
|
133
|
+
attr_accessor :destinations
|
134
|
+
end
|
137
135
|
|
138
|
-
|
139
|
-
attr_reader :publisher
|
140
|
-
attr_accessor :destinations
|
141
|
-
end
|
136
|
+
delegate :publisher, :destinations, :to => :"self.class"
|
142
137
|
|
143
|
-
|
138
|
+
def initialize(news)
|
139
|
+
@news = news
|
140
|
+
end
|
144
141
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
:body => {"headlines" => @news.headlines}
|
153
|
-
})
|
154
|
-
end
|
155
|
-
end
|
142
|
+
def publish
|
143
|
+
publisher.publish(:destinations => destinations, :message => {
|
144
|
+
:subject => "read_news",
|
145
|
+
:body => {"headlines" => @news.headlines}
|
146
|
+
})
|
147
|
+
end
|
148
|
+
end
|
156
149
|
|
157
|
-
|
158
|
-
|
159
|
-
|
150
|
+
# app/models/news
|
151
|
+
class News
|
152
|
+
serialize :headlines, Array
|
160
153
|
|
161
|
-
|
154
|
+
after_create :publish
|
162
155
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
156
|
+
def publish
|
157
|
+
NewsPublisher.new(self).publish
|
158
|
+
end
|
159
|
+
end
|
167
160
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
161
|
+
News.create!(:headlines => [
|
162
|
+
"April 29, 2013: Rails 4.0.0.rc1 is released.",
|
163
|
+
"May 14, 2013: Ruby 2.0.0-p195 is released"
|
164
|
+
])
|
172
165
|
```
|
173
166
|
|
174
167
|
### Subscriber
|
@@ -177,118 +170,52 @@ To setup a subscriber you'll need message handlers. The handlers are named the s
|
|
177
170
|
|
178
171
|
Sample message handler:
|
179
172
|
```ruby
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
puts headline
|
187
|
-
end
|
188
|
-
end
|
173
|
+
# app/handlers/bar_handler
|
174
|
+
module ReadNews
|
175
|
+
class << self
|
176
|
+
def process(message_body)
|
177
|
+
message_body["headlines"].each do |headline|
|
178
|
+
puts headline
|
189
179
|
end
|
190
180
|
end
|
191
|
-
|
181
|
+
end
|
182
|
+
end
|
192
183
|
```
|
193
184
|
|
194
185
|
Sample subscriber setup:
|
195
186
|
|
196
187
|
```ruby
|
188
|
+
subscriber = DispatchRider::Subscriber.new
|
197
189
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
subscriber.register_handler(:read_news)
|
202
|
-
subscriber.setup_demultiplexer(:aws_sqs)
|
203
|
-
|
204
|
-
subscriber.process
|
190
|
+
subscriber.register_queue(:aws_sqs, :name => "news-updates")
|
191
|
+
subscriber.register_handler(:read_news)
|
192
|
+
subscriber.setup_demultiplexer(:aws_sqs)
|
205
193
|
|
194
|
+
subscriber.process
|
206
195
|
```
|
207
196
|
|
208
197
|
Sample subscriber dispatch error handling (optional):
|
209
198
|
|
210
199
|
```ruby
|
200
|
+
# using blocks
|
211
201
|
|
212
|
-
|
202
|
+
subscriber.on_dispatch_error do |message, exception|
|
203
|
+
# put your error handling code here
|
213
204
|
|
214
|
-
|
215
|
-
|
205
|
+
return false # or return true to permanently remove the message
|
206
|
+
end
|
216
207
|
|
217
|
-
|
218
|
-
end
|
219
|
-
|
220
|
-
# using methods
|
221
|
-
|
222
|
-
def handle_dispatch_error(message, exception)
|
223
|
-
# put your error handling code here
|
208
|
+
# using methods
|
224
209
|
|
225
|
-
|
226
|
-
|
210
|
+
def handle_dispatch_error(message, exception)
|
211
|
+
# put your error handling code here
|
227
212
|
|
228
|
-
|
213
|
+
return false # or return true to permanently remove the message
|
214
|
+
end
|
229
215
|
|
216
|
+
subscriber.on_dispatch_error &method(:handle_dispatch_error)
|
230
217
|
```
|
231
218
|
|
232
|
-
|
233
|
-
Sample Rails application rake task:
|
234
|
-
|
235
|
-
```ruby
|
236
|
-
|
237
|
-
# lib/tasks/dispatch-rider
|
238
|
-
namespace :"dispatch-rider" do
|
239
|
-
desc "Tells DispatchRider to start running"
|
240
|
-
task :run => [:"run:remote"]
|
241
|
-
|
242
|
-
namespace :run do
|
243
|
-
desc "Tells DispatchRider to start running"
|
244
|
-
task :remote => [:ready, :set_aws_sqs_queue, :process]
|
245
|
-
|
246
|
-
desc "Tells DispatchRider to start running (using the filesystem as the queue)"
|
247
|
-
task :local => [:ready, :set_local_queue, :process]
|
248
|
-
|
249
|
-
task :ready => :environment do
|
250
|
-
puts "Creating subscriber..."
|
251
|
-
@subscriber = DispatchRider::Subscriber.new
|
252
|
-
|
253
|
-
[ # list of message handlers
|
254
|
-
:read_news
|
255
|
-
].each do |handler_name|
|
256
|
-
puts "Registering #{handler_name} handler..."
|
257
|
-
@subscriber.register_handler(handler_name)
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
task :set_aws_sqs_queue do
|
262
|
-
queue_name = "news-updates-#{Rails.env}"
|
263
|
-
puts "Setting AWS::SQS #{queue_name} queue..."
|
264
|
-
@subscriber.register_queue(:aws_sqs, :name => queue_name)
|
265
|
-
@subscriber.setup_demultiplexer(:aws_sqs)
|
266
|
-
end
|
267
|
-
|
268
|
-
task :set_local_queue do
|
269
|
-
queue_path = "tmp/news-updates-#{Rails.env}"
|
270
|
-
puts "Setting local filesystem queue @ #{queue_path.inspect}..."
|
271
|
-
@subscriber.register_queue(:file_system, :path => queue_path)
|
272
|
-
@subscriber.setup_demultiplexer(:file_system)
|
273
|
-
end
|
274
|
-
|
275
|
-
task :process do
|
276
|
-
puts "Running..."
|
277
|
-
@subscriber.process
|
278
|
-
end
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
```
|
283
|
-
|
284
|
-
To run the subscriber simply execute following:
|
285
|
-
|
286
|
-
$ bundle exec rake dispatch-rider:run
|
287
|
-
|
288
|
-
To run locally:
|
289
|
-
|
290
|
-
$ bundle exec rake dispatch-rider:run:local
|
291
|
-
|
292
219
|
## Contributing
|
293
220
|
|
294
221
|
### Process
|
@@ -2,10 +2,14 @@
|
|
2
2
|
# The handlers must be registered with the dispatcher.
|
3
3
|
# Tha handlers need to be modules that implement the process method.
|
4
4
|
# What handler to dispatch the message to is figured out from the subject of the message.
|
5
|
+
|
5
6
|
module DispatchRider
|
6
7
|
class Dispatcher
|
7
8
|
extend Forwardable
|
8
9
|
|
10
|
+
require 'dispatch-rider/dispatcher/named_process'
|
11
|
+
include NamedProcess
|
12
|
+
|
9
13
|
attr_reader :handler_registrar
|
10
14
|
|
11
15
|
def_delegators :handler_registrar, :register, :fetch, :unregister
|
@@ -20,10 +24,14 @@ module DispatchRider
|
|
20
24
|
end
|
21
25
|
|
22
26
|
def dispatch(message)
|
23
|
-
|
27
|
+
with_named_process(message.subject) do
|
28
|
+
handler_registrar.fetch(message.subject).process(message.body)
|
29
|
+
end
|
30
|
+
|
24
31
|
true # success => true (delete message)
|
25
32
|
rescue Exception => exception
|
26
33
|
@error_handler.call(message, exception)
|
34
|
+
false # failure => false (put message back on queue)
|
27
35
|
end
|
28
36
|
|
29
37
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dispatch-rider
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -169,6 +169,7 @@ files:
|
|
169
169
|
- lib/dispatch-rider/command.rb
|
170
170
|
- lib/dispatch-rider/demultiplexer.rb
|
171
171
|
- lib/dispatch-rider/dispatcher.rb
|
172
|
+
- lib/dispatch-rider/dispatcher/named_process.rb
|
172
173
|
- lib/dispatch-rider/errors.rb
|
173
174
|
- lib/dispatch-rider/message.rb
|
174
175
|
- lib/dispatch-rider/notification_services.rb
|
@@ -213,7 +214,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
213
214
|
version: '0'
|
214
215
|
segments:
|
215
216
|
- 0
|
216
|
-
hash: -
|
217
|
+
hash: -1276525084671356042
|
217
218
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
218
219
|
none: false
|
219
220
|
requirements:
|
@@ -222,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
222
223
|
version: '0'
|
223
224
|
requirements: []
|
224
225
|
rubyforge_project:
|
225
|
-
rubygems_version: 1.8.
|
226
|
+
rubygems_version: 1.8.25
|
226
227
|
signing_key:
|
227
228
|
specification_version: 3
|
228
229
|
summary: Messaging system based on the reactor patter. You can publish messages to
|