Class: Procodile::Config

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

Constant Summary collapse

COLORS =
[35, 31, 36, 32, 33, 34]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
17
18
# File 'lib/procodile/config.rb', line 12

def initialize(root)
  @root = root
  unless File.exist?(procfile_path)
    raise Error, "Procfile not found at #{procfile_path}"
  end
  FileUtils.mkdir_p(pid_root)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/procodile/config.rb', line 10

def root
  @root
end

Instance Method Details

#app_nameObject



47
48
49
# File 'lib/procodile/config.rb', line 47

def app_name
  @app_name ||= options['app_name'] || 'Procodile'
end

#log_pathObject



74
75
76
# File 'lib/procodile/config.rb', line 74

def log_path
  @log_path ||= File.expand_path(options['log_path'] || 'procodile.log', @root)
end

#optionsObject



62
63
64
# File 'lib/procodile/config.rb', line 62

def options
  @options ||= File.exist?(options_path) ? YAML.load_file(options_path) : {}
end

#pid_rootObject



70
71
72
# File 'lib/procodile/config.rb', line 70

def pid_root
  @pid_root ||= File.expand_path(options['pid_root'] || 'pids', @root)
end

#process_listObject



58
59
60
# File 'lib/procodile/config.rb', line 58

def process_list
  @process_list ||= YAML.load_file(procfile_path)
end

#process_optionsObject



66
67
68
# File 'lib/procodile/config.rb', line 66

def process_options
  @process_options ||= options['processes'] || {}
end

#processesObject



51
52
53
54
55
56
# File 'lib/procodile/config.rb', line 51

def processes
  @processes ||= process_list.each_with_index.each_with_object({}) do |((name, command), index), hash|
    hash[name] = Process.new(self, name, command, process_options[name] || {})
    hash[name].log_color = COLORS[index.divmod(COLORS.size)[1]]
  end
end

#reloadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/procodile/config.rb', line 20

def reload
  @process_list = nil
  @options = nil
  @process_options = nil

  process_list.each do |name, command|
    if process = @processes[name]
      # This command is already in our list. Add it.
      if process.command != command
        process.command = command
        Procodile.log nil, 'system', "#{name} command has changed. Updated."
      end

      if process_options[name].is_a?(Hash)
        process.options = process_options[name]
      else
        process.options = {}
      end
    else
      Procodile.log nil, 'system', "#{name} has been added to the Procfile. Adding it."
      @processes[name] = Process.new(self, name, command, process_options[name] || {})
      @processes[name].log_color = COLORS[@processes.size.divmod(COLORS.size)[1]]
    end
  end

end

#sock_pathObject



78
79
80
# File 'lib/procodile/config.rb', line 78

def sock_path
  File.join(pid_root, 'supervisor.sock')
end