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) ⇒ Engine

Returns a new instance of Engine.



18
19
20
21
# File 'lib/foreman/engine.rb', line 18

def initialize(procfile)
  @procfile  = read_procfile(procfile)
  @directory = File.expand_path(File.dirname(procfile))
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

#procfileObject (readonly)

Returns the value of attribute procfile.



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

def procfile
  @procfile
end

Instance Method Details

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



48
49
50
51
52
53
54
55
# File 'lib/foreman/engine.rb', line 48

def execute(name, options={})
  fork processes[name], options

  trap("TERM") { kill_and_exit("TERM") }
  trap("INT")  { kill_and_exit("INT")  }

  watch_for_termination
end

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



57
58
59
60
61
# File 'lib/foreman/engine.rb', line 57

def port_for(process, num, base_port=nil)
  base_port ||= 5000
  offset = processes.keys.sort.index(process.name) * 100
  base_port.to_i + offset + num - 1
end

#processesObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/foreman/engine.rb', line 23

def processes
  @processes ||= begin
    procfile.split("\n").inject({}) do |hash, line|
      next if line.strip == ""
      name, command = line.split(" ", 2)
      process = Foreman::Process.new(name, command)
      process.color = next_color
      hash.update(process.name => process)
    end
  end
end

#start(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/foreman/engine.rb', line 35

def start(options={})
  proctitle "ruby: foreman master"

  processes.each do |name, process|
    fork process, options
  end

  trap("TERM") { kill_and_exit("TERM") }
  trap("INT")  { kill_and_exit("INT")  }

  watch_for_termination
end