magistrate 0.2.0 → 0.2.1

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.
@@ -23,9 +23,9 @@ module Magistrate
23
23
 
24
24
  FileUtils.mkdir_p(@pid_path) unless File.directory? @pid_path
25
25
 
26
- @config[:workers].each do |k,v|
27
- v[:pid_path] ||= @pid_path
28
- @workers[k] = Process.new(k,v)
26
+ @config[:workers].each do |name,params|
27
+ params[:pid_path] ||= @pid_path
28
+ @workers[name] = Worker.new(name, params)
29
29
  end
30
30
 
31
31
  @loaded_from = nil
@@ -46,9 +46,19 @@ module Magistrate
46
46
  # Pull in all already-running workers and set their target states
47
47
  @workers.each do |k, worker|
48
48
  worker.supervise!
49
+
50
+ if worker.reset_target_state_to
51
+ begin
52
+ remote_request Net::HTTP::Post, "supervisors/#{self.name}/workers/#{worker.name}/set_target_state/#{worker.reset_target_state_to}"
53
+ rescue StandardError => e
54
+ log "Error resetting target_state for #{worker.name} to #{worker.reset_target_state_to}"
55
+ log "Error: #{e}"
56
+ end
57
+ end
58
+
49
59
  if @verbose
50
60
  puts "==== Worker: #{k}"
51
- worker.logs.join("\n")
61
+ puts worker.logs.join("\n")
52
62
  end
53
63
  end
54
64
 
@@ -92,8 +102,8 @@ module Magistrate
92
102
  :workers => {}
93
103
  }
94
104
 
95
- @workers.each do |k,process|
96
- s[:workers][k] = process.status
105
+ @workers.each do |k,worker|
106
+ s[:workers][k] = worker.status
97
107
  end
98
108
 
99
109
  s
@@ -126,7 +136,7 @@ module Magistrate
126
136
  # Gets and sets @target_states from the server
127
137
  # Automatically falls back to the local cache
128
138
  def load_remote_target_states!
129
- response = remote_request(Net::HTTP::Get)
139
+ response = remote_request Net::HTTP::Get, "api/status/#{self.name}"
130
140
 
131
141
  if response.code == '200'
132
142
  @loaded_from = :server
@@ -162,7 +172,7 @@ module Magistrate
162
172
  # Currently only sends basic worker info, but could start sending lots more:
163
173
  #
164
174
  def send_status
165
- remote_request Net::HTTP::Post, { :status => JSON.generate(status) }
175
+ remote_request Net::HTTP::Post, "api/status/#{self.name}", { :status => JSON.generate(status) }
166
176
  rescue StandardError => e
167
177
  log "Sending status to #{@config[:monitor_url]} failed"
168
178
  log "Error: #{e}"
@@ -174,11 +184,12 @@ module Magistrate
174
184
  end
175
185
 
176
186
  # Wrapper method for easy remote requests
177
- def remote_request(klass, form_data = nil)
187
+ def remote_request(klass, path, form_data = {})
188
+ log "#{klass} request to #{path}"
178
189
  http = Net::HTTP.new(@uri.host, @uri.port)
179
190
  http.read_timeout = 30
180
- request = klass.new(@uri.request_uri + "api/status/#{self.name}")
181
- request.set_form_data(form_data) if form_data
191
+ request = klass.new(@uri.request_uri + path)
192
+ request.set_form_data(form_data) if klass == Net::HTTP::Post
182
193
 
183
194
  if @config[:http_username] && @config[:http_password]
184
195
  request.basic_auth @config[:http_username], @config[:http_password]
@@ -1,3 +1,3 @@
1
1
  module Magistrate
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  module Magistrate
2
- class Process
2
+ class Worker
3
3
 
4
- attr_reader :name, :daemonize, :start_cmd, :stop_cmd, :pid_file, :working_dir, :env, :logs
4
+ attr_reader :name, :daemonize, :start_cmd, :stop_cmd, :pid_file, :working_dir, :env, :logs, :reset_target_state_to
5
5
  attr_accessor :target_state, :monitored
6
6
 
7
7
  def initialize(name, options = {})
@@ -62,21 +62,26 @@ class Process
62
62
  def supervise!
63
63
  log "Supervising. Is: #{state}. Target: #{@target_state}"
64
64
  if state != @target_state
65
- if @target_state == :running
66
- start
67
- elsif @target_state == :stopped
65
+ case @target_state
66
+ when :forced_restart then
67
+ @reset_target_state_to = :running
68
+ log "Restart: Stopping, then Starting, then reporting new target_state of :running"
68
69
  stop
70
+ start
71
+ when :running then start
72
+ when :stopped then stop
69
73
  end
70
74
  end
71
75
  end
72
76
 
73
77
  def start
74
- log "#{@name} starting"
75
78
  if @daemonize
79
+ log "Starting as daemon with double_fork"
76
80
  @pid = double_fork(@start_cmd)
77
81
  # TODO: Should check if the pid really exists as we expect
78
82
  write_pid
79
83
  else
84
+ log "Starting as self-daemonizing with single_fork"
80
85
  @pid = single_fork(@start_cmd)
81
86
  end
82
87
  @pid
data/lib/magistrate.rb CHANGED
@@ -2,8 +2,8 @@ $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'magistrate/core_ext'
4
4
  require 'magistrate/version'
5
+ require 'magistrate/worker'
5
6
  require 'magistrate/supervisor'
6
- require 'magistrate/process'
7
7
 
8
8
  module Magistrate
9
9
 
@@ -1,10 +1,10 @@
1
1
  require "spec_helper"
2
- require "magistrate/process"
2
+ require "magistrate/worker"
3
3
 
4
- describe "Magistrate::Process" do
4
+ describe "Magistrate::Worker" do
5
5
  describe 'Rake-Like Worker' do
6
6
  before(:each) do
7
- @process = Magistrate::Process.new(
7
+ @process = Magistrate::Worker.new(
8
8
  :name => 'rake_like_worker',
9
9
  :daemonize => true,
10
10
  :start_cmd => 'ruby spec/resources/rake_like_worker.rb'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magistrate
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
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-22 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
@@ -151,13 +151,13 @@ extra_rdoc_files: []
151
151
  files:
152
152
  - bin/magistrate
153
153
  - lib/magistrate/core_ext.rb
154
- - lib/magistrate/process.rb
155
154
  - lib/magistrate/supervisor.rb
156
155
  - lib/magistrate/version.rb
156
+ - lib/magistrate/worker.rb
157
157
  - lib/magistrate.rb
158
158
  - README.md
159
- - spec/magistrate/process_spec.rb
160
159
  - spec/magistrate/supervisor_spec.rb
160
+ - spec/magistrate/worker_spec.rb
161
161
  - spec/magistrate_spec.rb
162
162
  - spec/resources/example.yml
163
163
  - spec/resources/rake_like_worker.rb