magistrate_monitor 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/server.rb CHANGED
@@ -66,7 +66,7 @@ module MagistrateMonitor
66
66
  ActiveSupport::JSON.encode( {:status => 'Ok'} )
67
67
  end
68
68
 
69
- post '/set/:supervisor_name/:worker_name/:action' do
69
+ post '/supervisors/:supervisor_name/workers/:worker_name/set_target_state/:action' do
70
70
  @supervisor = Supervisor.find_or_create_by_name params[:supervisor_name]
71
71
 
72
72
  @supervisor.set_target_state!(params[:action], params[:worker_name])
@@ -79,8 +79,8 @@ module MagistrateMonitor
79
79
  # "#{root}/#{path}"
80
80
  # end
81
81
 
82
- def url_for_worker(supervisor, name, action)
83
- url("/set/#{supervisor.name}/#{name}/#{action}")
82
+ def target_state_url_for_worker(supervisor, name, action)
83
+ url("/supervisors/#{supervisor.name}/workers/#{name}/set_target_state/#{action}")
84
84
  end
85
85
 
86
86
  def normalize_status_data!
@@ -19,11 +19,13 @@ module Sinatra
19
19
  end
20
20
 
21
21
  def database
22
- @database ||= (
22
+ @database ||= if defined?(Rails)
23
+ ActiveRecord::Base
24
+ else
23
25
  #ActiveRecord::Base.logger ||= activerecord_logger # Having this enabled overrides Rails TODO: Find a way to make it not override Rails
24
26
  ActiveRecord::Base.establish_connection(database_options)
25
27
  ActiveRecord::Base
26
- )
28
+ end
27
29
  end
28
30
 
29
31
  protected
@@ -35,16 +37,11 @@ module Sinatra
35
37
  app.helpers ActiveRecordHelper
36
38
 
37
39
  app.configure do
38
- if defined?(Rails)
39
- env = Rails.env
40
- else
41
-
40
+ unless defined?(Rails)
42
41
  env = ENV['RACK_ENV'] || 'development'
42
+ file = File.join('config', 'database.yml')
43
+ app.database = YAML::load(ERB.new(IO.read(file)).result).with_indifferent_access[env]
43
44
  end
44
-
45
- file = File.join('config', 'database.yml')
46
-
47
- app.database = YAML::load(ERB.new(IO.read(file)).result).with_indifferent_access[env]
48
45
  end
49
46
  end
50
47
  end
data/lib/supervisor.rb CHANGED
@@ -22,11 +22,24 @@ module MagistrateMonitor
22
22
  self.databag['workers'] ||= {}
23
23
  end
24
24
 
25
- def set_target_state!(action, worker)
25
+ def normalize_databag_for!(worker)
26
26
  d = self.databag || {}
27
27
  d['workers'] ||= {}
28
- d['workers'][worker] = {'target_state' => action }
29
- self.update_attribute :databag, d
28
+ d['workers'][worker] ||= {}
29
+ self.databag = d
30
+ end
31
+
32
+ # This method abstracts access to a worker's databag. It guarantees to return a hash of some sort useful for referencing worker properties
33
+ # However, changes to this hash may not be propegated back.
34
+ # Quite possibly it would be better to do checking here like in set_target_state! to normalize the data?
35
+ def databag_for(worker)
36
+ (self.databag['workers'].is_a?(Hash) ? self.databag['workers'][worker] : {}) || {}
37
+ end
38
+
39
+ def set_target_state!(action, worker)
40
+ normalize_databag_for!(worker)
41
+ self.databag['workers'][worker]['target_state'] = action
42
+ self.update_attribute :databag, self.databag
30
43
  end
31
44
  end
32
45
  end
data/lib/views/index.erb CHANGED
@@ -9,7 +9,7 @@
9
9
  <th>State</th>
10
10
  <th>Supervisor Target State</th>
11
11
  <th>Monitor Target State</th>
12
- <th colspan="3">Actions</th>
12
+ <th colspan="4">Actions</th>
13
13
  </tr>
14
14
  </thead>
15
15
 
@@ -17,30 +17,37 @@
17
17
  <tr>
18
18
  <th><a href="<%= url "/supervisors/#{supervisor.name}" %>"><%= supervisor.name %></a><br>
