updatebroker 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/updatebroker/updatebroker.rb +38 -16
- data/lib/updatebroker/version.rb +1 -1
- metadata +2 -2
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# (c) 2011 Sourdough Labs Research and Development Corp
|
2
|
+
# (c) 2011, 2012 Sourdough Labs Research and Development Corp
|
3
3
|
#
|
4
4
|
|
5
5
|
require 'rubygems'
|
@@ -12,26 +12,51 @@ class UpdateBroker < Goliath::API
|
|
12
12
|
|
13
13
|
attr_accessor :redis
|
14
14
|
|
15
|
+
def initialize(opts = {})
|
16
|
+
@channels = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_channel_handler(channel, handler)
|
20
|
+
handlers = @channels[channel] || []
|
21
|
+
handlers << handler
|
22
|
+
@channels[channel] = handlers
|
23
|
+
end
|
24
|
+
|
25
|
+
# Don't call unless add_channel_stream has been called at least once for the given channel
|
26
|
+
def remove_channel_handler(channel, handler)
|
27
|
+
handlers = @channels[channel]
|
28
|
+
handlers.delete(handler) unless handlers.nil?
|
29
|
+
end
|
30
|
+
|
31
|
+
def send_message_to_handlers(channel, msg)
|
32
|
+
handlers = @channels[channel]
|
33
|
+
unless handlers.nil?
|
34
|
+
handlers.each do |e|
|
35
|
+
res = e.stream_send("data:#{msg}\n\n")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
15
40
|
def response(env)
|
16
41
|
path = env[Goliath::Request::REQUEST_PATH]
|
17
|
-
env.logger.info "Request for '#{path}'"
|
18
42
|
return [404, {}, "Not found"] unless path == "/events"
|
19
43
|
|
20
44
|
channel = env.params[:channel] || env.params['channel']
|
21
|
-
env.logger.info "On channel '#{channel}'"
|
22
45
|
return [404, {}, "Channel required"] if channel.nil? || channel == ""
|
23
46
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
if chn === channel
|
31
|
-
res = env.stream_send("data:#{msg}\n\n")
|
47
|
+
if self.redis.nil?
|
48
|
+
env.logger.info "Redis connection is down, Connecting to @#{options[:redis]}"
|
49
|
+
self.redis = EM::Hiredis.connect(options[:redis])
|
50
|
+
redis.on(:message) do |chn, msg|
|
51
|
+
env.logger.info "Message for channel '#{chn}':"
|
52
|
+
send_message_to_handlers(chn,msg)
|
32
53
|
end
|
33
54
|
end
|
34
55
|
|
56
|
+
env.logger.info "subscribing to channel '#{channel}' for updates"
|
57
|
+
redis.subscribe(channel)
|
58
|
+
add_channel_handler(channel, env)
|
59
|
+
|
35
60
|
streaming_response(200, {'Content-Type' => 'text/event-stream'})
|
36
61
|
end
|
37
62
|
|
@@ -43,11 +68,8 @@ class UpdateBroker < Goliath::API
|
|
43
68
|
return if channel.nil? || channel == ""
|
44
69
|
|
45
70
|
env.logger.info "Connection closed, Unsubscribing."
|
46
|
-
|
47
|
-
|
48
|
-
env['redis'].quit
|
49
|
-
env['redis'].close_connection
|
50
|
-
end
|
71
|
+
redis.unsubscribe(channel)
|
72
|
+
remove_channel_handler(channel, env)
|
51
73
|
end
|
52
74
|
|
53
75
|
def options_parser(opts, options)
|
data/lib/updatebroker/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: updatebroker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
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: 2012-
|
12
|
+
date: 2012-11-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hiredis
|