redis-stat 0.3.8-java → 0.3.9-java

Sign up to get free protection for your applications and to get access to all the features.
data/lib/redis-stat.rb CHANGED
@@ -81,6 +81,7 @@ class RedisStat
81
81
  if server || errs < NUM_RETRIES
82
82
  @os.puts if errs == 1
83
83
  @os.puts "#{e} (#{ server ? "#{errs}" : [errs, NUM_RETRIES].join('/') })".red.bold
84
+ server.alert "#{e} (#{errs})" if server
84
85
  sleep @interval
85
86
  retry
86
87
  else
@@ -116,6 +117,7 @@ private
116
117
  def start_server
117
118
  RedisStat::Server.set :port, @server_port
118
119
  RedisStat::Server.set :redis_stat, self
120
+ RedisStat::Server.set :last_info, info
119
121
  @server_thr = Thread.new { RedisStat::Server.run! }
120
122
  RedisStat::Server.wait_until_running
121
123
  trap('INT') { Thread.main.raise Interrupt }
@@ -1,6 +1,7 @@
1
1
  require 'sinatra/base'
2
2
  require 'json'
3
3
  require 'thread'
4
+ require 'set'
4
5
 
5
6
  class RedisStat
6
7
  class Server < Sinatra::Base
@@ -17,19 +18,36 @@ class Server < Sinatra::Base
17
18
  end
18
19
  set :environment, :production
19
20
  set :root, File.join( File.dirname(__FILE__), 'server' )
20
- set :clients, []
21
- set :history, []
22
21
  set :mutex, Mutex.new
22
+
23
+ set :clients, Set.new
24
+ set :history, []
25
+ set :last_error, nil
26
+ end
27
+
28
+ helpers do
29
+ def sync
30
+ settings.mutex.synchronize { yield }
31
+ end
32
+ end
33
+
34
+ not_found do
35
+ redirect '/'
23
36
  end
24
37
 
25
38
  get '/' do
26
- @hosts = settings.redis_stat.hosts
27
- @info = settings.redis_stat.info
28
- @measures = settings.redis_stat.measures
29
- @tab_measures = settings.redis_stat.tab_measures
30
- @interval = settings.redis_stat.interval
31
- @verbose = settings.redis_stat.verbose ? 'verbose' : ''
32
- @history = settings.history
39
+ redis_stat = settings.redis_stat
40
+ @hosts = redis_stat.hosts
41
+ @measures = redis_stat.measures
42
+ @tab_measures = redis_stat.tab_measures
43
+ @interval = redis_stat.interval
44
+ @verbose = redis_stat.verbose ? 'verbose' : ''
45
+ @history = sync { settings.history.dup }
46
+
47
+ info = redis_stat.info rescue nil
48
+ sync do
49
+ @info = info ? (settings.last_info = info) : settings.last_info
50
+ end
33
51
  erb :index
34
52
  end
35
53
 
@@ -37,13 +55,23 @@ class Server < Sinatra::Base
37
55
  content_type 'text/event-stream'
38
56
 
39
57
  if RUBY_PLATFORM == 'java'
40
- if last = settings.mutex.synchronize { settings.history.last }
41
- body "retry: #{settings.redis_stat.interval * 900}\ndata: #{last.to_json}\n\n"
42
- end
58
+ data =
59
+ sync {
60
+ if settings.last_error
61
+ { :error => settings.last_error }
62
+ elsif last = settings.history.last
63
+ last
64
+ else
65
+ {}
66
+ end
67
+ }.to_json
68
+ body "retry: #{settings.redis_stat.interval * 900}\ndata: #{data}\n\n"
43
69
  else
44
70
  stream(:keep_open) do |out|
45
- settings.clients << out
46
- out.callback { settings.clients.delete out }
71
+ sync do
72
+ settings.clients << out
73
+ out.callback { settings.clients.delete out }
74
+ end
47
75
  end
48
76
  end
49
77
  end
@@ -61,16 +89,29 @@ class Server < Sinatra::Base
61
89
  }]
62
90
  data = {:at => (Time.now.to_f * 1000).to_i, :static => static, :dynamic => data}
63
91
 
64
- hist = settings.history
65
92
  settings.mutex.synchronize do
93
+ settings.last_error = nil
94
+ hist = settings.history
66
95
  hist << data
67
96
  hist.shift if hist.length > HISTORY_LENGTH
68
97
  end
98
+ publish data
99
+ end
100
+
101
+ def alert error
102
+ settings.mutex.synchronize do
103
+ settings.last_error = error
104
+ end
105
+ publish({ :error => error })
106
+ end
69
107
 
70
- return if settings.clients.empty?
108
+ private
109
+ def publish data
110
+ clients = settings.mutex.synchronize { settings.clients.dup }
111
+ return if clients.empty?
71
112
 
72
113
  resp = "data: #{data.to_json}\n\n"
73
- settings.clients.each do |cl|
114
+ clients.each do |cl|
74
115
  cl << resp
75
116
  end
76
117
  end
@@ -26,11 +26,9 @@
26
26
  <div class="row">
27
27
  <div class="span12">
28
28
  <h3>Dashboard</h3>
29
- <% unless RUBY_PLATFORM == 'java' %>
30
29
  <div class="alert alert-error hide">
31
- <h4>Lost connection to redis-stat</h4>
30
+ <h4></h4>
32
31
  </div>
33
- <% end %>
34
32
  </div>
35
33
  </div>
36
34
 
@@ -144,16 +142,23 @@
144
142
 
145
143
  var source = new EventSource("/pull")
146
144
  source.onmessage = function(e) {
147
- if (appendToHistory(JSON.parse(e.data))) {
145
+ var json = JSON.parse(e.data);
146
+ if (json.error) {
147
+ $(".alert h4").text(json.error);
148
+ $(".alert").fadeIn();
149
+ } else if (appendToHistory(json)) {
148
150
  $(".alert").fadeOut()
149
151
  updateTable()
150
152
  updatePlot()
151
153
  }
152
154
  }
155
+ <% unless RUBY_PLATFORM == 'java' %>
153
156
  source.onerror = function(e) {
154
- $(".alert").fadeIn()
157
+ $(".alert h4").text("Lost connection to redis-stat");
158
+ $(".alert").fadeIn();
155
159
  // source.close()
156
160
  }
161
+ <% end %>
157
162
  })
158
163
  </script>
159
164
  </body>
@@ -1,3 +1,3 @@
1
1
  class RedisStat
2
- VERSION = "0.3.8"
2
+ VERSION = "0.3.9"
3
3
  end
@@ -104,9 +104,6 @@ class TestRedisStat < Test::Unit::TestCase
104
104
 
105
105
  # Server
106
106
  if RUBY_PLATFORM == 'java'
107
- assert_raise(SystemExit) {
108
- RedisStat::Option.parse(%w[-h localhost:8888 10 -a password --csv=/tmp/a.csv --style=ascii --server=5555])
109
- }
110
107
  assert_raise(SystemExit) {
111
108
  RedisStat::Option.parse(%w[-h localhost:8888 10 -a password --csv=/tmp/a.csv --style=ascii --server=5555 --daemon])
112
109
  }
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
5
4
  prerelease:
5
+ version: 0.3.9
6
6
  platform: java
7
7
  authors:
8
8
  - Junegunn Choi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-27 00:00:00.000000000 Z
12
+ date: 2013-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ansi256