Commit 5711eeed authored by Douwe Maan's avatar Douwe Maan

Add `--dryrun` option to `bin/sidekiq-cluster`, and spec to verify it actually runs

parent c5bd57e8
......@@ -62,26 +62,32 @@ module Gitlab
# directory - The directory of the Rails application.
#
# Returns an Array containing the PIDs of the started processes.
def self.start(queues, env, directory = Dir.pwd)
queues.map { |pair| start_sidekiq(pair, env, directory) }
def self.start(queues, env, directory = Dir.pwd, dryrun: false)
queues.map { |pair| start_sidekiq(pair, env, directory, dryrun: dryrun) }
end
# Starts a Sidekiq process that processes _only_ the given queues.
#
# Returns the PID of the started process.
def self.start_sidekiq(queues, env, directory = Dir.pwd)
switches = queues.map { |q| "-q#{q},1" }
def self.start_sidekiq(queues, env, directory = Dir.pwd, dryrun: false)
cmd = %w[bundle exec sidekiq]
cmd << "-c #{queues.length + 1}"
cmd << "-e#{env}"
cmd << "-gqueues: #{queues.join(', ')}"
cmd << "-r#{directory}"
queues.each do |q|
cmd << "-q#{q},1"
end
if dryrun
puts "Sidekiq command: #{cmd}" # rubocop:disable Rails/Output
return
end
pid = Process.spawn(
{ 'ENABLE_SIDEKIQ_CLUSTER' => '1' },
'bundle',
'exec',
'sidekiq',
"-c #{queues.length + 1}",
"-e#{env}",
"-gqueues: #{queues.join(', ')}",
"-r#{directory}",
*switches,
*cmd,
err: $stderr,
out: $stdout
)
......
......@@ -15,6 +15,7 @@ module Gitlab
@processes = []
@logger = Logger.new(log_output)
@rails_path = Dir.pwd
@dryrun = false
# Use a log format similar to Sidekiq to make parsing/grepping easier.
@logger.formatter = proc do |level, date, program, message|
......@@ -39,7 +40,9 @@ module Gitlab
@logger.info("Starting cluster with #{queue_groups.length} processes")
@processes = SidekiqCluster.start(queue_groups, @environment, @rails_path)
@processes = SidekiqCluster.start(queue_groups, @environment, @rails_path, dryrun: @dryrun)
return if @dryrun
write_pid
trap_signals
......@@ -105,6 +108,10 @@ module Gitlab
opt.on('-i', '--interval INT', 'The number of seconds to wait between worker checks') do |int|
@interval = int.to_i
end
opt.on('-d', '--dryrun', 'Print commands that would be run without this flag, and quit') do |int|
@dryrun = true
end
end
end
end
......
require 'spec_helper'
describe 'bin/sidekiq-cluster' do
it 'runs successfully', :aggregate_failures do
cmd = %w[bin/sidekiq-cluster --dryrun --negate cronjob]
output, status = Gitlab::Popen.popen(cmd, Rails.root.to_s)
expect(status).to be(0)
expect(output).to include('"bundle", "exec", "sidekiq"')
expect(output).not_to include('-qcronjob,1')
expect(output).to include('-qdefault,1')
end
end
......@@ -24,7 +24,7 @@ describe Gitlab::SidekiqCluster::CLI do
it 'starts Sidekiq workers for all queues on sidekiq_queues.yml except the ones on argv' do
expect(Gitlab::SidekiqConfig).to receive(:config_queues).and_return(['baz'])
expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['baz']], 'test', Dir.pwd)
.with([['baz']], 'test', Dir.pwd, dryrun: false)
.and_return([])
expect(cli).to receive(:write_pid)
expect(cli).to receive(:trap_signals)
......
......@@ -58,10 +58,10 @@ describe Gitlab::SidekiqCluster do
describe '.start' do
it 'starts Sidekiq with the given queues and environment' do
expect(described_class).to receive(:start_sidekiq)
.ordered.with(%w(foo), :production, 'foo/bar')
.ordered.with(%w(foo), :production, 'foo/bar', dryrun: false)
expect(described_class).to receive(:start_sidekiq)
.ordered.with(%w(bar baz), :production, 'foo/bar')
.ordered.with(%w(bar baz), :production, 'foo/bar', dryrun: false)
described_class.start([%w(foo), %w(bar baz)], :production, 'foo/bar')
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment