adamhenry-vincent 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vincent.rb +37 -26
- data/lib/vincent/base.rb +33 -32
- data/lib/vincent/server.rb +27 -15
- metadata +3 -3
data/lib/vincent.rb
CHANGED
@@ -1,43 +1,54 @@
|
|
1
|
-
|
2
1
|
require 'mq'
|
3
2
|
require 'uri'
|
4
3
|
require 'json'
|
5
4
|
|
6
5
|
module Vincent
|
6
|
+
def self.cast(key,data)
|
7
|
+
Vincent::Client.cast(key,data)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.call(key,data)
|
11
|
+
raise "dont do this"
|
12
|
+
end
|
7
13
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
14
|
+
def self.method_missing(method,data = {})
|
15
|
+
key = method.to_s.gsub(/_/,".")
|
16
|
+
Vincent::Client.cast(key,data)
|
17
|
+
end
|
12
18
|
|
13
|
-
|
19
|
+
module Core
|
14
20
|
def exchange
|
15
21
|
@exchange ||= MQ.topic
|
16
22
|
end
|
17
23
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
24
|
+
def cast(key,args = {})
|
25
|
+
args['kill_by'] = Time.now.to_i + args['ttl'] if args['ttl'].to_i > 0
|
26
|
+
args.delete('ttl') if args['ttl'].to_i > 0
|
27
|
+
mq_publish(encode(args), :routing_key => key)
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def mq_publish(data, opts)
|
32
|
+
exchange.publish(data, opts)
|
33
|
+
end
|
23
34
|
|
24
|
-
|
25
|
-
|
26
|
-
|
35
|
+
def encode(data)
|
36
|
+
data.to_json
|
37
|
+
end
|
27
38
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
39
|
+
def decode(data)
|
40
|
+
begin
|
41
|
+
JSON.parse(data)
|
42
|
+
rescue
|
32
43
|
# log error
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
44
|
+
{}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Client
|
50
|
+
extend Core
|
51
|
+
end
|
41
52
|
end
|
42
53
|
|
43
54
|
config = URI.parse(ENV['AMQP_URI'] || 'amqp://guest:guest@localhost/')
|
data/lib/vincent/base.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
|
2
|
-
require 'vincent'
|
1
|
+
require 'lib/vincent'
|
3
2
|
require 'fiber'
|
3
|
+
require 'socket'
|
4
|
+
require 'neverblock'
|
4
5
|
|
5
6
|
### TODO
|
6
7
|
|
@@ -11,7 +12,6 @@ require 'fiber'
|
|
11
12
|
### autodelete queues?
|
12
13
|
|
13
14
|
module Vincent
|
14
|
-
|
15
15
|
class RejectMessage < RuntimeError ; end
|
16
16
|
class MissingCallHandler < RuntimeError ; end
|
17
17
|
class MissingCastHandler < RuntimeError ; end
|
@@ -19,32 +19,27 @@ module Vincent
|
|
19
19
|
module Core
|
20
20
|
def call(key, args = {})
|
21
21
|
args['reply_to'] = "reply_to.#{rand 99_999_999}"
|
22
|
-
|
23
|
-
|
22
|
+
fiber = Fiber.current
|
23
|
+
subscribe(args['reply_to']) do |result|
|
24
|
+
fiber.resume(result)
|
24
25
|
end
|
26
|
+
cast(key, args)
|
27
|
+
reply = Fiber.yield
|
25
28
|
raise unpack(reply["exception"]) if reply["exception"]
|
26
29
|
return unpack(reply["results"]) if reply["results"]
|
27
30
|
return reply
|
28
31
|
end
|
29
32
|
|
30
|
-
def receive(q)
|
31
|
-
f = Fiber.current
|
32
|
-
subscribe(q) do |result|
|
33
|
-
## maybe we can destroy the queue here - unsubscribe - autodelete
|
34
|
-
f.resume(result)
|
35
|
-
end
|
36
|
-
yield
|
37
|
-
Fiber.yield
|
38
|
-
end
|
39
|
-
|
40
33
|
def listen4(key, options = {}, &block)
|
41
34
|
q = options[:queue]
|
42
|
-
q ||= "q.#{
|
35
|
+
q ||= "q.#{key}.#{Socket.gethostname}"
|
43
36
|
|
44
37
|
bind(q, key)
|
45
|
-
|
46
|
-
|
47
|
-
|
38
|
+
Fiber.new do
|
39
|
+
subscribe(q) do |args|
|
40
|
+
block.call(Vincent::Base.new(args, args))
|
41
|
+
end
|
42
|
+
end.resume
|
48
43
|
end
|
49
44
|
|
50
45
|
def bind(q, key)
|
@@ -65,18 +60,16 @@ module Vincent
|
|
65
60
|
info.ack
|
66
61
|
return
|
67
62
|
end
|
68
|
-
|
69
63
|
begin
|
70
64
|
results = block.call(args)
|
71
65
|
rescue RejectMessage
|
72
66
|
info.reject
|
73
67
|
next
|
74
|
-
rescue
|
68
|
+
rescue Exception => e
|
75
69
|
puts "got exception #{e} - packing it for return"
|
76
70
|
## just display the exception if there's not reply_to
|
77
71
|
results = { :exception => pack(e) }
|
78
72
|
end
|
79
|
-
|
80
73
|
results = { :results => pack(results) } unless results.is_a?(Hash)
|
81
74
|
MQ.queue(args['reply_to']).publish(encode(results)) if args['reply_to']
|
82
75
|
info.ack
|
@@ -117,10 +110,17 @@ module Vincent
|
|
117
110
|
module Routes
|
118
111
|
def bind_to(key, options = {})
|
119
112
|
key = key.to_s
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
113
|
+
|
114
|
+
case options[:route_to]
|
115
|
+
when :one
|
116
|
+
q = "q.#{key}"
|
117
|
+
when :host
|
118
|
+
q = "q.#{key}.#{Socket.gethostname}"
|
119
|
+
when :all
|
120
|
+
q = "q.#{key}.#{Socket.gethostname}.#{Process.pid}"
|
121
|
+
else
|
122
|
+
raise "route_to => :one, :all or :host"
|
123
|
+
end
|
124
124
|
|
125
125
|
active = options[:active]
|
126
126
|
active ||= :active
|
@@ -178,11 +178,13 @@ module Vincent
|
|
178
178
|
end
|
179
179
|
|
180
180
|
def handle
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
181
|
+
Fiber.new do
|
182
|
+
if params['reply_to']
|
183
|
+
handle_call
|
184
|
+
else
|
185
|
+
handle_cast
|
186
|
+
end
|
187
|
+
end .resume
|
186
188
|
end
|
187
189
|
|
188
190
|
def handle_call
|
@@ -192,7 +194,6 @@ module Vincent
|
|
192
194
|
def handle_cast
|
193
195
|
raise MissingCastHandler
|
194
196
|
end
|
195
|
-
|
196
197
|
end
|
197
198
|
end
|
198
199
|
|
data/lib/vincent/server.rb
CHANGED
@@ -1,21 +1,33 @@
|
|
1
|
-
require 'vincent/base'
|
1
|
+
require 'lib/vincent/base'
|
2
|
+
require 'neverblock'
|
2
3
|
|
3
4
|
module Vincent
|
4
|
-
|
5
|
-
|
5
|
+
class Server
|
6
|
+
extend Core
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def self.start(&block)
|
9
|
+
error = nil
|
10
|
+
EM.run {
|
11
|
+
Signal.trap('INT') { stop }
|
12
|
+
Signal.trap('TERM') { stop }
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
14
|
+
Fiber.new {
|
15
|
+
Vincent::Routes.bind
|
16
|
+
EM.add_periodic_timer(60) { Vincent::Routes.check }
|
17
|
+
begin
|
18
|
+
block.call(Vincent::Server) if block
|
19
|
+
rescue Object => e
|
20
|
+
error = e
|
21
|
+
EM.stop
|
22
|
+
end
|
23
|
+
}.resume
|
24
|
+
}
|
25
|
+
raise error if error
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.stop
|
29
|
+
EM.next_tick { AMQP.stop { EM.stop } }
|
30
|
+
end
|
31
|
+
end
|
20
32
|
end
|
21
33
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adamhenry-vincent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Henry
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-04-14 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -25,8 +25,8 @@ extra_rdoc_files: []
|
|
25
25
|
files:
|
26
26
|
- lib/vincent.rb
|
27
27
|
- lib/vincent/server.rb
|
28
|
-
- README
|
29
28
|
- lib/vincent/base.rb
|
29
|
+
- README
|
30
30
|
has_rdoc: false
|
31
31
|
homepage: http://www.vincent.com/
|
32
32
|
post_install_message:
|