2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Users
|
|
|
|
class ActivityService
|
2018-11-18 11:00:15 +05:30
|
|
|
LEASE_TIMEOUT = 1.minute.to_i
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def initialize(author)
|
2018-11-18 11:00:15 +05:30
|
|
|
@user = if author.respond_to?(:username)
|
|
|
|
author
|
|
|
|
elsif author.respond_to?(:user)
|
|
|
|
author.user
|
|
|
|
end
|
|
|
|
|
|
|
|
@user = nil unless @user.is_a?(User)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-11-18 11:00:15 +05:30
|
|
|
return unless @user
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
record_activity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def record_activity
|
2018-11-18 11:00:15 +05:30
|
|
|
return if Gitlab::Database.read_only?
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
today = Date.today
|
|
|
|
|
|
|
|
return if @user.last_activity_on == today
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
lease = Gitlab::ExclusiveLease.new("activity_service:#{@user.id}",
|
2018-11-18 11:00:15 +05:30
|
|
|
timeout: LEASE_TIMEOUT)
|
|
|
|
return unless lease.try_obtain
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
@user.update_attribute(:last_activity_on, today)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|