2020-03-11 20:09:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-11-18 01:10:36 -05:00
|
|
|
require_relative '../lib/gitlab/process_management'
|
2020-03-20 17:09:17 -04:00
|
|
|
|
2020-03-11 20:09:34 -04:00
|
|
|
module Gitlab
|
|
|
|
module SidekiqCluster
|
2021-11-08 13:09:52 -05:00
|
|
|
# How long to wait when asking for a clean termination.
|
|
|
|
# It maps the Sidekiq default timeout:
|
|
|
|
# https://github.com/mperham/sidekiq/wiki/Signals#term
|
|
|
|
#
|
|
|
|
# This value is passed to Sidekiq's `-t` if none
|
|
|
|
# is given through arguments.
|
|
|
|
DEFAULT_SOFT_TIMEOUT_SECONDS = 25
|
|
|
|
|
2022-02-22 07:14:09 -05:00
|
|
|
# Additional time granted after surpassing the soft timeout
|
|
|
|
# before we kill the process.
|
|
|
|
TIMEOUT_GRACE_PERIOD_SECONDS = 5
|
2021-11-08 13:09:52 -05:00
|
|
|
|
2020-03-11 20:09:34 -04:00
|
|
|
# Starts Sidekiq workers for the pairs of processes.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# start([ ['foo'], ['bar', 'baz'] ], :production)
|
|
|
|
#
|
|
|
|
# This would start two Sidekiq processes: one processing "foo", and one
|
|
|
|
# processing "bar" and "baz". Each one is placed in its own process group.
|
|
|
|
#
|
|
|
|
# queues - An Array containing Arrays. Each sub Array should specify the
|
|
|
|
# queues to use for a single process.
|
|
|
|
#
|
|
|
|
# directory - The directory of the Rails application.
|
|
|
|
#
|
|
|
|
# Returns an Array containing the PIDs of the started processes.
|
2021-11-08 13:09:52 -05:00
|
|
|
def self.start(queues, env: :development, directory: Dir.pwd, max_concurrency: 50, min_concurrency: 0, timeout: DEFAULT_SOFT_TIMEOUT_SECONDS, dryrun: false)
|
2020-03-11 20:09:34 -04:00
|
|
|
queues.map.with_index do |pair, index|
|
2020-03-25 05:08:11 -04:00
|
|
|
start_sidekiq(pair, env: env,
|
|
|
|
directory: directory,
|
|
|
|
max_concurrency: max_concurrency,
|
|
|
|
min_concurrency: min_concurrency,
|
|
|
|
worker_id: index,
|
|
|
|
timeout: timeout,
|
|
|
|
dryrun: dryrun)
|
2020-03-11 20:09:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Starts a Sidekiq process that processes _only_ the given queues.
|
|
|
|
#
|
|
|
|
# Returns the PID of the started process.
|
2020-03-25 05:08:11 -04:00
|
|
|
def self.start_sidekiq(queues, env:, directory:, max_concurrency:, min_concurrency:, worker_id:, timeout:, dryrun:)
|
2020-03-11 20:09:34 -04:00
|
|
|
counts = count_by_queue(queues)
|
|
|
|
|
|
|
|
cmd = %w[bundle exec sidekiq]
|
2020-03-20 17:09:17 -04:00
|
|
|
cmd << "-c#{self.concurrency(queues, min_concurrency, max_concurrency)}"
|
2020-03-11 20:09:34 -04:00
|
|
|
cmd << "-e#{env}"
|
2020-03-25 05:08:11 -04:00
|
|
|
cmd << "-t#{timeout}"
|
2020-03-20 17:09:17 -04:00
|
|
|
cmd << "-gqueues:#{proc_details(counts)}"
|
2020-03-11 20:09:34 -04:00
|
|
|
cmd << "-r#{directory}"
|
|
|
|
|
|
|
|
counts.each do |queue, count|
|
|
|
|
cmd << "-q#{queue},#{count}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if dryrun
|
2020-03-20 17:09:17 -04:00
|
|
|
puts Shellwords.join(cmd) # rubocop:disable Rails/Output
|
2020-03-11 20:09:34 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-12-08 10:13:43 -05:00
|
|
|
# We need to remove Bundler specific env vars, since otherwise the
|
|
|
|
# child process will think we are passing an alternative Gemfile
|
|
|
|
# and will clear and reset LOAD_PATH.
|
|
|
|
pid = Bundler.with_original_env do
|
|
|
|
Process.spawn(
|
|
|
|
{ 'ENABLE_SIDEKIQ_CLUSTER' => '1',
|
|
|
|
'SIDEKIQ_WORKER_ID' => worker_id.to_s },
|
|
|
|
*cmd,
|
|
|
|
pgroup: true,
|
|
|
|
err: $stderr,
|
|
|
|
out: $stdout
|
|
|
|
)
|
|
|
|
end
|
2020-03-11 20:09:34 -04:00
|
|
|
|
2021-11-18 01:10:36 -05:00
|
|
|
ProcessManagement.wait_async(pid)
|
2020-03-11 20:09:34 -04:00
|
|
|
|
|
|
|
pid
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.count_by_queue(queues)
|
2020-12-16 01:10:11 -05:00
|
|
|
queues.tally
|
2020-03-11 20:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.proc_details(counts)
|
|
|
|
counts.map do |queue, count|
|
|
|
|
if count == 1
|
|
|
|
queue
|
|
|
|
else
|
|
|
|
"#{queue} (#{count})"
|
|
|
|
end
|
2020-03-20 17:09:17 -04:00
|
|
|
end.join(',')
|
2020-03-11 20:09:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.concurrency(queues, min_concurrency, max_concurrency)
|
|
|
|
concurrency_from_queues = queues.length + 1
|
2020-08-05 11:09:59 -04:00
|
|
|
max = max_concurrency > 0 ? max_concurrency : concurrency_from_queues
|
2020-03-11 20:09:34 -04:00
|
|
|
min = [min_concurrency, max].min
|
|
|
|
|
|
|
|
concurrency_from_queues.clamp(min, max)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|