Skip to content

Commit

Permalink
Merge pull request #114 from nisanth074/autostart_option
Browse files Browse the repository at this point in the history
Support for processes that are inactive by default
  • Loading branch information
Hemant Kumar committed Nov 16, 2014
2 parents 69100f2 + 5707ca0 commit c3212c5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/invoker/commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def start_manager
unix_server_thread = Thread.new { Invoker::IPC::Server.new }
@thread_group.add(unix_server_thread)
process_manager.run_power_server
Invoker.config.processes.each { |process_info| process_manager.start_process(process_info) }
Invoker.config.autorunnable_processes.each { |process_info| process_manager.start_process(process_info) }
at_exit { process_manager.kill_workers }
start_event_loop
end
Expand Down
32 changes: 17 additions & 15 deletions lib/invoker/parsers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def https_port
power_config && power_config.https_port
end

def autorunnable_processes
processes.reject(&:disable_autorun)
end

def process(label)
processes.detect { |pconfig| pconfig.label == label }
end
Expand Down Expand Up @@ -64,10 +68,13 @@ def process_procfile
def process_command_from_section(section)
if supports_subdomain?(section)
port = pick_port(section)
make_option_for_subdomain(section, port)
else
make_option(section)
if port
command = replace_port_in_command(section['command'], port)
section['port'], section['command'] = port, command
end
end

make_pconfig(section)
end

def pick_port(section)
Expand All @@ -80,21 +87,16 @@ def pick_port(section)
end
end

def make_option_for_subdomain(section, port)
OpenStruct.new(
port: port,
label: section["label"] || section.key,
dir: expand_directory(section["directory"]),
cmd: replace_port_in_command(section["command"], port)
)
end

def make_option(section)
OpenStruct.new(
def make_pconfig(section)
pconfig = {
label: section["label"] || section.key,
dir: expand_directory(section["directory"]),
cmd: section["command"]
)
}
pconfig['port'] = section['port'] if section['port']
pconfig['disable_autorun'] = section['disable_autorun'] if section['disable_autorun']

OpenStruct.new(pconfig)
end

def supports_subdomain?(section)
Expand Down
38 changes: 32 additions & 6 deletions spec/invoker/commander_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
describe "#start_process" do
describe "when not daemonized" do
before do
invoker_config.stubs(:processes).returns(
[OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'])]
)
processes = [OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'])]
invoker_config.stubs(:processes).returns(processes)
invoker_config.stubs(:autorunnable_processes).returns(processes)
@commander = Invoker::Commander.new
Invoker.commander = @commander
end
Expand Down Expand Up @@ -51,9 +51,9 @@

describe "when daemonized" do
before do
invoker_config.stubs(:processes).returns(
[OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'])]
)
processes = [OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'])]
invoker_config.stubs(:processes).returns(processes)
invoker_config.stubs(:autorunnable_processes).returns(processes)
@commander = Invoker::Commander.new
Invoker.commander = @commander
Invoker.daemonize = true
Expand Down Expand Up @@ -87,6 +87,32 @@
end
end

describe 'disable_autorun option' do
context 'autorun is disabled for a process' do
before do
@processes = [
OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME']),
OpenStruct.new(:label => "panda", :cmd => "panda_command", :dir => ENV['HOME'], :disable_autorun => true)
]
invoker_config.stubs(:processes).returns(@processes)
invoker_config.stubs(:autorunnable_processes).returns([@processes.first])

@commander = Invoker::Commander.new
end

it "doesn't run process" do
@commander.expects(:install_interrupt_handler)
@commander.process_manager.expects(:run_power_server)
@commander.expects(:at_exit)
@commander.expects(:start_event_loop)

@commander.process_manager.expects(:start_process).with(@processes[0])
@commander.process_manager.expects(:start_process).with(@processes[1]).never
@commander.start_manager
end
end
end

describe "#runnables" do
before do
@commander = Invoker::Commander.new
Expand Down
33 changes: 33 additions & 0 deletions spec/invoker/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,37 @@
end
end

describe "#autorunnable_processes" do
it "returns a list of processes that can be autorun" do
begin
file = Tempfile.new(["config", ".ini"])
config_data =<<-EOD
[postgres]
command = postgres -D /usr/local/var/postgres
[redis]
command = redis-server /usr/local/etc/redis.conf
disable_autorun = true
[memcached]
command = /usr/local/opt/memcached/bin/memcached
disable_autorun = false
[panda-api]
command = bundle exec rails s
disable_autorun = true
[panda-auth]
command = bundle exec rails s -p $PORT
EOD
file.write(config_data)
file.close

config = Invoker::Parsers::Config.new(file.path, 9000)
expect(config.autorunnable_processes.map(&:label)).to eq(['postgres', 'memcached', 'panda-auth'])
ensure
file.unlink()
end
end
end
end

0 comments on commit c3212c5

Please sign in to comment.