Read environment at initialisation

This allows us to expose the environment attribute from the engine
object and utilise it to build exported startup files.
This commit is contained in:
Thom May
2011-09-01 17:26:50 +01:00
parent ddcaac8749
commit 7fc6d02e7b
2 changed files with 16 additions and 19 deletions
+9 -9
View File
@@ -11,6 +11,8 @@ class Foreman::Engine
attr_reader :procfile attr_reader :procfile
attr_reader :directory attr_reader :directory
attr_reader :environment
attr_reader :options
extend Term::ANSIColor extend Term::ANSIColor
@@ -20,6 +22,7 @@ class Foreman::Engine
@procfile = read_procfile(procfile) @procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile)) @directory = File.expand_path(File.dirname(procfile))
@options = options @options = options
@environment = read_environment(options[:env])
end end
def processes def processes
@@ -52,12 +55,10 @@ class Foreman::Engine
end end
def start def start
environment = read_environment(@options[:env])
proctitle "ruby: foreman master" proctitle "ruby: foreman master"
processes_in_order.each do |name, process| processes_in_order.each do |name, process|
fork process, @options, environment fork process
end end
trap("TERM") { puts "SIGTERM received"; terminate_gracefully } trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
@@ -67,9 +68,8 @@ class Foreman::Engine
end end
def execute(name) def execute(name)
environment = read_environment(@options[:env])
fork processes[name], @options, environment fork processes[name]
trap("TERM") { puts "SIGTERM received"; terminate_gracefully } trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
trap("INT") { puts "SIGINT received"; terminate_gracefully } trap("INT") { puts "SIGINT received"; terminate_gracefully }
@@ -85,16 +85,16 @@ class Foreman::Engine
private ###################################################################### private ######################################################################
def fork(process, options={}, environment={}) def fork(process)
concurrency = Foreman::Utils.parse_concurrency(@options[:concurrency]) concurrency = Foreman::Utils.parse_concurrency(@options[:concurrency])
1.upto(concurrency[process.name]) do |num| 1.upto(concurrency[process.name]) do |num|
fork_individual(process, num, port_for(process, num, @options[:port]), environment) fork_individual(process, num, port_for(process, num, @options[:port]))
end end
end end
def fork_individual(process, num, port, environment) def fork_individual(process, num, port)
environment.each { |k,v| ENV[k] = v } @environment.each { |k,v| ENV[k] = v }
ENV["PORT"] = port.to_s ENV["PORT"] = port.to_s
ENV["PS"] = "#{process.name}.#{num}" ENV["PS"] = "#{process.name}.#{num}"
+7 -10
View File
@@ -37,8 +37,8 @@ describe "Foreman::Engine" do
describe "start" do describe "start" do
it "forks the processes" do it "forks the processes" do
write_procfile write_procfile
mock(subject).fork(subject.processes["alpha"], {}, {}) mock(subject).fork(subject.processes["alpha"])
mock(subject).fork(subject.processes["bravo"], {}, {}) mock(subject).fork(subject.processes["bravo"])
mock(subject).watch_for_termination mock(subject).watch_for_termination
subject.start subject.start
end end
@@ -46,9 +46,9 @@ describe "Foreman::Engine" do
it "handles concurrency" do it "handles concurrency" do
write_procfile write_procfile
engine = Foreman::Engine.new("Procfile",:concurrency => "alpha=2") engine = Foreman::Engine.new("Procfile",:concurrency => "alpha=2")
mock(engine).fork_individual(engine.processes["alpha"], 1, 5000, {}) mock(engine).fork_individual(engine.processes["alpha"], 1, 5000)
mock(engine).fork_individual(engine.processes["alpha"], 2, 5001, {}) mock(engine).fork_individual(engine.processes["alpha"], 2, 5001)
mock(engine).fork_individual(engine.processes["bravo"], 1, 5100, {}) mock(engine).fork_individual(engine.processes["bravo"], 1, 5100)
mock(engine).watch_for_termination mock(engine).watch_for_termination
engine.start engine.start
end end
@@ -57,7 +57,7 @@ describe "Foreman::Engine" do
describe "execute" do describe "execute" do
it "runs the processes" do it "runs the processes" do
write_procfile write_procfile
mock(subject).fork(subject.processes["alpha"], {}, {}) mock(subject).fork(subject.processes["alpha"])
mock(subject).watch_for_termination mock(subject).watch_for_termination
subject.execute("alpha") subject.execute("alpha")
end end
@@ -81,9 +81,6 @@ describe "Foreman::Engine" do
it "should fail if specified and doesnt exist" do it "should fail if specified and doesnt exist" do
mock.instance_of(Foreman::Engine).error("No such file: /tmp/env") mock.instance_of(Foreman::Engine).error("No such file: /tmp/env")
engine = Foreman::Engine.new("Procfile", :env => "/tmp/env") engine = Foreman::Engine.new("Procfile", :env => "/tmp/env")
stub(engine).info
mock(engine).watch_for_termination
engine.execute("alpha")
end end
it "should read .env if none specified" do it "should read .env if none specified" do
@@ -91,7 +88,7 @@ describe "Foreman::Engine" do
engine = Foreman::Engine.new("Procfile") engine = Foreman::Engine.new("Procfile")
stub(engine).info stub(engine).info
mock(engine).watch_for_termination mock(engine).watch_for_termination
mock(engine).fork_individual(anything, anything, anything, { "FOO" => "qoo" }) mock(engine).fork_individual(anything, anything, anything)
engine.execute("bravo") engine.execute("bravo")
end end
end end