Commit 9c4cb45b authored by Rémy Coutable's avatar Rémy Coutable Committed by Ruben Davila

Merge branch 'throttle-last-activity-at' into 'master'

Restrict last_activity_at updates to one per hour

This MR reduces the number of updates to `projects.last_activity_at` to once per hour.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/22213

See merge request !6391
parent 7fd0e153
......@@ -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,
......
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