allow concurrency to be used in development mode
This commit is contained in:
1 parent
096f532624
commit
0b6fdad3a2
7 files changed
+61
-24
No files matched your search
+5
-3
@@ -9,17 +9,19 @@ class Foreman::CLI < Thor
|
|||||||
|
|
||||||
desc "start [PROCESS]", "Start the application, or a specific process"
|
desc "start [PROCESS]", "Start the application, or a specific process"
|
||||||
|
|
||||||
method_option :screen, :type => :boolean, :aliases => "-s"
|
method_option :screen, :type => :boolean, :aliases => "-s"
|
||||||
|
method_option :concurrency, :type => :string, :aliases => "-c",
|
||||||
|
:banner => '"alpha=5,bar=3"'
|
||||||
|
|
||||||
def start(process=nil)
|
def start(process=nil)
|
||||||
check_procfile!
|
check_procfile!
|
||||||
|
|
||||||
if process
|
if process
|
||||||
engine.execute(process)
|
engine.execute(process, options)
|
||||||
elsif options[:screen]
|
elsif options[:screen]
|
||||||
engine.screen
|
engine.screen
|
||||||
else
|
else
|
||||||
engine.start
|
engine.start(options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
+33
-9
@@ -1,5 +1,6 @@
|
|||||||
require "foreman"
|
require "foreman"
|
||||||
require "foreman/process"
|
require "foreman/process"
|
||||||
|
require "foreman/utils"
|
||||||
require "pty"
|
require "pty"
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
require "term/ansicolor"
|
require "term/ansicolor"
|
||||||
@@ -12,28 +13,42 @@ class Foreman::Engine
|
|||||||
|
|
||||||
extend Term::ANSIColor
|
extend Term::ANSIColor
|
||||||
|
|
||||||
COLORS = [ cyan, yellow, green, magenta, on_blue ]
|
COLORS = [ cyan, yellow, green, magenta, red ]
|
||||||
|
|
||||||
def initialize(procfile)
|
def initialize(procfile)
|
||||||
@procfile = read_procfile(procfile)
|
@procfile = read_procfile(procfile)
|
||||||
@directory = File.expand_path(File.dirname(procfile))
|
@directory = File.expand_path(File.dirname(procfile))
|
||||||
end
|
end
|
||||||
|
|
||||||
def processes
|
def processes(concurrency=nil)
|
||||||
@processes ||= begin
|
@processes ||= begin
|
||||||
|
concurrency = Foreman::Utils.parse_concurrency(concurrency)
|
||||||
|
|
||||||
procfile.split("\n").inject({}) do |hash, line|
|
procfile.split("\n").inject({}) do |hash, line|
|
||||||
next if line.strip == ""
|
next if line.strip == ""
|
||||||
process = Foreman::Process.new(*line.split(" ", 2))
|
name, command = line.split(" ", 2)
|
||||||
process.color = next_color
|
|
||||||
hash.update(process.name => process)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start(options={})
|
||||||
proctitle "ruby: foreman master"
|
proctitle "ruby: foreman master"
|
||||||
|
|
||||||
processes.each do |name, process|
|
processes(options[:concurrency]).each do |name, process|
|
||||||
fork process
|
fork process
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -56,8 +71,17 @@ class Foreman::Engine
|
|||||||
tempfile.delete
|
tempfile.delete
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(name)
|
def execute(name, options={})
|
||||||
run(processes[name], false)
|
processes(options[:concurrency]).values.select do |process|
|
||||||
|
process.name =~ /\A#{name}\.?\d*\Z/
|
||||||
|
end.each do |process|
|
||||||
|
fork process
|
||||||
|
end
|
||||||
|
|
||||||
|
trap("TERM") { kill_and_exit("TERM") }
|
||||||
|
trap("INT") { kill_and_exit("INT") }
|
||||||
|
|
||||||
|
watch_for_termination
|
||||||
end
|
end
|
||||||
|
|
||||||
private ######################################################################
|
private ######################################################################
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
require "foreman/export"
|
require "foreman/export"
|
||||||
|
require "foreman/utils"
|
||||||
|
|
||||||
class Foreman::Export::Base
|
class Foreman::Export::Base
|
||||||
|
|
||||||
@@ -26,16 +27,6 @@ private ######################################################################
|
|||||||
File.read(File.expand_path("../../../../export/#{name}", __FILE__))
|
File.read(File.expand_path("../../../../export/#{name}", __FILE__))
|
||||||
end
|
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)
|
def port_for(base_port, app, num)
|
||||||
base_port ||= 5000
|
base_port ||= 5000
|
||||||
offset = engine.processes.keys.sort.index(app) * 100
|
offset = engine.processes.keys.sort.index(app) * 100
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -25,6 +25,10 @@ application type.
|
|||||||
|
|
||||||
The following options control how the application is run:
|
The following options control how the application is run:
|
||||||
|
|
||||||
|
* `-c`, `--concurrency`:
|
||||||
|
Specify the number of each process type to run. The value passed in
|
||||||
|
should be in the format `process=num,process=num`
|
||||||
|
|
||||||
* `-s`, `--screen`:
|
* `-s`, `--screen`:
|
||||||
Run the application as a series of screen windows rather than interleaved
|
Run the application as a series of screen windows rather than interleaved
|
||||||
in stdout.
|
in stdout.
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ describe "Foreman::Engine" do
|
|||||||
describe "execute" do
|
describe "execute" do
|
||||||
it "runs the processes" do
|
it "runs the processes" do
|
||||||
write_procfile
|
write_procfile
|
||||||
mock(subject).run(subject.processes["alpha"], false)
|
mock(subject).fork(subject.processes["alpha"])
|
||||||
|
mock(subject).watch_for_termination
|
||||||
subject.execute("alpha")
|
subject.execute("alpha")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in new issue
Block a user