Skip to content

Commit

Permalink
Read environment at initialisation
Browse files Browse the repository at this point in the history
This allows us to expose the environment attribute from the engine
object and utilise it to build exported startup files.
  • Loading branch information
Thom May committed Sep 1, 2011
1 parent ddcaac8 commit 7fc6d02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
18 changes: 9 additions & 9 deletions lib/foreman/engine.rb
Expand Up @@ -11,6 +11,8 @@ class Foreman::Engine

attr_reader :procfile
attr_reader :directory
attr_reader :environment
attr_reader :options

extend Term::ANSIColor

Expand All @@ -20,6 +22,7 @@ def initialize(procfile, options={})
@procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile))
@options = options
@environment = read_environment(options[:env])
end

def processes
Expand Down Expand Up @@ -52,12 +55,10 @@ def processes_in_order
end

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

proctitle "ruby: foreman master"

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

trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
Expand All @@ -67,9 +68,8 @@ def start
end

def execute(name)
environment = read_environment(@options[:env])

fork processes[name], @options, environment
fork processes[name]

trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
trap("INT") { puts "SIGINT received"; terminate_gracefully }
Expand All @@ -85,16 +85,16 @@ def port_for(process, num, base_port=nil)

private ######################################################################

def fork(process, options={}, environment={})
def fork(process)
concurrency = Foreman::Utils.parse_concurrency(@options[:concurrency])

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

def fork_individual(process, num, port, environment)
environment.each { |k,v| ENV[k] = v }
def fork_individual(process, num, port)
@environment.each { |k,v| ENV[k] = v }

ENV["PORT"] = port.to_s
ENV["PS"] = "#{process.name}.#{num}"
Expand Down
17 changes: 7 additions & 10 deletions spec/foreman/engine_spec.rb
Expand Up @@ -37,18 +37,18 @@
describe "start" do
it "forks the processes" do
write_procfile
mock(subject).fork(subject.processes["alpha"], {}, {})
mock(subject).fork(subject.processes["bravo"], {}, {})
mock(subject).fork(subject.processes["alpha"])
mock(subject).fork(subject.processes["bravo"])
mock(subject).watch_for_termination
subject.start
end

it "handles concurrency" do
write_procfile
engine = Foreman::Engine.new("Procfile",:concurrency => "alpha=2")
mock(engine).fork_individual(engine.processes["alpha"], 1, 5000, {})
mock(engine).fork_individual(engine.processes["alpha"], 2, 5001, {})
mock(engine).fork_individual(engine.processes["bravo"], 1, 5100, {})
mock(engine).fork_individual(engine.processes["alpha"], 1, 5000)
mock(engine).fork_individual(engine.processes["alpha"], 2, 5001)
mock(engine).fork_individual(engine.processes["bravo"], 1, 5100)
mock(engine).watch_for_termination
engine.start
end
Expand All @@ -57,7 +57,7 @@
describe "execute" do
it "runs the processes" do
write_procfile
mock(subject).fork(subject.processes["alpha"], {}, {})
mock(subject).fork(subject.processes["alpha"])
mock(subject).watch_for_termination
subject.execute("alpha")
end
Expand All @@ -81,17 +81,14 @@
it "should fail if specified and doesnt exist" do
mock.instance_of(Foreman::Engine).error("No such file: /tmp/env")
engine = Foreman::Engine.new("Procfile", :env => "/tmp/env")
stub(engine).info
mock(engine).watch_for_termination
engine.execute("alpha")
end

it "should read .env if none specified" do
File.open(".env", "w") { |f| f.puts("FOO=qoo") }
engine = Foreman::Engine.new("Procfile")
stub(engine).info
mock(engine).watch_for_termination
mock(engine).fork_individual(anything, anything, anything, { "FOO" => "qoo" })
mock(engine).fork_individual(anything, anything, anything)
engine.execute("bravo")
end
end
Expand Down

0 comments on commit 7fc6d02

Please sign in to comment.