Commit c8ba5d35 authored by Jason Goodman's avatar Jason Goodman Committed by Igor Drozdov

Send storage usage notification email in RootStatisticsWorker

Send if storage is low after refreshing root storage statistics
Hide behind a default disabled feature flag
parent d008ab74
......@@ -20,8 +20,17 @@ module Namespaces
Namespaces::StatisticsRefresherService.new.execute(namespace)
namespace.aggregation_schedule.destroy
notify_storage_usage(namespace)
rescue ::Namespaces::StatisticsRefresherService::RefresherError, ActiveRecord::RecordNotFound => ex
Gitlab::ErrorTracking.track_exception(ex, namespace_id: namespace_id, namespace: namespace&.full_path)
end
private
def notify_storage_usage(namespace)
end
end
end
Namespaces::RootStatisticsWorker.prepend_mod_with('Namespaces::RootStatisticsWorker')
# frozen_string_literal: true
module Namespaces
module Storage
class EnforcementCheckService
def self.enforce_limit?(namespace)
::Gitlab::CurrentSettings.enforce_namespace_storage_limit? &&
::Feature.enabled?(:namespace_storage_limit, namespace)
end
end
end
end
# frozen_string_literal: true
module EE
module Namespaces
module RootStatisticsWorker
extend ::Gitlab::Utils::Override
private
override :notify_storage_usage
def notify_storage_usage(namespace)
return unless ::Namespaces::Storage::EnforcementCheckService.enforce_limit?(namespace)
mailer = ::Emails::NamespaceStorageUsageMailer
::Namespaces::Storage::EmailNotificationService.new(mailer).execute(namespace)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::Namespaces::Storage::EnforcementCheckService do
let_it_be(:group) { create(:group) }
describe '.enforce_limit?' do
before do
stub_feature_flags(namespace_storage_limit: group)
stub_application_setting(enforce_namespace_storage_limit: true)
end
it 'returns true when namespace storage limits are enforced for the namespace' do
expect(described_class.enforce_limit?(group)).to eq(true)
end
it 'returns false when the feature flag is disabled' do
stub_feature_flags(namespace_storage_limit: false)
expect(described_class.enforce_limit?(group)).to eq(false)
end
it 'returns false when the application setting is disabled' do
stub_application_setting(enforce_namespace_storage_limit: false)
expect(described_class.enforce_limit?(group)).to eq(false)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespaces::RootStatisticsWorker, '#perform', :saas do
let_it_be(:group, refind: true) { create(:group_with_plan, :with_aggregation_schedule, plan: :ultimate_plan) }
let_it_be(:project, refind: true) { create(:project, namespace: group) }
let_it_be(:owner) { create(:user) }
let(:mailer) { class_double(::Emails::NamespaceStorageUsageMailer).as_stubbed_const }
subject(:worker) { described_class.new }
before_all do
group.add_owner(owner)
end
context 'when storage limits are enforced for the namespace' do
before do
allow(::Namespaces::Storage::EnforcementCheckService).to receive(:enforce_limit?).with(group).and_return(true)
end
context 'when the namespace is running low on storage' do
it 'sends a notification email' do
set_storage_size_limit(group, megabytes: 10)
project.statistics.update!(repository_size: 9.megabytes)
expect(mailer).to receive(:notify_limit_warning).with(group, [owner.email], 10)
worker.perform(group.id)
end
end
context 'without a namespace' do
it 'does not send an email notification' do
expect(mailer).not_to receive(:notify_limit_warning)
expect(mailer).not_to receive(:notify_out_of_storage)
worker.perform(non_existing_record_id)
end
end
context 'without an aggregation scheduled' do
before do
group.aggregation_schedule.destroy!
end
it 'does not send an email notification' do
expect(mailer).not_to receive(:notify_limit_warning)
expect(mailer).not_to receive(:notify_out_of_storage)
worker.perform(group.id)
end
end
context 'when something goes wrong when updating' do
before do
allow_next_instance_of(Namespaces::StatisticsRefresherService) do |instance|
allow(instance).to receive(:execute)
.and_raise(Namespaces::StatisticsRefresherService::RefresherError, 'error')
end
end
it 'does not send an email notification' do
expect(mailer).not_to receive(:notify_limit_warning)
expect(mailer).not_to receive(:notify_out_of_storage)
worker.perform(group.id)
end
end
end
context 'when storage limits are not enforced for the namespace' do
before do
allow(::Namespaces::Storage::EnforcementCheckService).to receive(:enforce_limit?).with(group).and_return(false)
end
context 'when the namespace is running low on storage' do
it 'does not send a notification email' do
set_storage_size_limit(group, megabytes: 10)
project.statistics.update!(repository_size: 9.megabytes)
expect(mailer).not_to receive(:notify_limit_warning)
expect(mailer).not_to receive(:notify_out_of_storage)
worker.perform(group.id)
end
end
end
def set_storage_size_limit(group, megabytes:)
group.gitlab_subscription.hosted_plan.actual_limits.update!(storage_size_limit: megabytes)
end
end
......@@ -10,7 +10,7 @@ RSpec.describe Namespaces::RootStatisticsWorker, '#perform' do
context 'with a namespace' do
it 'executes refresher service' do
expect_any_instance_of(Namespaces::StatisticsRefresherService)
.to receive(:execute)
.to receive(:execute).and_call_original
worker.perform(group.id)
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