Permalink
Browse files

Spawn clusters of daemons

  • Loading branch information...
1 parent d962ca8 commit df36889b4c5a1dc023244862c705fc0eb7815162 Michael Sofaer and Raphael Sofaer committed Nov 19, 2010
Showing with 29 additions and 9 deletions.
  1. +13 −2 lib/hellspawn.rb
  2. +16 −7 test/hellspawn_test.rb
View
@@ -12,7 +12,18 @@ def march!
end
def summon(options)
- self << Daemon.new(options)
+ by_flag = options.delete :by_flag
+ if by_flag
+ by_flag[1].each do |value|
+ new_options = options.dup
+ new_options[:flags] = options[:flags].dup || {}
+ new_options[:flags][by_flag[0]] = value
+ new_options[:name] = options[:name] + "_" + value.to_s
+ self << Daemon.new(new_options)
+ end
+ else
+ self << Daemon.new(options)
+ end
end
class Daemon < Hash
@@ -33,7 +44,7 @@ def run_script
"exec #{self[:executable]} " + flag_snippet
end
def flag_snippet
- self[:flags].map{|k,v| k + " " + v}.join(" ")
+ self[:flags].map{|k,v| k + " " + v.to_s}.join(" ")
end
def run_prep
"exec 2&>1"
View
@@ -9,10 +9,11 @@ def setup
"-c" => "/usr/local/app/my_app",
},
}
- @legion = Hellspawn.legion(:base => "/tmp/test_services", :name => "test_legion")
+ @base = "/tmp/test_services"
+ @legion = Hellspawn.legion(:base => @base, :name => "test_legion")
end
def teardown
- FileUtils.rm_rf("/tmp/test_services")
+ FileUtils.rm_rf @base
end
def test_legion
assert {@legion.size == 0}
@@ -24,25 +25,33 @@ def test_summon
def test_march
@legion.summon @thin
@legion.march!
- run_script = File.read("/tmp/test_services/thin/run")
+ run_script = File.read("#{@base}/thin/run")
assert { run_script.match /exec \/usr\/local\/bin\/thin/ }
end
def test_flags
@legion.summon @thin
@legion.march!
- run_script_lines = File.read("/tmp/test_services/thin/run").split("\n")
+ run_script_lines = File.read("#{@base}/thin/run").split("\n")
assert { run_script_lines.include?("exec /usr/local/bin/thin -c /usr/local/app/my_app -e production") }
end
def test_stderr
@legion.summon @thin
@legion.march!
- run_script = File.read("/tmp/test_services/thin/run")
+ run_script = File.read("#{@base}/thin/run")
assert { run_script.split("\n").first == "exec 2&>1" }
end
def test_removal
@legion.summon @thin
@legion.march!
- Hellspawn.legion(:base => "/tmp/test_services", :name => "empty_legion").march!
- assert {Dir.glob("/tmp/test_services/*") == [] }
+ Hellspawn.legion(:base => @base, :name => "empty_legion").march!
+ assert {Dir.glob("#{@base}/*") == [] }
+ end
+ def test_squad_by_flag
+ @legion.summon @thin.merge(:by_flag => ["-p", [8004, 8005, 8006, 8007]])
+ @legion.march!
+ assert {File.read("#{@base}/thin_8004/run").match /thin .* -p 8004/ }
+ assert {File.read("#{@base}/thin_8005/run").match /thin .* -p 8005/ }
+ assert {File.read("#{@base}/thin_8006/run").match /thin .* -p 8006/ }
+ assert {File.read("#{@base}/thin_8007/run").match /thin .* -p 8007/ }
end
end

0 comments on commit df36889

Please sign in to comment.