Optimize Redis usage in User::ActivityService
Avoid checking Redis when User#last_activity_on is already up to date.
This commit is contained in:
parent
c8fe0d6a8a
commit
a0e97b1c6a
|
@ -26,12 +26,15 @@ module Users
|
||||||
def record_activity
|
def record_activity
|
||||||
return if Gitlab::Database.read_only?
|
return if Gitlab::Database.read_only?
|
||||||
|
|
||||||
|
today = Date.today
|
||||||
|
|
||||||
|
return if @user.last_activity_on == today
|
||||||
|
|
||||||
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
|
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
|
||||||
timeout: LEASE_TIMEOUT)
|
timeout: LEASE_TIMEOUT)
|
||||||
return unless lease.try_obtain
|
return unless lease.try_obtain
|
||||||
|
|
||||||
@user.update_attribute(:last_activity_on, Date.today)
|
@user.update_attribute(:last_activity_on, today)
|
||||||
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@user.id} (username: #{@user.username})")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Optimize Redis usage in User::ActivityService
|
||||||
|
merge_request: 25005
|
||||||
|
author:
|
||||||
|
type: performance
|
|
@ -26,6 +26,12 @@ describe Users::ActivityService do
|
||||||
.from(last_activity_on)
|
.from(last_activity_on)
|
||||||
.to(Date.today)
|
.to(Date.today)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'tries to obtain ExclusiveLease' do
|
||||||
|
expect(Gitlab::ExclusiveLease).to receive(:new).and_call_original
|
||||||
|
|
||||||
|
subject.execute
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when a bad object is passed' do
|
context 'when a bad object is passed' do
|
||||||
|
@ -46,6 +52,12 @@ describe Users::ActivityService do
|
||||||
it 'does not update last_activity_on' do
|
it 'does not update last_activity_on' do
|
||||||
expect { subject.execute }.not_to change(user, :last_activity_on)
|
expect { subject.execute }.not_to change(user, :last_activity_on)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does not try to obtain ExclusiveLease' do
|
||||||
|
expect(Gitlab::ExclusiveLease).not_to receive(:new)
|
||||||
|
|
||||||
|
subject.execute
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when in GitLab read-only instance' do
|
context 'when in GitLab read-only instance' do
|
||||||
|
|
Loading…
Reference in New Issue