lolitra 0.0.5 → 0.1.0b
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lolitra/handler_base.rb +78 -9
- data/lib/lolitra/version.rb +1 -1
- data/lolitra.gemspec +2 -0
- metadata +36 -12
- data/lib/lolitra/.future.rb.swp +0 -0
- data/lib/lolitra/.handler_base.rb.swp +0 -0
- data/spec/.lolitra_spec.rb.swp +0 -0
data/lib/lolitra/handler_base.rb
CHANGED
@@ -1,12 +1,28 @@
|
|
1
1
|
require 'singleton'
|
2
|
+
require 'log4r'
|
2
3
|
require 'amqp'
|
3
4
|
require 'amqp/utilities/event_loop_helper'
|
4
5
|
require 'json'
|
6
|
+
require 'fiber'
|
5
7
|
|
6
8
|
module Lolitra
|
9
|
+
include Log4r
|
10
|
+
|
11
|
+
@@logger = Logger.new 'lolitra'
|
12
|
+
@@logger.outputters = Outputter.stdout
|
13
|
+
|
14
|
+
def self.logger
|
15
|
+
@@logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.logger=(new_logger)
|
19
|
+
@@logger = new_logger
|
20
|
+
end
|
21
|
+
|
7
22
|
module MessageHandler
|
8
23
|
module Helpers
|
9
|
-
def self.underscore(
|
24
|
+
def self.underscore(arg)
|
25
|
+
word = arg.dup
|
10
26
|
word.gsub!(/::/, '/')
|
11
27
|
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
12
28
|
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
@@ -104,6 +120,10 @@ module Lolitra
|
|
104
120
|
base.send :extend, MessageHandlerClass
|
105
121
|
end
|
106
122
|
|
123
|
+
def publish(message)
|
124
|
+
self.class.publish(message)
|
125
|
+
end
|
126
|
+
|
107
127
|
def handle(message)
|
108
128
|
handler_method = self.class.handlers[message.class.message_key][1]
|
109
129
|
raise "Can't handle message #{message.class}" unless handler_method
|
@@ -165,7 +185,7 @@ module Lolitra
|
|
165
185
|
if (key)
|
166
186
|
self.class_message_key = key
|
167
187
|
else
|
168
|
-
self.class_message_key || "#{MessageHandler::
|
188
|
+
self.class_message_key || "#{MessageHandler::Helpers.underscore(self.class.name)}"
|
169
189
|
end
|
170
190
|
end
|
171
191
|
|
@@ -181,14 +201,38 @@ module Lolitra
|
|
181
201
|
end
|
182
202
|
|
183
203
|
def initialize(hash={})
|
184
|
-
hash.each { |key, value| self.send("#{MessageHandler::
|
204
|
+
hash.each { |key, value| self.send("#{MessageHandler::Helpers.underscore(key)}=", value) }
|
185
205
|
end
|
186
206
|
|
187
|
-
def
|
207
|
+
def to_hash
|
188
208
|
hash = {}
|
189
|
-
self.instance_variables.each {|var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) }
|
190
|
-
|
209
|
+
self.instance_variables.each {|var| hash[var.to_s.delete("@").to_sym] = self.instance_variable_get(var) }
|
210
|
+
hash
|
191
211
|
end
|
212
|
+
|
213
|
+
def marshall
|
214
|
+
JSON.generate(to_hash)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
class FayeBus
|
219
|
+
def initialize(options = {})
|
220
|
+
EM::next_tick do
|
221
|
+
@socketClient = Faye::Client.new(options[:url] || 'http://localhost:9292/faye')
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def subscribe(message_class, handler_class)
|
226
|
+
EM::next_tick do
|
227
|
+
@socketClient.subscribe(message_class.message_key) do |payload|
|
228
|
+
handler_class.handle(message_class.unmarshall(payload))
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def publish(message)
|
234
|
+
@socketClient.publish(message.class.message_key, message.marshall)
|
235
|
+
end
|
192
236
|
end
|
193
237
|
|
194
238
|
class AmqpBus
|
@@ -217,6 +261,8 @@ module Lolitra
|
|
217
261
|
end
|
218
262
|
|
219
263
|
def publish(message)
|
264
|
+
Lolitra::logger.debug("Message sent: #{message.class.message_key}")
|
265
|
+
Lolitra::logger.debug("#{message.marshall}")
|
220
266
|
self.exchange.publish(message.marshall, :routing_key => message.class.message_key, :timestamp => Time.now.to_i)
|
221
267
|
end
|
222
268
|
|
@@ -224,9 +270,32 @@ module Lolitra
|
|
224
270
|
def create_queue(message_class, handler_class, options, queue_name)
|
225
271
|
EM.next_tick do
|
226
272
|
channel = AMQP::Channel.new(self.connection)
|
227
|
-
channel.prefetch(1).queue(queue_name, options).bind(self.exchange, :routing_key => message_class.message_key).subscribe do |info, payload|
|
228
|
-
|
229
|
-
|
273
|
+
channel.prefetch(1).queue(queue_name, options).bind(self.exchange, :routing_key => message_class.message_key).subscribe(:ack => true) do |info, payload|
|
274
|
+
Fiber.new do
|
275
|
+
current_fiber = Fiber.current
|
276
|
+
for i in (0..5)
|
277
|
+
begin
|
278
|
+
Lolitra::logger.debug("Message recived: #{info.routing_key}")
|
279
|
+
Lolitra::logger.debug("#{payload}")
|
280
|
+
message_class_tmp = handler_class.handlers[info.routing_key][0]
|
281
|
+
handler_class.handle(message_class_tmp.unmarshall(payload))
|
282
|
+
info.ack
|
283
|
+
Lolitra::logger.debug("Message processed")
|
284
|
+
break
|
285
|
+
rescue => e
|
286
|
+
Lolitra::logger.error("Try #{i}: #{e.message}")
|
287
|
+
if (i!=5)
|
288
|
+
EventMachine.add_timer(5) do
|
289
|
+
current_fiber.resume
|
290
|
+
end
|
291
|
+
Fiber.yield
|
292
|
+
else
|
293
|
+
Lolitra::logger.error(e.backtrace.join("\n\t"))
|
294
|
+
info.reject(:requeue => false)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end.resume
|
230
299
|
end
|
231
300
|
end
|
232
301
|
end
|
data/lib/lolitra/version.rb
CHANGED
data/lolitra.gemspec
CHANGED
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lolitra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
4
|
+
prerelease: true
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 1
|
8
|
+
- 0b
|
9
|
+
version: 0.1.0b
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Hugo Freire
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2013-07-23 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -56,6 +56,32 @@ dependencies:
|
|
56
56
|
version: "0"
|
57
57
|
type: :runtime
|
58
58
|
version_requirements: *id003
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: log4r
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
type: :runtime
|
71
|
+
version_requirements: *id004
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: faye
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
type: :runtime
|
84
|
+
version_requirements: *id005
|
59
85
|
description: Lolitra, build Sagas, a kind of Long Live Transaction (LLT), in less lines
|
60
86
|
email:
|
61
87
|
- hfreire@abajar.com
|
@@ -73,12 +99,9 @@ files:
|
|
73
99
|
- README.md
|
74
100
|
- Rakefile
|
75
101
|
- lib/lolitra.rb
|
76
|
-
- lib/lolitra/.future.rb.swp
|
77
|
-
- lib/lolitra/.handler_base.rb.swp
|
78
102
|
- lib/lolitra/handler_base.rb
|
79
103
|
- lib/lolitra/version.rb
|
80
104
|
- lolitra.gemspec
|
81
|
-
- spec/.lolitra_spec.rb.swp
|
82
105
|
- spec/lolitra_spec.rb
|
83
106
|
- spec/spec_helper.rb
|
84
107
|
has_rdoc: true
|
@@ -101,11 +124,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
125
|
none: false
|
103
126
|
requirements:
|
104
|
-
- - "
|
127
|
+
- - ">"
|
105
128
|
- !ruby/object:Gem::Version
|
106
129
|
segments:
|
107
|
-
-
|
108
|
-
|
130
|
+
- 1
|
131
|
+
- 3
|
132
|
+
- 1
|
133
|
+
version: 1.3.1
|
109
134
|
requirements: []
|
110
135
|
|
111
136
|
rubyforge_project:
|
@@ -114,6 +139,5 @@ signing_key:
|
|
114
139
|
specification_version: 3
|
115
140
|
summary: Lolitra, build Sagas, a kind of Long Live Transaction (LLT), in less lines
|
116
141
|
test_files:
|
117
|
-
- spec/.lolitra_spec.rb.swp
|
118
142
|
- spec/lolitra_spec.rb
|
119
143
|
- spec/spec_helper.rb
|
data/lib/lolitra/.future.rb.swp
DELETED
Binary file
|
Binary file
|
data/spec/.lolitra_spec.rb.swp
DELETED
Binary file
|