Skip to content

Commit

Permalink
Move options into class initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom May committed Sep 1, 2011
1 parent bda6e30 commit ddcaac8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/foreman/cli.rb
Expand Up @@ -18,9 +18,9 @@ def start(process=nil)
check_procfile!

if process
engine.execute(process, options)
engine.execute(process)
else
engine.start(options)
engine.start
end
end

Expand Down Expand Up @@ -64,7 +64,7 @@ def check_procfile!
end

def engine
@engine ||= Foreman::Engine.new(procfile)
@engine ||= Foreman::Engine.new(procfile, options)
end

def procfile
Expand Down
19 changes: 10 additions & 9 deletions lib/foreman/engine.rb
Expand Up @@ -16,9 +16,10 @@ class Foreman::Engine

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

def initialize(procfile)
def initialize(procfile, options={})
@procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile))
@options = options
end

def processes
Expand Down Expand Up @@ -50,13 +51,13 @@ def processes_in_order
end
end

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

proctitle "ruby: foreman master"

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

trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
Expand All @@ -65,10 +66,10 @@ def start(options={})
watch_for_termination
end

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

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

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

def fork(process, options={}, environment={})
concurrency = Foreman::Utils.parse_concurrency(options[:concurrency])
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]), environment)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/foreman/cli_spec.rb
Expand Up @@ -19,7 +19,7 @@

it "runs successfully" do
dont_allow(subject).error
mock.instance_of(Foreman::Engine).start({})
mock.instance_of(Foreman::Engine).start
subject.start
end
end
Expand Down
35 changes: 22 additions & 13 deletions spec/foreman/engine_spec.rb
Expand Up @@ -2,7 +2,7 @@
require "foreman/engine"

describe "Foreman::Engine" do
subject { Foreman::Engine.new("Procfile") }
subject { Foreman::Engine.new("Procfile", {}) }

describe "initialize" do
describe "without an existing Procfile" do
Expand Down Expand Up @@ -45,11 +45,12 @@

it "handles concurrency" do
write_procfile
mock(subject).fork_individual(subject.processes["alpha"], 1, 5000, {})
mock(subject).fork_individual(subject.processes["alpha"], 2, 5001, {})
mock(subject).fork_individual(subject.processes["bravo"], 1, 5100, {})
mock(subject).watch_for_termination
subject.start(: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"], 2, 5001, {})
mock(engine).fork_individual(engine.processes["bravo"], 1, 5100, {})
mock(engine).watch_for_termination
engine.start
end
end

Expand All @@ -63,27 +64,35 @@
end

describe "environment" do

before(:each) do
write_procfile
stub(Process).fork
stub(subject).info
mock(subject).watch_for_termination
end

it "should read if specified" do
File.open("/tmp/env", "w") { |f| f.puts("FOO=baz") }
subject.execute("alpha", :env => "/tmp/env")
engine = Foreman::Engine.new("Procfile", :env => "/tmp/env")
stub(engine).info
mock(engine).watch_for_termination
engine.execute("alpha")
end

it "should fail if specified and doesnt exist" do
mock(subject).error("No such file: /tmp/env")
subject.execute("alpha", :env => "/tmp/env")
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") }
mock(subject).fork_individual(anything, anything, anything, { "FOO" => "qoo" })
subject.execute("bravo")
engine = Foreman::Engine.new("Procfile")
stub(engine).info
mock(engine).watch_for_termination
mock(engine).fork_individual(anything, anything, anything, { "FOO" => "qoo" })
engine.execute("bravo")
end
end
end

0 comments on commit ddcaac8

Please sign in to comment.