Compare commits

...

6 Commits

Author SHA1 Message Date
David Dollar
d199ef2b4d Regenerated gemspec for version 0.7.0 2010-07-19 17:14:04 -07:00
David Dollar
7a1895e435 0.7.0 2010-07-19 17:13:54 -07:00
David Dollar
151ddb45c8 remove screen option, seems too hokey 2010-07-19 17:11:42 -07:00
David Dollar
51513dcb6d pedantry 2010-07-19 17:08:48 -07:00
David Dollar
0b6fdad3a2 allow concurrency to be used in development mode 2010-07-19 17:08:42 -07:00
David Dollar
096f532624 fix failing spec 2010-07-19 15:50:19 -07:00
9 changed files with 63 additions and 49 deletions

View File

@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{foreman}
s.version = "0.6.0"
s.version = "0.7.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["David Dollar"]
s.date = %q{2010-07-06}
s.date = %q{2010-07-19}
s.default_executable = %q{foreman}
s.description = %q{Process manager for applications with multiple components}
s.email = %q{ddollar@gmail.com}
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
"lib/foreman/export/inittab.rb",
"lib/foreman/export/upstart.rb",
"lib/foreman/process.rb",
"lib/foreman/utils.rb",
"spec/foreman/cli_spec.rb",
"spec/foreman/engine_spec.rb",
"spec/foreman/export/upstart_spec.rb",
@@ -39,7 +40,6 @@ Gem::Specification.new do |s|
"spec/foreman_spec.rb",
"spec/spec_helper.rb"
]
s.has_rdoc = false
s.homepage = %q{http://github.com/ddollar/foreman}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]

View File

@@ -1,6 +1,6 @@
module Foreman
VERSION = "0.6.0"
VERSION = "0.7.0"
class AppDoesNotExist < Exception; end

View File

@@ -9,17 +9,16 @@ class Foreman::CLI < Thor
desc "start [PROCESS]", "Start the application, or a specific process"
method_option :screen, :type => :boolean, :aliases => "-s"
method_option :concurrency, :type => :string, :aliases => "-c",
:banner => '"alpha=5,bar=3"'
def start(process=nil)
check_procfile!
if process
engine.execute(process)
elsif options[:screen]
engine.screen
engine.execute(process, options)
else
engine.start
engine.start(options)
end
end

View File

@@ -1,5 +1,6 @@
require "foreman"
require "foreman/process"
require "foreman/utils"
require "pty"
require "tempfile"
require "term/ansicolor"
@@ -12,28 +13,42 @@ class Foreman::Engine
extend Term::ANSIColor
COLORS = [ cyan, yellow, green, magenta, on_blue ]
COLORS = [ cyan, yellow, green, magenta, red ]
def initialize(procfile)
@procfile = read_procfile(procfile)
@directory = File.expand_path(File.dirname(procfile))
end
def processes
def processes(concurrency=nil)
@processes ||= begin
concurrency = Foreman::Utils.parse_concurrency(concurrency)
procfile.split("\n").inject({}) do |hash, line|
next if line.strip == ""
process = Foreman::Process.new(*line.split(" ", 2))
process.color = next_color
hash.update(process.name => process)
name, command = line.split(" ", 2)
if concurrency[name] > 1 then
1.upto(concurrency[name]) do |num|
process = Foreman::Process.new("#{name}.#{num}", command)
process.color = next_color
hash[process.name] = process
end
else
process = Foreman::Process.new(name, command)
process.color = next_color
hash[process.name] = process
end
hash
end
end
end
def start
def start(options={})
proctitle "ruby: foreman master"
processes.each do |name, process|
processes(options[:concurrency]).each do |name, process|
fork process
end
@@ -43,21 +58,17 @@ class Foreman::Engine
watch_for_termination
end
def screen
tempfile = Tempfile.new("foreman")
tempfile.puts "sessionname foreman"
processes.each do |name, process|
tempfile.puts "screen -t #{name} #{process.command}"
def execute(name, options={})
processes(options[:concurrency]).values.select do |process|
process.name =~ /\A#{name}\.?\d*\Z/
end.each do |process|
fork process
end
tempfile.close
system "screen -c #{tempfile.path}"
trap("TERM") { kill_and_exit("TERM") }
trap("INT") { kill_and_exit("INT") }
tempfile.delete
end
def execute(name)
run(processes[name], false)
watch_for_termination
end
private ######################################################################

View File

@@ -1,4 +1,5 @@
require "foreman/export"
require "foreman/utils"
class Foreman::Export::Base
@@ -26,16 +27,6 @@ private ######################################################################
File.read(File.expand_path("../../../../export/#{name}", __FILE__))
end
def parse_concurrency(concurrency)
@concurrency ||= begin
pairs = concurrency.to_s.gsub(/\s/, "").split(",")
pairs.inject(Hash.new(1)) do |hash, pair|
process, amount = pair.split("=")
hash.update(process => amount.to_i)
end
end
end
def port_for(base_port, app, num)
base_port ||= 5000
offset = engine.processes.keys.sort.index(app) * 100

15
lib/foreman/utils.rb Normal file
View File

@@ -0,0 +1,15 @@
require "foreman"
class Foreman::Utils
def self.parse_concurrency(concurrency)
@concurrency ||= begin
pairs = concurrency.to_s.gsub(/\s/, "").split(",")
pairs.inject(Hash.new(1)) do |hash, pair|
process, amount = pair.split("=")
hash.update(process => amount.to_i)
end
end
end
end

View File

@@ -25,9 +25,9 @@ application type.
The following options control how the application is run:
* `-s`, `--screen`:
Run the application as a series of screen windows rather than interleaved
in stdout.
* `-c`, `--concurrency`:
Specify the number of each process type to run. The value passed in
should be in the format `process=num,process=num`
## EXPORTING

View File

@@ -19,7 +19,7 @@ describe "Foreman::CLI" do
it "runs successfully" do
dont_allow(subject).error
mock.instance_of(Foreman::Engine).start
mock.instance_of(Foreman::Engine).start({})
subject.start
end
end
@@ -51,10 +51,7 @@ describe "Foreman::CLI" do
it "runs successfully" do
dont_allow(subject).error
mock.instance_of(Foreman::Export::Upstart).export("/tmp/foo", {
:concurrency => nil,
:name => nil
})
mock.instance_of(Foreman::Export::Upstart).export("/tmp/foo", {})
subject.export("upstart", "/tmp/foo")
end
end

View File

@@ -10,7 +10,7 @@ describe "Foreman::Engine" do
lambda { subject }.should raise_error
end
end
describe "with a Procfile" do
it "reads the processes" do
write_procfile
@@ -19,7 +19,7 @@ describe "Foreman::Engine" do
end
end
end
describe "start" do
it "forks the processes" do
write_procfile
@@ -33,7 +33,8 @@ describe "Foreman::Engine" do
describe "execute" do
it "runs the processes" do
write_procfile
mock(subject).run(subject.processes["alpha"], false)
mock(subject).fork(subject.processes["alpha"])
mock(subject).watch_for_termination
subject.execute("alpha")
end
end