Skip to content
Snippets Groups Projects

Improve database response time for listing user activity

Merged Andreas Brandl requested to merge 40525-listing-user-activity-timeouts into master
All threads resolved!
Files
5
@@ -2,8 +2,8 @@ class UserInteractedProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
validates :project, presence: true
validates :user, presence: true
validates :project_id, presence: true
validates :user_id, presence: true
CACHE_EXPIRY_TIME = 1.day
@@ -15,13 +15,7 @@ def track(event)
# For events without a project, we simply don't care.
# An example of this is the creation of a snippet (which
# is not related to any project).
return unless event.project
# This is a precaution because the cache lookup
# will work just fine without an author.
#
# However, this should never happen (tm).
raise 'event#author not present unexpectedly' unless event.author
return unless event.project_id
attributes = {
project_id: event.project_id,
@@ -29,11 +23,17 @@ def track(event)
}
cached_exists?(attributes) do
begin
find_or_create_by!(attributes)
true # not caching the whole record here for now
rescue ActiveRecord::RecordNotUnique
retry
transaction(requires_new: true) do
begin
where(attributes).select(1).first || create!(attributes)
true # not caching the whole record here for now
rescue ActiveRecord::RecordNotUnique
# Note, above queries are not atomic and prone
# to race conditions (similar like #find_or_create!).
# We retry and make sure the outer transaction (if any)
# is not aborted because of this.
retry
end
end
end
end
Loading