Class: Procodile::Supervisor

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Supervisor

Create a new supervisor instance that will be monitoring the processes that have been provided.



6
7
8
9
# File 'lib/procodile/supervisor.rb', line 6

def initialize(config)
  @config = config
  @instances = []
end

Instance Method Details

#restartObject



35
36
37
38
# File 'lib/procodile/supervisor.rb', line 35

def restart
  Procodile.log nil, 'system', "Restarting all #{@config.app_name} processes"
  @instances.each(&:restart)
end

#startObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/procodile/supervisor.rb', line 11

def start
  Procodile.log nil, "system", "#{@config.app_name} supervisor started with PID #{::Process.pid}"
  @config.processes.each do |name, process|
    process.generate_instances.each do |instance|
      instance.start
      @instances << instance
    end
  end
  supervise
end

#statusObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/procodile/supervisor.rb', line 40

def status
  Procodile.log '37;44', 'status', "Status as at: #{Time.now.utc.to_s}"
  @instances.each do |instance|
    if instance.running?
      Procodile.log '37;44', 'status', "#{instance.description} is RUNNING (pid #{instance.pid}). Respawned #{instance.respawns} time(s)"
    else
      Procodile.log '37;44', 'status', "#{instance.description} is STOPPED"
    end
  end
end

#stopObject



22
23
24
25
26
27
# File 'lib/procodile/supervisor.rb', line 22

def stop
  return if @stopping
  @stopping = true
  Procodile.log nil, "system", "Stopping all #{@config.app_name} processes"
  @instances.each(&:stop)
end

#stop_supervisorObject



29
30
31
32
33
# File 'lib/procodile/supervisor.rb', line 29

def stop_supervisor
  Procodile.log nil, 'system', "Stopping #{@config.app_name} supervisor"
  FileUtils.rm_f(File.join(@config.pid_root, 'supervisor.pid'))
  ::Process.exit 0
end

#superviseObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/procodile/supervisor.rb', line 51

def supervise
  loop do
    # Tidy up any instances that we no longer wish to be managed. They will
    # be removed from the list.
    remove_dead_instances

    if @stopping
      # If the system is stopping, we'll remove any instances that have already
      # stopped and trigger their on_stop callback.
      remove_stopped_instances

      # When all the instances we manage have gone away, we can stop ourself.
      if @instances.size > 0
        Procodile.log nil, "system", "Waiting for #{@instances.size} processes to stop"
      else
        Procodile.log nil, "system", "All processes have stopped"
        stop_supervisor
      end
    else
      # Check all instances that we manage and let them do their things.
      @instances.each(&:check)
    end

    sleep 5
  end
end