Class: Pazuzu::Supervisor

Inherits:
Object
  • Object
show all
Includes:
Utility::Runnable
Defined in:
lib/pazuzu/supervisor.rb

Overview

The supervisor controls the different applications registered with it.

Constant Summary collapse

DEFAULT_SOCKET_PATH =
'/var/run/pazuzud.socket'

Instance Attribute Summary collapse

Attributes included from Utility::Runnable

#started_at, #stopped_at

Instance Method Summary collapse

Methods included from Utility::Runnable

#run_state, #start!, #stop!, #wait_for_state_change!

Constructor Details

#initialize(options) ⇒ Supervisor

Returns a new instance of Supervisor.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pazuzu/supervisor.rb', line 12

def initialize(options)
  options.assert_valid_keys(:config_path)
  @logger = Logger.new($stderr)
  @logger.level = Logger::WARN
  @applications = Utility::RunnablePool.new
  @config_path = options[:config_path]
  Thread.start {
    # Wait for PIDs, otherwise we will get zombies
    loop do
      begin
        Process.waitpid
      rescue Errno::ECHILD
        sleep(1)
      rescue
        # Ignore
      end
    end
  }
  super()
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



131
132
133
# File 'lib/pazuzu/supervisor.rb', line 131

def logger
  @logger
end

Instance Method Details

#applicationsObject



116
117
118
# File 'lib/pazuzu/supervisor.rb', line 116

def applications
  @applications.children
end

#cgroup_for_instance(instance) ⇒ Object

Returns a Cgroup object given an instance.



121
122
123
124
125
126
127
128
129
# File 'lib/pazuzu/supervisor.rb', line 121

def cgroup_for_instance(instance)
  path = [
    @cgroup_hieararchy_root,
    instance.worker.application.name,
    instance.worker.name,
    instance.index
  ].join('/')
  Cgroup.new(@cgroups_fs_root_path, path, @cgroup_subsystems)
end

#configure!(configuration) ⇒ Object



45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pazuzu/supervisor.rb', line 45

def configure!(configuration)
  case configuration['log_path']
    when 'syslog'
      @logger = SyslogLogger('pazuzu')
    when nil
      @logger = Logger.new($stderr)
    else
      @logger = Logger.new(configuration['log_path'])
  end
  @logger.level = Logger::DEBUG

  new_socket_path = configuration['socket_path']
  new_socket_path ||= DEFAULT_SOCKET_PATH
  if @socket_path and new_socket_path != @socket_path
    @logger.warn("Cannot change socket path after start")
  else
    @socket_path = new_socket_path
  end

  @socket_server ||= Control::SocketServer.new(self, @socket_path)

  leftover_applications = @applications.children.dup
  (configuration['applications'] || {}).each do |name, app_config|
    if app_config
      name = name.to_s
      application = @applications.children.select { |a| a.name == name }.first
      if application
        leftover_applications.delete(application)
      else
        @logger.info("Adding application #{name}")
        application = Application.new(self, name)
        @applications.register(application)
      end
      application.configure!(app_config['procfile'], app_config)
    end
  end
  leftover_applications.each do |application|
    @logger.info("Removing application #{name}")
    @applications.unregister(application)
  end

  cgroups_config = configuration['cgroups'] || {}

  new_cgroup_hiearchy_root = cgroups_config['hieararchy_root']
  new_cgroup_hiearchy_root ||= 'pazuzu'
  new_cgroup_hiearchy_root.gsub!(/^\/+/, '')
  new_cgroup_hiearchy_root.gsub!(/\/$/, '')
  if @cgroup_hieararchy_root and new_cgroup_hiearchy_root != @cgroup_hieararchy_root
    @logger.warn("Cannot change cgroups hiearchy root after start")
  else
    @cgroup_hieararchy_root = new_cgroup_hiearchy_root
  end

  new_cgroup_subsystems = [cgroups_config['subsystems']].flatten.compact
  new_cgroup_subsystems ||= %w(memory cpu cpuacct blkio)
  if @cgroup_subsystems and new_cgroup_subsystems != @cgroup_subsystems
    @logger.warn("Cannot change cgroups subsystems after start")
  else
    @cgroup_subsystems = new_cgroup_subsystems
  end

  new_cgroups_fs_root_path = cgroups_config['fs_root']
  new_cgroups_fs_root_path ||= '/sys/fs/cgroup'
  new_cgroups_fs_root_path.gsub!(/\/$/, '')
  if @cgroups_fs_root_path and @cgroups_fs_root_path != new_cgroups_fs_root_path
    @logger.warn("Cannot change cgroups root after start")
  else
    @cgroups_fs_root_path = new_cgroups_fs_root_path
  end
end

#load_configuration!Object



41
42
43
# File 'lib/pazuzu/supervisor.rb', line 41

def load_configuration!
  configure!(load_config_from_yaml(@config_path))
end

#run!Object



33
34
35
36
37
38
39
# File 'lib/pazuzu/supervisor.rb', line 33

def run!
  load_configuration!
  start!
  while [:starting, :running].include?(run_state)
    sleep(1)
  end
end