2018-07-18 12:03:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-05 10:41:32 -04:00
|
|
|
module Users
|
|
|
|
class ActivityService
|
2018-07-12 07:21:08 -04:00
|
|
|
LEASE_TIMEOUT = 1.minute.to_i
|
|
|
|
|
2016-10-05 10:41:32 -04:00
|
|
|
def initialize(author, activity)
|
2018-07-12 07:21:08 -04:00
|
|
|
@user = if author.respond_to?(:username)
|
|
|
|
author
|
|
|
|
elsif author.respond_to?(:user)
|
|
|
|
author.user
|
|
|
|
end
|
|
|
|
|
2018-08-02 14:17:00 -04:00
|
|
|
@user = nil unless @user.is_a?(User)
|
2016-10-05 10:41:32 -04:00
|
|
|
@activity = activity
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-07-12 07:21:08 -04:00
|
|
|
return unless @user
|
2016-10-05 10:41:32 -04:00
|
|
|
|
|
|
|
record_activity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def record_activity
|
2018-07-12 07:21:08 -04:00
|
|
|
return if Gitlab::Database.read_only?
|
|
|
|
|
|
|
|
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
|
|
|
|
timeout: LEASE_TIMEOUT)
|
|
|
|
return unless lease.try_obtain
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2018-07-12 07:21:08 -04:00
|
|
|
@user.update_attribute(:last_activity_on, Date.today)
|
|
|
|
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@user.id} (username: #{@user.username})")
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|