Commit df4ebb50 authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'ci-worker-make-build-id-required-param' into 'master'

Make build_id param as required when updating CI minutes async

See merge request gitlab-org/gitlab!71297
parents b05eb6d0 fd716d44
......@@ -9,7 +9,7 @@ module Ci
urgency :low
data_consistency :always # primarily performs writes
def perform(consumption, project_id, namespace_id, build_id = nil)
def perform(consumption, project_id, namespace_id, build_id)
::Ci::Minutes::UpdateProjectAndNamespaceUsageService
.new(project_id, namespace_id, build_id)
.execute(consumption)
......
......@@ -11,8 +11,10 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageWorker do
let(:consumption_seconds) { consumption * 60 }
let(:worker) { described_class.new }
describe '#perform' do
shared_examples 'executes the update' do
describe '#perform', :clean_gitlab_redis_shared_state do
subject { perform_multiple([consumption, project.id, namespace.id, build.id]) }
context 'behaves idempotently for monthly usage update' do
it 'executes UpdateProjectAndNamespaceUsageService' do
service_instance = double
expect(::Ci::Minutes::UpdateProjectAndNamespaceUsageService).to receive(:new).at_least(:once).and_return(service_instance)
......@@ -29,57 +31,13 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageWorker do
end
end
shared_examples 'skips the update' do
it 'does not execute UpdateProjectAndNamespaceUsageService' do
expect(::Ci::Minutes::UpdateProjectAndNamespaceUsageService).not_to receive(:new)
subject
end
end
context 'when build_id is not passed as parameter (param backward compatibility)' do
subject { worker.perform(consumption, project.id, namespace.id) }
it_behaves_like 'executes the update'
it 'updates legacy statistics' do
subject
expect(project.statistics.reload.shared_runners_seconds).to eq(consumption_seconds)
expect(namespace.reload.namespace_statistics.shared_runners_seconds).to eq(consumption_seconds)
end
context 'does not behave idempotently' do
subject { perform_multiple([consumption, project.id, namespace.id], worker: worker) }
it 'does not behave idempotently for legacy statistics update' do
expect(::Ci::Minutes::UpdateProjectAndNamespaceUsageService).to receive(:new).twice.and_call_original
it 'executes the operation multiple times' do
expect(::Ci::Minutes::UpdateProjectAndNamespaceUsageService).to receive(:new).twice.and_call_original
subject
subject
expect(project.statistics.reload.shared_runners_seconds).to eq(2 * consumption_seconds)
expect(namespace.reload.namespace_statistics.shared_runners_seconds).to eq(2 * consumption_seconds)
expect(Ci::Minutes::NamespaceMonthlyUsage.find_by(namespace: namespace).amount_used).to eq(2 * consumption)
expect(Ci::Minutes::ProjectMonthlyUsage.find_by(project: project).amount_used).to eq(2 * consumption)
end
end
end
context 'when build_id is passed as parameter', :clean_gitlab_redis_shared_state do
subject { perform_multiple([consumption, project.id, namespace.id, build.id]) }
context 'behaves idempotently for monthly usage update' do
it_behaves_like 'executes the update'
end
it 'does not behave idempotently for legacy statistics update' do
expect(::Ci::Minutes::UpdateProjectAndNamespaceUsageService).to receive(:new).twice.and_call_original
subject
expect(project.statistics.reload.shared_runners_seconds).to eq(2 * consumption_seconds)
expect(namespace.reload.namespace_statistics.shared_runners_seconds).to eq(2 * consumption_seconds)
end
expect(project.statistics.reload.shared_runners_seconds).to eq(2 * consumption_seconds)
expect(namespace.reload.namespace_statistics.shared_runners_seconds).to eq(2 * consumption_seconds)
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