Commit 354207d1 authored by Alper Akgun's avatar Alper Akgun

Merge branch 'catch-timeout-update-max-seats-worker' into 'master'

Catch Timeouts in UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker

See merge request gitlab-org/gitlab!61741
parents e3be5309 94ff9719
...@@ -21,6 +21,8 @@ class UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker # rubocop:disable Scalab ...@@ -21,6 +21,8 @@ class UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker # rubocop:disable Scalab
subscription.refresh_seat_attributes! subscription.refresh_seat_attributes!
tuples << [subscription.id, subscription.max_seats_used, subscription.seats_in_use, subscription.seats_owed] tuples << [subscription.id, subscription.max_seats_used, subscription.seats_in_use, subscription.seats_owed]
rescue ActiveRecord::QueryCanceled => e
track_error(e, subscription)
end end
if tuples.present? if tuples.present?
...@@ -42,9 +44,9 @@ class UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker # rubocop:disable Scalab ...@@ -42,9 +44,9 @@ class UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker # rubocop:disable Scalab
private private
def track_error(subscription) def track_error(error, subscription)
Gitlab::ErrorTracking.track_exception( Gitlab::ErrorTracking.track_exception(
StandardError.new('Namespace absent'), error,
gitlab_subscription_id: subscription.id, gitlab_subscription_id: subscription.id,
namespace_id: subscription.namespace_id namespace_id: subscription.namespace_id
) )
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do
describe '#perform' do
subject { described_class.new } subject { described_class.new }
let_it_be(:bronze_plan) { create(:bronze_plan) } let_it_be(:bronze_plan) { create(:bronze_plan) }
...@@ -15,14 +16,6 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do ...@@ -15,14 +16,6 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do
before do before do
allow(Gitlab::Database).to receive(:read_only?) { db_is_read_only } allow(Gitlab::Database).to receive(:read_only?) { db_is_read_only }
allow(Gitlab::CurrentSettings).to receive(:should_check_namespace_plan?) { true } allow(Gitlab::CurrentSettings).to receive(:should_check_namespace_plan?) { true }
allow_next_found_instance_of(GitlabSubscription) do |subscription|
allow(subscription).to receive(:refresh_seat_attributes!) do
subscription.max_seats_used = subscription.seats + 3
subscription.seats_in_use = subscription.seats + 2
subscription.seats_owed = subscription.seats + 1
end
end
end end
def perform_and_reload def perform_and_reload
...@@ -66,6 +59,14 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do ...@@ -66,6 +59,14 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do
context 'when the DB is not read-only' do context 'when the DB is not read-only' do
before do before do
gitlab_subscription.update!(subscription_attrs) if subscription_attrs gitlab_subscription.update!(subscription_attrs) if subscription_attrs
allow_next_found_instance_of(GitlabSubscription) do |subscription|
allow(subscription).to receive(:refresh_seat_attributes!) do
subscription.max_seats_used = subscription.seats + 3
subscription.seats_in_use = subscription.seats + 2
subscription.seats_owed = subscription.seats + 1
end
end
end end
context 'with a free plan' do context 'with a free plan' do
...@@ -99,6 +100,43 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do ...@@ -99,6 +100,43 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do
end end
end end
context 'when a statement timeout exception is thrown for a subscription' do
before do
allow_next_found_instance_of(GitlabSubscription) do |subscription|
allow(subscription).to receive(:refresh_seat_attributes!) do
if subscription.id == gitlab_subscription.id
raise ActiveRecord::QueryCanceled, 'statement timeout'
else
subscription.max_seats_used = subscription.seats + 3
subscription.seats_in_use = subscription.seats + 2
subscription.seats_owed = subscription.seats + 1
end
end
end
end
it 'catches and logs the exception' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
an_instance_of(ActiveRecord::QueryCanceled),
{ gitlab_subscription_id: gitlab_subscription.id,
namespace_id: gitlab_subscription.namespace_id })
perform_and_reload
end
it 'successfully updates remaining subscriptions' do
expect do
perform_and_reload
end.to not_change(gitlab_subscription, :max_seats_used).from(0)
.and not_change(gitlab_subscription, :seats_in_use).from(0)
.and not_change(gitlab_subscription, :seats_owed).from(0)
.and change(gitlab_subscription_2, :max_seats_used).from(0).to(14)
.and change(gitlab_subscription_2, :seats_in_use).from(0).to(13)
.and change(gitlab_subscription_2, :seats_owed).from(0).to(12)
end
end
end
describe '.last_enqueue_time' do describe '.last_enqueue_time' do
it 'returns last_enqueue_time from the cron job instance' do it 'returns last_enqueue_time from the cron job instance' do
time = Time.current time = Time.current
......
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