Commit c051dd70 authored by Fabio Pitino's avatar Fabio Pitino

Expire Ci::Minutes::CachedQuota when minutes limits are updated

- When quota is reset by admin
- when monthly limit is updated via API (plan change)
- when additional minutes limit is updated via API (purchases)
parent 3be50b9f
......@@ -10,7 +10,9 @@ class ClearNamespaceSharedRunnersMinutesService < BaseService
NamespaceStatistics.where(namespace: @namespace).update_all(
shared_runners_seconds: 0,
shared_runners_seconds_last_reset: Time.current
)
).tap do
::Gitlab::Ci::Minutes::CachedQuota.new(@namespace).expire!
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
......@@ -29,6 +29,10 @@ module EE
::Ci::Runner.instance_type.each(&:tick_runner_queue)
end
if params[:extra_shared_runners_minutes_limit].present? || params[:shared_runners_minutes_limit].present?
::Gitlab::Ci::Minutes::CachedQuota.new(namespace).expire!
end
namespace.update(update_attrs)
end
end
......
......@@ -16,12 +16,10 @@ module Gitlab
@root_namespace = root_namespace
end
# TODO:
# - when monthly minutes are updated via the API (e.g. plan change)
# - when extra_shared_runners_minutes_limit are updated via the API
# - when minutes consumption is reset via the controller (TS or admin)
def expire!
# todo
::Gitlab::Redis::SharedState.with do |redis|
redis.unlink(cache_key)
end
end
# Reduces the remaining minutes by the consumption argument.
......
......@@ -46,4 +46,22 @@ RSpec.describe Gitlab::Ci::Minutes::CachedQuota do
end
end
end
describe '#expire!' do
subject { cached_quota.expire! }
before do
::Gitlab::Redis::SharedState.with do |redis|
redis.set(cached_quota.cache_key, 80.0, ex: 20)
end
end
it 'expires the key' do
subject
::Gitlab::Redis::SharedState.with do |redis|
expect(redis.exists(cached_quota.cache_key)).to be_falsey
end
end
end
end
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe API::Namespaces do
include AfterNextHelpers
let(:admin) { create(:admin) }
let(:user) { create(:user) }
......@@ -218,6 +220,34 @@ RSpec.describe API::Namespaces do
expect(json_response['additional_purchased_storage_size']).to eq(params[:additional_purchased_storage_size])
expect(json_response['additional_purchased_storage_ends_on']).to eq(params[:additional_purchased_storage_ends_on])
end
it 'expires the CI minutes CachedQuota' do
expect_next(Gitlab::Ci::Minutes::CachedQuota).to receive(:expire!)
put api("/namespaces/#{group1.id}", admin), params: params
end
context 'when request has extra_shared_runners_minutes_limit param' do
before do
params[:extra_shared_runners_minutes_limit] = 1000
params.delete(:shared_runners_minutes_limit)
end
it 'expires the CI minutes CachedQuota' do
expect_next(Gitlab::Ci::Minutes::CachedQuota).to receive(:expire!)
put api("/namespaces/#{group1.id}", admin), params: params
end
end
context 'when neither minutes limit params is provided' do
it 'does not expire the CI minutes CachedQuota' do
params.delete(:shared_runners_minutes_limit)
expect(Gitlab::Ci::Minutes::CachedQuota).not_to receive(:new)
put api("/namespaces/#{group1.id}", admin), params: params
end
end
end
context 'when not authenticated as admin' do
......
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe ClearNamespaceSharedRunnersMinutesService do
include AfterNextHelpers
describe '#execute' do
subject { described_class.new(namespace).execute }
......@@ -24,6 +26,12 @@ RSpec.describe ClearNamespaceSharedRunnersMinutesService do
it 'successfully clears minutes' do
expect(subject).to be_truthy
end
it 'expires the CachedQuota' do
expect_next(Gitlab::Ci::Minutes::CachedQuota).to receive(:expire!)
subject
end
end
context 'when project does not have namespace_statistics' do
......
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