Commit b40ca15a authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch 'hchouraria-shared-runner-minute-reset-zero-division-error' into 'master'

Fix shared runner minutes reset on self-managed

See merge request gitlab-org/gitlab!62781
parents eb0464ae 972311f9
......@@ -20,7 +20,8 @@ class ClearSharedRunnersMinutesWorker # rubocop:disable Scalability/IdempotentWo
start_id = Namespace.minimum(:id)
last_id = Namespace.maximum(:id)
execution_offset = TIME_SPREAD / ((last_id - start_id) / BATCH_SIZE)
batches = [(last_id - start_id) / BATCH_SIZE, 1].max
execution_offset = (TIME_SPREAD / batches).to_i
(start_id..last_id).step(BATCH_SIZE).with_index do |batch_start_id, batch_index|
batch_end_id = batch_start_id + BATCH_SIZE - 1
......
......@@ -142,19 +142,33 @@ RSpec.describe ClearSharedRunnersMinutesWorker do
[2, 3, 4, 5, 7, 8, 10, 14].each do |id|
create(:namespace, id: id)
end
end
context 'with batch size lower than count of namespaces' do
before do
stub_const("#{described_class}::BATCH_SIZE", 3)
end
it 'runs a worker per batch', :aggregate_failures do
# Spreads evenly accross 8 hours (28,800 seconds)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(0.seconds, 2, 4)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(7200.seconds, 5, 7)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(14400.seconds, 8, 10)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(21600.seconds, 11, 13)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(28800.seconds, 14, 16)
stub_const("#{described_class}::BATCH_SIZE", 3)
subject
end
end
it 'runs a worker per batch', :aggregate_failures do
# Spread evenly accross 8 hours (28,800 seconds)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(0.seconds, 2, 4)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(7200.seconds, 5, 7)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(14400.seconds, 8, 10)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(21600.seconds, 11, 13)
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(28800.seconds, 14, 16)
context 'with batch size higher than count of namespaces' do
# Uses default BATCH_SIZE
it 'runs the worker in a single batch', :aggregate_failures do
# Runs a single batch, immediately
expect(Ci::BatchResetMinutesWorker).to receive(:perform_in).with(0.seconds, 2, 100001)
subject
subject
end
end
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