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 ...@@ -62,26 +62,32 @@ module Gitlab
# directory - The directory of the Rails application. # directory - The directory of the Rails application.
# #
# Returns an Array containing the PIDs of the started processes. # Returns an Array containing the PIDs of the started processes.
def self.start(queues, env, directory = Dir.pwd) def self.start(queues, env, directory = Dir.pwd, dryrun: false)
queues.map { |pair| start_sidekiq(pair, env, directory) } queues.map { |pair| start_sidekiq(pair, env, directory, dryrun: dryrun) }
end end
# Starts a Sidekiq process that processes _only_ the given queues. # Starts a Sidekiq process that processes _only_ the given queues.
# #
# Returns the PID of the started process. # Returns the PID of the started process.
def self.start_sidekiq(queues, env, directory = Dir.pwd) def self.start_sidekiq(queues, env, directory = Dir.pwd, dryrun: false)
switches = queues.map { |q| "-q#{q},1" } 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( pid = Process.spawn(
{ 'ENABLE_SIDEKIQ_CLUSTER' => '1' }, { 'ENABLE_SIDEKIQ_CLUSTER' => '1' },
'bundle', *cmd,
'exec',
'sidekiq',
"-c #{queues.length + 1}",
"-e#{env}",
"-gqueues: #{queues.join(', ')}",
"-r#{directory}",
*switches,
err: $stderr, err: $stderr,
out: $stdout out: $stdout
) )
......
...@@ -15,6 +15,7 @@ module Gitlab ...@@ -15,6 +15,7 @@ module Gitlab
@processes = [] @processes = []
@logger = Logger.new(log_output) @logger = Logger.new(log_output)
@rails_path = Dir.pwd @rails_path = Dir.pwd
@dryrun = false
# Use a log format similar to Sidekiq to make parsing/grepping easier. # Use a log format similar to Sidekiq to make parsing/grepping easier.
@logger.formatter = proc do |level, date, program, message| @logger.formatter = proc do |level, date, program, message|
...@@ -39,7 +40,9 @@ module Gitlab ...@@ -39,7 +40,9 @@ module Gitlab
@logger.info("Starting cluster with #{queue_groups.length} processes") @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 write_pid
trap_signals trap_signals
...@@ -105,6 +108,10 @@ module Gitlab ...@@ -105,6 +108,10 @@ module Gitlab
opt.on('-i', '--interval INT', 'The number of seconds to wait between worker checks') do |int| opt.on('-i', '--interval INT', 'The number of seconds to wait between worker checks') do |int|
@interval = int.to_i @interval = int.to_i
end end
opt.on('-d', '--dryrun', 'Print commands that would be run without this flag, and quit') do |int|
@dryrun = true
end
end end
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 ...@@ -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 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::SidekiqConfig).to receive(:config_queues).and_return(['baz'])
expect(Gitlab::SidekiqCluster).to receive(:start) expect(Gitlab::SidekiqCluster).to receive(:start)
.with([['baz']], 'test', Dir.pwd) .with([['baz']], 'test', Dir.pwd, dryrun: false)
.and_return([]) .and_return([])
expect(cli).to receive(:write_pid) expect(cli).to receive(:write_pid)
expect(cli).to receive(:trap_signals) expect(cli).to receive(:trap_signals)
......
...@@ -58,10 +58,10 @@ describe Gitlab::SidekiqCluster do ...@@ -58,10 +58,10 @@ describe Gitlab::SidekiqCluster do
describe '.start' do describe '.start' do
it 'starts Sidekiq with the given queues and environment' do it 'starts Sidekiq with the given queues and environment' do
expect(described_class).to receive(:start_sidekiq) 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) 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') described_class.start([%w(foo), %w(bar baz)], :production, 'foo/bar')
end 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