Skip to content

Commit

Permalink
Exit with proper exit code.
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenk committed Jun 6, 2016
1 parent 601ff7e commit 2641319
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions bin/sidekiq_wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ def run_process(options, arguments, name)
'--logfile', "#{File.join(options[:workdir], name)}.log")
end

def wait_for_process(wait_threads, name, pid, pids)
def wait_for_process(statuses, wait_threads, name, pid, pids)
wait_threads << Thread.new do
_pid, status = Process.wait2(pid)
statuses << status
stop_all_processes(name, pid, pids)
end
end

def send_signal(signal, pid)
Process.kill(signal, pid)
rescue Errno::ESRCH
$stderr.puts "Process #{pid} was already terminated."
$stderr.puts "Could not send signal: Process #{pid} was already terminated."
end

$stopped_all = false
Expand All @@ -69,6 +70,14 @@ def stop_all_processes(name_dead, pid_dead, pids)
$stopped_all = true
end

def exit_with_status(statuses)
if statuses.all? { |status| status.exited? && status.success? }
exit 0
else
exit 1
end
end

def hets_queue_thread_count
# One thread per configured hets instance, minus one for the sequential queue.
[1, Settings.hets.instance_urls.size - 1].max
Expand Down Expand Up @@ -104,5 +113,10 @@ processes.each do |name, arguments|
end

wait_threads = []
pids.each { |name, pid| wait_for_process(wait_threads, name, pid, pids) }
statuses = []
pids.each do |name, pid|
wait_for_process(statuses, wait_threads, name, pid, pids)
end
wait_threads.each(&:join)

exit_with_status(statuses)

0 comments on commit 2641319

Please sign in to comment.