diff options
Diffstat (limited to 'sidekiq_cluster')
-rw-r--r-- | sidekiq_cluster/cli.rb | 19 | ||||
-rw-r--r-- | sidekiq_cluster/sidekiq_cluster.rb | 82 |
2 files changed, 12 insertions, 89 deletions
diff --git a/sidekiq_cluster/cli.rb b/sidekiq_cluster/cli.rb index 55b4521d37d..2dbb1e9c7c7 100644 --- a/sidekiq_cluster/cli.rb +++ b/sidekiq_cluster/cli.rb @@ -11,6 +11,7 @@ require_relative '../lib/gitlab/utils' require_relative '../lib/gitlab/sidekiq_config/cli_methods' require_relative '../lib/gitlab/sidekiq_config/worker_matcher' require_relative '../lib/gitlab/sidekiq_logging/json_formatter' +require_relative '../lib/gitlab/process_management' require_relative 'sidekiq_cluster' module Gitlab @@ -106,7 +107,7 @@ module Gitlab end def write_pid - SidekiqCluster.write_pid(@pid) if @pid + ProcessManagement.write_pid(@pid) if @pid end def soft_timeout_seconds @@ -123,11 +124,11 @@ module Gitlab end def continue_waiting?(deadline) - SidekiqCluster.any_alive?(@processes) && monotonic_time < deadline + ProcessManagement.any_alive?(@processes) && monotonic_time < deadline end def hard_stop_stuck_pids - SidekiqCluster.signal_processes(SidekiqCluster.pids_alive(@processes), "-KILL") + ProcessManagement.signal_processes(ProcessManagement.pids_alive(@processes), "-KILL") end def wait_for_termination @@ -138,14 +139,14 @@ module Gitlab end def trap_signals - SidekiqCluster.trap_terminate do |signal| + ProcessManagement.trap_terminate do |signal| @alive = false - SidekiqCluster.signal_processes(@processes, signal) + ProcessManagement.signal_processes(@processes, signal) wait_for_termination end - SidekiqCluster.trap_forward do |signal| - SidekiqCluster.signal_processes(@processes, signal) + ProcessManagement.trap_forward do |signal| + ProcessManagement.signal_processes(@processes, signal) end end @@ -153,12 +154,12 @@ module Gitlab while @alive sleep(@interval) - unless SidekiqCluster.all_alive?(@processes) + unless ProcessManagement.all_alive?(@processes) # If a child process died we'll just terminate the whole cluster. It's up to # runit and such to then restart the cluster. @logger.info('A worker terminated, shutting down the cluster') - SidekiqCluster.signal_processes(@processes, :TERM) + ProcessManagement.signal_processes(@processes, :TERM) break end end diff --git a/sidekiq_cluster/sidekiq_cluster.rb b/sidekiq_cluster/sidekiq_cluster.rb index 49478ba740d..c3aa9e05a09 100644 --- a/sidekiq_cluster/sidekiq_cluster.rb +++ b/sidekiq_cluster/sidekiq_cluster.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'dependencies' +require_relative '../lib/gitlab/process_management' module Gitlab module SidekiqCluster @@ -17,49 +18,6 @@ module Gitlab # After surpassing the soft timeout. DEFAULT_HARD_TIMEOUT_SECONDS = 5 - # The signals that should terminate both the master and workers. - TERMINATE_SIGNALS = %i(INT TERM).freeze - - # The signals that should simply be forwarded to the workers. - FORWARD_SIGNALS = %i(TTIN USR1 USR2 HUP).freeze - - # Traps the given signals and yields the block whenever these signals are - # received. - # - # The block is passed the name of the signal. - # - # Example: - # - # trap_signals(%i(HUP TERM)) do |signal| - # ... - # end - def self.trap_signals(signals) - signals.each do |signal| - trap(signal) do - yield signal - end - end - end - - def self.trap_terminate(&block) - trap_signals(TERMINATE_SIGNALS, &block) - end - - def self.trap_forward(&block) - trap_signals(FORWARD_SIGNALS, &block) - end - - def self.signal(pid, signal) - Process.kill(signal, pid) - true - rescue Errno::ESRCH - false - end - - def self.signal_processes(pids, signal) - pids.each { |pid| signal(pid, signal) } - end - # Starts Sidekiq workers for the pairs of processes. # # Example: @@ -118,7 +76,7 @@ module Gitlab out: $stdout ) - wait_async(pid) + ProcessManagement.wait_async(pid) pid end @@ -144,41 +102,5 @@ module Gitlab concurrency_from_queues.clamp(min, max) end - - # Waits for the given process to complete using a separate thread. - def self.wait_async(pid) - Thread.new do - Process.wait(pid) rescue Errno::ECHILD - end - end - - # Returns true if all the processes are alive. - def self.all_alive?(pids) - pids.each do |pid| - return false unless process_alive?(pid) - end - - true - end - - def self.any_alive?(pids) - pids_alive(pids).any? - end - - def self.pids_alive(pids) - pids.select { |pid| process_alive?(pid) } - end - - def self.process_alive?(pid) - # Signal 0 tests whether the process exists and we have access to send signals - # but is otherwise a noop (doesn't actually send a signal to the process) - signal(pid, 0) - end - - def self.write_pid(path) - File.open(path, 'w') do |handle| - handle.write(Process.pid.to_s) - end - end end end |