Class: Foreman::Engine

Inherits:
Object
  • Object
show all
Extended by:
Term::ANSIColor
Defined in:
lib/foreman/engine.rb

Constant Summary collapse

COLORS =
[ cyan, yellow, green, magenta, red ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(procfile, options = {}) ⇒ Engine

Returns a new instance of Engine.



19
20
21
22
23
24
25
26
27
# File 'lib/foreman/engine.rb', line 19

def initialize(procfile, options={})
  @procfile =
  if procfile.end_with? ".rb"
    eval_procfile(procfile, options)
  else
    read_procfile(procfile)
  end
  @directory = File.expand_path(File.dirname(procfile))
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



13
14
15
# File 'lib/foreman/engine.rb', line 13

def directory
  @directory
end

#procfileObject (readonly)

Returns the value of attribute procfile.



12
13
14
# File 'lib/foreman/engine.rb', line 12

def procfile
  @procfile
end

Instance Method Details

#execute(name, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/foreman/engine.rb', line 73

def execute(name, options={})
  environment = read_environment(options[:env])

  fork processes[name], options, environment

  trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
  trap("INT")  { puts "SIGINT received";  terminate_gracefully }

  watch_for_termination
end

#port_for(process, num, base_port = nil) ⇒ Object



84
85
86
87
88
# File 'lib/foreman/engine.rb', line 84

def port_for(process, num, base_port=nil)
  base_port ||= 5000
  offset = processes_in_order.map { |p| p.first }.index(process.name) * 100
  base_port.to_i + offset + num - 1
end

#process_orderObject



47
48
49
50
# File 'lib/foreman/engine.rb', line 47

def process_order
  processes
  @order.uniq
end

#processesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/foreman/engine.rb', line 29

def processes
  @processes ||= begin
    @order = []
    procfile.split("\n").inject({}) do |hash, line|
      next hash if line.strip == ""
      name, command = line.split(/ *: +/, 2)
      unless command
        warn_deprecated_procfile!
        name, command = line.split(/ +/, 2)
      end
      process = Foreman::Process.new(name, command)
      process.color = next_color
      @order << process.name
      hash.update(process.name => process)
    end
  end
end

#processes_in_orderObject



52
53
54
55
56
# File 'lib/foreman/engine.rb', line 52

def processes_in_order
  process_order.map do |name|
    [name, processes[name]]
  end
end

#start(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/foreman/engine.rb', line 58

def start(options={})
  environment = read_environment(options[:env])

  proctitle "ruby: foreman master"

  processes_in_order.each do |name, process|
    fork process, options, environment
  end

  trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
  trap("INT")  { puts "SIGINT received";  terminate_gracefully }

  watch_for_termination
end