Commit d9c0c8c2 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'enable-idempotent-ci-minutes-update' into 'master'

Enable CI minutes update to run idempotently

See merge request gitlab-org/gitlab!69994
parents d0f8668d 8879b166
......@@ -14,14 +14,14 @@ module Ci
return unless consumption > 0
update_minutes(consumption)
update_minutes(consumption, build)
compare_with_live_consumption(build, consumption)
end
private
def update_minutes(consumption)
::Ci::Minutes::UpdateProjectAndNamespaceUsageWorker.perform_async(consumption, project.id, namespace.id)
def update_minutes(consumption, build)
::Ci::Minutes::UpdateProjectAndNamespaceUsageWorker.perform_async(consumption, project.id, namespace.id, build.id)
end
def compare_with_live_consumption(build, consumption)
......
......@@ -22,7 +22,7 @@ module Ci
# TODO: fix this condition after the next deployment when `build_id`
# is made a mandatory argument.
# https://gitlab.com/gitlab-org/gitlab/-/issues/331785
if @build_id
if @build_id && idempotent_consumption_enabled?
ensure_idempotency { track_usage_of_monthly_minutes(consumption) }
else
track_usage_of_monthly_minutes(consumption)
......@@ -103,7 +103,10 @@ module Ci
# Ensure we only add the CI minutes consumption once for the given build
# even if the worker is retried.
def ensure_idempotency
return if already_completed?
if already_completed?
::Gitlab::AppJsonLogger.info(event: 'ci_minutes_consumption_already_updated', build_id: @build_id)
return
end
yield
......@@ -121,6 +124,15 @@ module Ci
redis.exists(idempotency_cache_key)
end
end
# When running this worker the project might have been deleted.
# In this case we consider the feature flag disabled for backward
# compatibility.
def idempotent_consumption_enabled?
return false unless @project
Feature.enabled?(:idempotent_ci_minutes_consumption, @project, default_enabled: :yaml)
end
end
end
end
---
name: idempotent_ci_minutes_consumption
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/69994
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/340517
milestone: '14.4'
type: development
group: group::pipeline execution
default_enabled: false
......@@ -170,6 +170,23 @@ RSpec.describe Ci::Minutes::UpdateProjectAndNamespaceUsageService do
it_behaves_like 'does not update monthly consumption'
it_behaves_like 'updates legacy consumption' # not idempotent / to be removed
it 'logs the event' do
expect(::Gitlab::AppJsonLogger)
.to receive(:info)
.with(event: 'ci_minutes_consumption_already_updated', build_id: build.id)
.and_call_original
subject
end
context 'when feature flag idempotent_ci_minutes_consumption is disabled' do
before do
stub_feature_flags(idempotent_ci_minutes_consumption: false)
end
it_behaves_like 'updates monthly consumption'
end
context 'when build_id is not provided as parameter' do
let(:service) { described_class.new(project.id, namespace.id) }
......
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