Skip to content

Commit

Permalink
Merge pull request #174 from code-mancers/process-ordering
Browse files Browse the repository at this point in the history
Process ordering
  • Loading branch information
iffyuva committed Oct 16, 2016
2 parents 08c8575 + 44cabce commit beb471d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/invoker/commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ 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.autorunnable_processes.each { |process_info| process_manager.start_process(process_info) }
Invoker.config.autorunnable_processes.each do |process_info|
process_manager.start_process(process_info)
Logger.puts("Starting process - #{process_info.label} waiting for #{process_info.sleep_duration} seconds...")
sleep(process_info.sleep_duration)
end
at_exit { process_manager.kill_workers }
start_event_loop
end
Expand Down
17 changes: 16 additions & 1 deletion lib/invoker/parsers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def tld
end

def autorunnable_processes
processes.reject(&:disable_autorun)
process_to_run = processes.reject(&:disable_autorun)
process_to_run.sort_by { |process| process.index }
end

def process(label)
Expand Down Expand Up @@ -123,6 +124,20 @@ def make_pconfig(section)
}
pconfig['port'] = section['port'] if section['port']
pconfig['disable_autorun'] = section['disable_autorun'] if section['disable_autorun']
pconfig['index'] = section['index'].to_i if section['index']
section_index = pconfig['index'].to_i
if section_index
pconfig['index'] = section_index
else
pconfig['index'] = 0
end

sleep_duration = section['sleep'].to_i
if sleep_duration > 0
pconfig['sleep_duration'] = sleep_duration
else
pconfig['sleep_duration'] = 1
end

OpenStruct.new(pconfig)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/invoker/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ def next
Version.new(next_splits.join('.'))
end
end
VERSION = "1.5.1"
VERSION = "1.5.2"
end
8 changes: 4 additions & 4 deletions spec/invoker/commander_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
describe "#start_process" do
describe "when not daemonized" do
before do
processes = [OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'])]
processes = [OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'], :sleep_duration => 2)]
Invoker.config.stubs(:processes).returns(processes)
Invoker.config.stubs(:autorunnable_processes).returns(processes)
Invoker.stubs(:can_run_balancer?).returns(false)
Expand Down Expand Up @@ -61,7 +61,7 @@

describe "when daemonized" do
before do
processes = [OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'])]
processes = [OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'], :sleep_duration => 2)]
Invoker.config.stubs(:processes).returns(processes)
Invoker.config.stubs(:autorunnable_processes).returns(processes)
Invoker.stubs(:can_run_balancer?).returns(false)
Expand Down Expand Up @@ -102,8 +102,8 @@
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)
OpenStruct.new(:label => "foobar", :cmd => "foobar_command", :dir => ENV['HOME'], :sleep_duration => 2),
OpenStruct.new(:label => "panda", :cmd => "panda_command", :dir => ENV['HOME'], :disable_autorun => true, :sleep_duration => 2)
]
Invoker.config.stubs(:processes).returns(@processes)
Invoker.config.stubs(:autorunnable_processes).returns([@processes.first])
Expand Down
45 changes: 45 additions & 0 deletions spec/invoker/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,47 @@
file.unlink()
end
end

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

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

describe "global config file" do
Expand Down Expand Up @@ -299,6 +340,10 @@ def create_procfile

config = Invoker::Parsers::Config.new(nil, 9000)
expect(config.process("some_process").cmd).to eq("some_command")
processes = config.autorunnable_processes
process_1 = processes[0]
expect(process_1.sleep_duration).to eq(1)
expect(process_1.index).to eq(0)
ensure
File.delete(invoker_ini)
File.delete(procfile)
Expand Down

0 comments on commit beb471d

Please sign in to comment.