19
19
  <%= supervisor.last_checkin_at %></th>
20
- <th colspan="7"> - </th>
20
+ <th colspan="8"> - </th>
21
21
  <td><a href="<%= url "/supervisors/#{supervisor.name}/delete" %>" data-confirm="Are you sure?" data-method="post" rel="nofollow">Delete</a><br>
22
22
  </tr>
23
23
  <% supervisor.status['workers'].each do |k,v| %>
24
+ <% databag = supervisor.databag_for(k) %>
25
+ <% current_target_state = databag['target_state'] %>
24
26
  <tr>
25
27
  <th> - </th>
26
28
  <th><%= k %></th>
27
29
  <td><%= v['pid'] %></td>
28
30
  <td><%= v['state'] %></td>
29
31
  <td><%= v['target_state']%></td>
30
- <td><%= supervisor.databag['workers'][k]['target_state'] if supervisor.databag['workers'].is_a?(Hash) && supervisor.databag['workers'][k].is_a?(Hash) %></td>
32
+ <td><%= current_target_state %></td>
31
33
  <td>
32
- <% if v['state'] != 'running' %>
33
- <a href="<%= url_for_worker(supervisor, k, 'running') %>" data-method="post" rel="nofollow">Run</a>
34
+ <% if current_target_state != 'running' %>
35
+ <a href="<%= target_state_url_for_worker(supervisor, k, 'running') %>" data-method="post" rel="nofollow">Run</a>
34
36
  <% end %>
35
37
  </td>
36
38
  <td>
37
- <% if v['state'] != 'stopped' %>
38
- <a href="<%= url_for_worker(supervisor, k, 'stopped') %>" data-method="post" rel="nofollow">Stop</a>
39
+ <% if current_target_state != 'stopped' %>
40
+ <a href="<%= target_state_url_for_worker(supervisor, k, 'stopped') %>" data-method="post" rel="nofollow">Stop</a>
39
41
  <% end %>
40
42
  </td>
41
43
  <td>
42
- <% if v['state'] != 'unmonitored' %>
43
- <a href="<%= url_for_worker(supervisor, k, 'unmonitored') %>" data-method="post" rel="nofollow">Unmonitor</a>
44
+ <% if current_target_state != 'forced_restart' %>
45
+ <a href="<%= target_state_url_for_worker(supervisor, k, 'forced_restart') %>" data-method="post" rel="nofollow">Restart</a>
46
+ <% end %>
47
+ </td>
48
+ <td>
49
+ <% if current_target_state != 'unmonitored' %>
50
+ <a href="<%= target_state_url_for_worker(supervisor, k, 'unmonitored') %>" data-method="post" rel="nofollow">Unmonitor</a>
44
51
  <% end %>
45
52
  </td>
46
53
  </tr>
data/spec/server_spec.rb CHANGED
@@ -28,6 +28,30 @@ describe "MagistrateMonitor::Server" do
28
28
  last_response.status.should == 302
29
29
  end
30
30
 
31
+ it 'should set the target state to running' do
32
+ @supervisor.set_target_state!('stopped', 'worker1')
33
+ @supervisor.databag['workers']['worker1']['target_state'].should == 'stopped'
34
+
35
+ post '/supervisors/foo/workers/worker1/set_target_state/running'
36
+
37
+ last_response.status.should == 302
38
+
39
+ @supervisor.reload
40
+ @supervisor.databag['workers']['worker1']['target_state'].should == 'running'
41
+ end
42
+
43
+ ['stopped', 'restart', 'unmonitored'].each do |action|
44
+ it "should set the target state to #{action}" do
45
+ @supervisor.databag['workers']['worker1']['target_state'].should_not == action
46
+ post "/supervisors/foo/workers/worker1/set_target_state/#{action}"
47
+
48
+ last_response.status.should == 302
49
+
50
+ @supervisor.reload
51
+ @supervisor.databag['workers']['worker1']['target_state'].should == action
52
+ end
53
+ end
54
+
31
55
  context 'api' do
32
56
  it 'should provide the databag' do
33
57
  get '/api/status/foo'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magistrate_monitor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Drew Blas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-10 00:00:00 -05:00
18
+ date: 2011-08-24 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency