adamhenry-vincent 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
- def self.method_missing(method,data = {})
9
- key = method.to_s.gsub(/_/,".")
10
- Vincent::Client.cast(key,data)
11
- end
14
+ def self.method_missing(method,data = {})
15
+ key = method.to_s.gsub(/_/,".")
16
+ Vincent::Client.cast(key,data)
17
+ end
12
18
 
13
- module Core
19
+ module Core
14
20
  def exchange
15
21
  @exchange ||= MQ.topic
16
22
  end
17
23
 
18
- def cast(key,args = {})
19
- args['kill_by'] = Time.now.to_i + args['ttl'] if args['ttl'].to_i > 0
20
- exchange.publish(encode(args), :routing_key => key)
21
- nil
22
- end
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
- def encode(data)
25
- data.to_json
26
- end
35
+ def encode(data)
36
+ data.to_json
37
+ end
27
38
 
28
- def decode(data)
29
- begin
30
- JSON.parse(data)
31
- rescue
39
+ def decode(data)
40
+ begin
41
+ JSON.parse(data)
42
+ rescue
32
43
  # log error
33
- {}
34
- end
35
- end
36
- end
37
-
38
- class Client
39
- extend Core
40
- end
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
- reply = receive(args['reply_to']) do
23
- cast(key, args)
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.#{ENV['HOSTNAME']}"
35
+ q ||= "q.#{key}.#{Socket.gethostname}"
43
36
 
44
37
  bind(q, key)
45
- subscribe(q) do |args|
46
- block.call(Vincent::Base.new(args, args))
47
- end
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 Object => e
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
- q = "q.#{key}" if options[:route_to] == :one
121
- q = "q.#{ENV['HOSTNAME']}" if options[:route_to] == :host
122
- q = "q.#{rand 9_999_999}" if options[:route_to] == :all
123
- raise "route_to => :one, :all or :host" unless q
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
- if params['reply_to']
182
- handle_call
183
- else
184
- handle_cast
185
- end
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
 
@@ -1,21 +1,33 @@
1
- require 'vincent/base'
1
+ require 'lib/vincent/base'
2
+ require 'neverblock'
2
3
 
3
4
  module Vincent
4
- class Server
5
- extend Core
5
+ class Server
6
+ extend Core
6
7
 
7
- def self.start(&block)
8
- EM.run {
9
- Signal.trap('INT') { AMQP.stop{ EM.stop } }
10
- Signal.trap('TERM'){ AMQP.stop{ EM.stop } }
8
+ def self.start(&block)
9
+ error = nil
10
+ EM.run {
11
+ Signal.trap('INT') { stop }
12
+ Signal.trap('TERM') { stop }
11
13
 
12
- Fiber.new {
13
- Vincent::Routes.bind
14
- EM.add_periodic_timer(60) { Vincent::Routes.check }
15
- block.call(Vincent::Server) if block
16
- }.resume
17
- }
18
- end
19
- end
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.2
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-03-20 00:00:00 -07:00
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: