Move options into class initialisation

This commit is contained in:
Thom May committed 2011-09-01 17:06:37 +01:00
1 parent bda6e30323
commit ddcaac8749
4 files changed
+36 -26

No files matched your search

+3 -3
View File
@@ -18,9 +18,9 @@ class Foreman::CLI < Thor
check_procfile! check_procfile!
if process if process
engine.execute(process, options) engine.execute(process)
else else
engine.start(options) engine.start
end end
end end
@@ -64,7 +64,7 @@ private ######################################################################
end end
def engine def engine
@engine ||= Foreman::Engine.new(procfile) @engine ||= Foreman::Engine.new(procfile, options)
end end
def procfile def procfile
+10 -9
View File
@@ -16,9 +16,10 @@ class Foreman::Engine
COLORS = [ cyan, yellow, green, magenta, red ] COLORS = [ cyan, yellow, green, magenta, red ]
def initialize(procfile) def initialize(procfile, options={})
@procfile = read_procfile(procfile) @procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile)) @directory = File.expand_path(File.dirname(procfile))
@options = options
end end
def processes def processes
@@ -50,13 +51,13 @@ class Foreman::Engine
end end
end end
def start(options={}) def start
environment = read_environment(options[:env]) 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, @options, environment
end end
trap("TERM") { puts "SIGTERM received"; terminate_gracefully } trap("TERM") { puts "SIGTERM received"; terminate_gracefully }
@@ -65,10 +66,10 @@ class Foreman::Engine
watch_for_termination watch_for_termination
end end
def execute(name, options={}) def execute(name)
environment = read_environment(options[:env]) environment = read_environment(@options[:env])
fork processes[name], options, environment fork processes[name], @options, environment
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,10 +86,10 @@ class Foreman::Engine
private ###################################################################### private ######################################################################
def fork(process, options={}, environment={}) 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| 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
end end
+1 -1
View File
@@ -19,7 +19,7 @@ describe "Foreman::CLI" do
it "runs successfully" do it "runs successfully" do
dont_allow(subject).error dont_allow(subject).error
mock.instance_of(Foreman::Engine).start({}) mock.instance_of(Foreman::Engine).start
subject.start subject.start
end end
end end
+22 -13
View File
@@ -2,7 +2,7 @@ require "spec_helper"
require "foreman/engine" require "foreman/engine"
describe "Foreman::Engine" do describe "Foreman::Engine" do
subject { Foreman::Engine.new("Procfile") } subject { Foreman::Engine.new("Procfile", {}) }
describe "initialize" do describe "initialize" do
describe "without an existing Procfile" do describe "without an existing Procfile" do
@@ -45,11 +45,12 @@ describe "Foreman::Engine" do
it "handles concurrency" do it "handles concurrency" do
write_procfile write_procfile
mock(subject).fork_individual(subject.processes["alpha"], 1, 5000, {}) engine = Foreman::Engine.new("Procfile",:concurrency => "alpha=2")
mock(subject).fork_individual(subject.processes["alpha"], 2, 5001, {}) mock(engine).fork_individual(engine.processes["alpha"], 1, 5000, {})
mock(subject).fork_individual(subject.processes["bravo"], 1, 5100, {}) mock(engine).fork_individual(engine.processes["alpha"], 2, 5001, {})
mock(subject).watch_for_termination mock(engine).fork_individual(engine.processes["bravo"], 1, 5100, {})
subject.start(:concurrency => "alpha=2") mock(engine).watch_for_termination
engine.start
end end
end end
@@ -63,27 +64,35 @@ describe "Foreman::Engine" do
end end
describe "environment" do describe "environment" do
before(:each) do before(:each) do
write_procfile write_procfile
stub(Process).fork stub(Process).fork
stub(subject).info
mock(subject).watch_for_termination
end end
it "should read if specified" do it "should read if specified" do
File.open("/tmp/env", "w") { |f| f.puts("FOO=baz") } 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 end
it "should fail if specified and doesnt exist" do it "should fail if specified and doesnt exist" do
mock(subject).error("No such file: /tmp/env") mock.instance_of(Foreman::Engine).error("No such file: /tmp/env")
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 end
it "should read .env if none specified" do it "should read .env if none specified" do
File.open(".env", "w") { |f| f.puts("FOO=qoo") } File.open(".env", "w") { |f| f.puts("FOO=qoo") }
mock(subject).fork_individual(anything, anything, anything, { "FOO" => "qoo" }) engine = Foreman::Engine.new("Procfile")
subject.execute("bravo") stub(engine).info
mock(engine).watch_for_termination
mock(engine).fork_individual(anything, anything, anything, { "FOO" => "qoo" })
engine.execute("bravo")
end end
end end
end end