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 |k,v|
    v[:pid_path] ||= @pid_path
    @workers[k] = Process.new(k,v)
  end
  
  @loaded_from = nil
  @logs = []
  @verbose = overrides[:verbose]
  
  if @verbose
    require 'pp'
  end
end

Instance Method Details

#list(params = nil) ⇒ Object



77
78
79
80
81
82
# File 'lib/magistrate/supervisor.rb', line 77

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

#log(str) ⇒ Object



102
103
104
105
# File 'lib/magistrate/supervisor.rb', line 102

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
# 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 @verbose
      puts "==== Worker: #{k}"
      worker.logs.join("\n")
    end
  end
  
  send_status
  
  log "Run Complete at: #{Time.now}" #This is only good in verbose mode, but that's ok
end

#start(params = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/magistrate/supervisor.rb', line 61

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/magistrate/supervisor.rb', line 85

def status
  s = {
       :name => self.name,
       :pid_path => @pid_path,
       :monitor_url => @config[:monitor_url],
       :config_file => @config_file,
       :logs => @logs,
       :workers => {}
      }
  
  @workers.each do |k,process|
    s[:workers][k] = process.status
  end
  
  s
end

#stop(params = nil) ⇒ Object



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

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