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

Revert "Merge pull request #16130 from jrafanie/fork_workers_no_more" #16154

Merged
merged 1 commit into from
Oct 9, 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 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ gem "manageiq-api-client", "~>0.1.0", :require => false
gem "manageiq-network_discovery", "~>0.1.2", :require => false
gem "mime-types", "~>2.6.1", :path => "mime-types-redirector"
gem "more_core_extensions", "~>3.3"
gem "nakayoshi_fork", "~>0.0.3" # provides a more CoW friendly fork (GC a few times before fork)
gem "net-ldap", "~>0.14.0", :require => false
gem "net-ping", "~>1.7.4", :require => false
gem "net-ssh", "=3.2.0", :require => false
Expand Down
45 changes: 44 additions & 1 deletion app/models/miq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,29 @@ def send_message_to_worker_monitor(message, *args)
)
end

def self.before_fork
preload_for_worker_role if respond_to?(:preload_for_worker_role)
end

def self.after_fork
close_pg_sockets_inherited_from_parent
DRb.stop_service
renice(Process.pid)
end

# When we fork, the children inherits the parent's file descriptors
# so we need to close any inherited raw pg sockets in the child.
def self.close_pg_sockets_inherited_from_parent
owner_to_pool = ActiveRecord::Base.connection_handler.instance_variable_get(:@owner_to_pool)
owner_to_pool[Process.ppid].values.compact.each do |pool|
pool.connections.each do |conn|
socket = conn.raw_connection.socket
_log.info("Closing socket: #{socket}")
IO.for_fd(socket).close
end
end
end

# Overriding queue_name as now some queue names can be
# arrays of names for some workers not just a singular name.
# We use JSON.parse as the array of names is stored as a string.
Expand All @@ -316,6 +339,26 @@ def queue_name
end
end

def start_runner
if ENV['MIQ_SPAWN_WORKERS'] || !Process.respond_to?(:fork)
start_runner_via_spawn
else
start_runner_via_fork
end
end

def start_runner_via_fork
self.class.before_fork
pid = fork(:cow_friendly => true) do
self.class.after_fork
self.class::Runner.start_worker(worker_options)
exit!
end

Process.detach(pid)
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
Expand All @@ -327,7 +370,7 @@ def self.runner_script
script
end

def start_runner
def start_runner_via_spawn
pid = Kernel.spawn(self.class.build_command_line(guid), [:out, :err] => [Rails.root.join("log", "evm.log"), "a"])
Process.detach(pid)
pid
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/bin/run_single_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
require File.expand_path("../../../config/environment", __dir__)

worker_class = worker_class.constantize
worker_class.preload_for_worker_role if worker_class.respond_to?(:preload_for_worker_role)
worker_class.before_fork
unless options[:dry_run]
create_options = {:pid => Process.pid}
runner_options = {}
Expand Down