Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make spawn an option again #15379

Merged
merged 2 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/embedded_ansible_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def start_runner
Thread.exit
end
end
nil # return no pid
end

def kill
Expand Down
21 changes: 21 additions & 0 deletions app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ def self.close_pg_sockets_inherited_from_parent
end

def start_runner
ENV['MIQ_SPAWN_WORKERS'] ? start_runner_via_spawn : start_runner_via_fork
end

def start_runner_via_fork
self.class.before_fork
pid = fork(:cow_friendly => true) do
self.class.after_fork
Expand All @@ -346,6 +350,23 @@ def start_runner
pid
end

def self.build_command_line(guid)
command_line = "#{Gem.ruby} #{runner_script} --heartbeat --guid=#{guid} #{name}"
ENV['APPLIANCE'] ? "nice #{nice_increment} #{command_line}" : command_line
end

def self.runner_script
script = ManageIQ.root.join("lib/workers/bin/run_single_worker.rb")
raise "script not found: #{script}" unless File.exist?(script)
script
end

def start_runner_via_spawn
pid = Kernel.spawn(self.class.build_command_line(guid), :out => "/dev/null", :err => [Rails.root.join("log", "evm.log"), "a"])
Process.detach(pid)
pid
end

def start
self.pid = start_runner
save
Expand Down
1 change: 1 addition & 0 deletions app/models/miq_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TemporaryFailure < RuntimeError

INTERRUPT_SIGNALS = ["SIGINT", "SIGTERM"]

# DELETE ME
OPTIONS_PARSER_SETTINGS = [
[:guid, 'EVM Worker GUID', String],
]
Expand Down
11 changes: 10 additions & 1 deletion lib/workers/bin/run_single_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
options[:dry_run] = val
end

opts.on("-g=GUID", "--guid=GUID", "Find an existing worker record instead of creating") do |val|
options[:guid] = val
end

opts.on("-h", "--help", "Displays this help") do
puts opts
exit
Expand Down Expand Up @@ -52,7 +56,12 @@
worker_class = worker_class.constantize
worker_class.before_fork
unless options[:dry_run]
worker = worker_class.create_worker_record
worker = if options[:guid]
worker_class.find_by!(:guid => options[:guid])
else
worker_class.create_worker_record
end

begin
worker.class::Runner.start_worker(:guid => worker.guid)
ensure
Expand Down
26 changes: 26 additions & 0 deletions spec/models/miq_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ def all_workers
expect(result.command_line).to eq "renice -n 5 -p 123"
end

context ".build_command_line" do
before do
allow(MiqGenericWorker).to receive(:nice_increment).and_return("+10")
end

it "with ENV['APPLIANCE']" do
begin
old_env = ENV.delete('DATABASE_URL')
ENV['APPLIANCE'] = 'true'
w = FactoryGirl.build(:miq_generic_worker)
cmd = w.class.build_command_line(123)
expect(cmd).to start_with("nice +10")
expect(cmd).to end_with("MiqGenericWorker")
ensure
# ENV['x'] = nil deletes the key because ENV accepts only string values
ENV['APPLIANCE'] = old_env
end
end

it "without ENV['APPLIANCE']" do
w = FactoryGirl.build(:miq_generic_worker)
cmd = w.class.build_command_line(123)
expect(cmd).to_not start_with("nice +10")
end
end

context ".has_required_role?" do
def check_has_required_role(worker_role_names, expected_result)
allow(described_class).to receive(:required_roles).and_return(worker_role_names)
Expand Down