Restrict last_activity_at updates to one per hour
The lock in turn is only obtained when actually needed, reducing some load on Redis. Fixes gitlab-org/gitlab-ce#22213
This commit is contained in:
parent
c20e4267e8
commit
028c086f90
3 changed files with 48 additions and 13 deletions
|
@ -15,6 +15,7 @@ v 8.12.0 (unreleased)
|
|||
- Make push events have equal vertical spacing.
|
||||
- Add two-factor recovery endpoint to internal API !5510
|
||||
- Pass the "Remember me" value to the U2F authentication form
|
||||
- Only update projects.last_activity_at once per hour when creating a new event
|
||||
- Remove vendor prefixes for linear-gradient CSS (ClemMakesApps)
|
||||
- Move pushes_since_gc from the database to Redis
|
||||
- Add font color contrast to external label in admin area (ClemMakesApps)
|
||||
|
|
|
@ -13,6 +13,8 @@ class Event < ActiveRecord::Base
|
|||
LEFT = 9 # User left project
|
||||
DESTROYED = 10
|
||||
|
||||
RESET_PROJECT_ACTIVITY_INTERVAL = 1.hour
|
||||
|
||||
delegate :name, :email, to: :author, prefix: true, allow_nil: true
|
||||
delegate :title, to: :issue, prefix: true, allow_nil: true
|
||||
delegate :title, to: :merge_request, prefix: true, allow_nil: true
|
||||
|
@ -324,8 +326,17 @@ class Event < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def reset_project_activity
|
||||
if project && Gitlab::ExclusiveLease.new("project:update_last_activity_at:#{project.id}", timeout: 60).try_obtain
|
||||
project.update_column(:last_activity_at, self.created_at)
|
||||
end
|
||||
return unless project
|
||||
|
||||
# Don't even bother obtaining a lock if the last update happened less than
|
||||
# 60 minutes ago.
|
||||
return if project.last_activity_at > RESET_PROJECT_ACTIVITY_INTERVAL.ago
|
||||
|
||||
return unless Gitlab::ExclusiveLease.
|
||||
new("project:update_last_activity_at:#{project.id}",
|
||||
timeout: RESET_PROJECT_ACTIVITY_INTERVAL.to_i).
|
||||
try_obtain
|
||||
|
||||
project.update_column(:last_activity_at, created_at)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,18 +16,12 @@ describe Event, models: true do
|
|||
|
||||
describe 'Callbacks' do
|
||||
describe 'after_create :reset_project_activity' do
|
||||
let(:project) { create(:project) }
|
||||
let(:project) { create(:empty_project) }
|
||||
|
||||
context "project's last activity was less than 5 minutes ago" do
|
||||
it 'does not update project.last_activity_at if it has been touched less than 5 minutes ago' do
|
||||
create_event(project, project.owner)
|
||||
project.update_column(:last_activity_at, 5.minutes.ago)
|
||||
project_last_activity_at = project.last_activity_at
|
||||
it 'calls the reset_project_activity method' do
|
||||
expect_any_instance_of(Event).to receive(:reset_project_activity)
|
||||
|
||||
create_event(project, project.owner)
|
||||
|
||||
expect(project.last_activity_at).to eq(project_last_activity_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -161,6 +155,35 @@ describe Event, models: true do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#reset_project_activity' do
|
||||
let(:project) { create(:empty_project) }
|
||||
|
||||
context 'when a project was updated less than 1 hour ago' do
|
||||
it 'does not update the project' do
|
||||
project.update(last_activity_at: Time.now)
|
||||
|
||||
expect(project).not_to receive(:update_column).
|
||||
with(:last_activity_at, a_kind_of(Time))
|
||||
|
||||
create_event(project, project.owner)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a project was updated more than 1 hour ago' do
|
||||
it 'updates the project' do
|
||||
project.update(last_activity_at: 1.year.ago)
|
||||
|
||||
expect_any_instance_of(Gitlab::ExclusiveLease).
|
||||
to receive(:try_obtain).and_return(true)
|
||||
|
||||
expect(project).to receive(:update_column).
|
||||
with(:last_activity_at, a_kind_of(Time))
|
||||
|
||||
create_event(project, project.owner)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_event(project, user, attrs = {})
|
||||
data = {
|
||||
before: Gitlab::Git::BLANK_SHA,
|
||||
|
|
Loading…
Reference in a new issue