Class: Magistrate::Supervisor

Inherits:
Object
  • Object
show all
Defined in:
lib/magistrate/supervisor.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file, overrides = {}) ⇒ Supervisor

Returns a new instance of Supervisor.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/magistrate/supervisor.rb', line 13

def initialize(config_file, overrides = {})
  @workers = {}
  
  #File.expand_path('~')
  @config_file = config_file
  @config = File.open(config_file) { |file| YAML.load(file) }    
  @config.recursive_symbolize_keys!

  @uri = URI.parse @config[:monitor_url]
  @pid_path = @config[:pid_path] || File.join( 'tmp', 'pids' )
  
  FileUtils.mkdir_p(@pid_path) unless File.directory? @pid_path

  @config[:workers].each do |name,params|
    params[:pid_path] ||= @pid_path
    @workers[name] = Worker.new(name, params)
  end
  
  @loaded_from = nil
  @logs = []
  @verbose = overrides[:verbose]
  
  if @verbose
    require 'pp'
  end
end

Instance Method Details

#list(params = nil) ⇒ Object



87
88
89
90
91
92
# File 'lib/magistrate/supervisor.rb', line 87

def list(params = nil)
  set_target_states!
  
  require 'pp'
  pp status
end

#log(str) ⇒ Object



113
114
115
116
# File 'lib/magistrate/supervisor.rb', line 113

def log(str)
  @logs << str
  puts str if @verbose
end

#run(params = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/magistrate/supervisor.rb', line 40

def run(params = nil)
  log "Run started at: #{Time.now}"
  
  log "Starting Magistrate [[[#{self.name}]]] talking to [[[#{@config[:monitor_url]}]]]"
  set_target_states!
  
  # Pull in all already-running workers and set their target states
  @workers.each do |k, worker|
    worker.supervise!
    
    if worker.reset_target_state_to
      begin
        remote_request Net::HTTP::Post, "supervisors/#{self.name}/workers/#{worker.name}/set_target_state/#{worker.reset_target_state_to}"
      rescue StandardError => e
        log "Error resetting target_state for #{worker.name} to #{worker.reset_target_state_to}"
        log "Error: #{e}"
      end
    end
    
    if @verbose
      puts "==== Worker: #{k}"
      puts worker.logs.join("\n")
    end
  end

  log "Run Complete at: #{Time.now}" #This is only good in verbose mode, but that's ok

  send_status
end

#start(params = nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/magistrate/supervisor.rb', line 71

def start(params = nil)
  worker = params
  log "Starting: #{worker}"
  @workers[worker.to_sym].supervise!
  
  # Save that we've requested this to be started
end

#statusObject

Returns the actual hash of all workers and their status



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/magistrate/supervisor.rb', line 95

def status
  s = {
       :name => self.name,
       :pid_path => @pid_path,
       :monitor_url => @config[:monitor_url],
       :config_file => @config_file,
       :logs => @logs,
       :env => `env`.split("\n"),
       :workers => {}
      }
  
  @workers.each do |k,worker|
    s[:workers][k] = worker.status
  end
  
  s
end

#stop(params = nil) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/magistrate/supervisor.rb', line 79

def stop(params = nil)
  worker = params
  log "Stopping: #{worker}"
  @workers[worker.to_sym].stop
  
  # Save that we've requested this to be stopped
end