beetle_joevandyk_extensions 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,23 @@
|
|
1
1
|
require "beetle_joevandyk_extensions/version"
|
2
2
|
require 'beetle'
|
3
|
+
require 'timeout'
|
3
4
|
|
4
5
|
module BeetleJoevandykExtensions
|
6
|
+
class BeetleError < RuntimeError; end
|
7
|
+
|
8
|
+
DEFAULT_TIMEOUT = 10
|
9
|
+
|
5
10
|
def rpc name, message, options={}
|
6
11
|
client = Beetle::Client.new
|
7
12
|
client.register_message name
|
8
|
-
|
9
|
-
|
10
|
-
|
13
|
+
Timeout.timeout(options[:timeout] || DEFAULT_TIMEOUT, BeetleError) do
|
14
|
+
status, result = client.rpc(name, message.to_json)
|
15
|
+
raise BeetleError.new(result.to_s) if status != "OK"
|
16
|
+
JSON.parse(result)
|
17
|
+
end
|
11
18
|
end
|
12
19
|
|
13
|
-
def
|
20
|
+
def listen name, &block
|
14
21
|
client = Beetle::Client.new
|
15
22
|
client.register_queue name
|
16
23
|
client.register_handler name do |input|
|
@@ -20,6 +27,12 @@ module BeetleJoevandykExtensions
|
|
20
27
|
puts "Started #{ name } at #{ Time.now }"
|
21
28
|
end
|
22
29
|
end
|
30
|
+
|
31
|
+
def publish name, message
|
32
|
+
client = Beetle::Client.new
|
33
|
+
client.register_message name
|
34
|
+
client.publish name, message
|
35
|
+
end
|
23
36
|
end
|
24
37
|
|
25
38
|
Beetle.extend(BeetleJoevandykExtensions)
|