debian-mirror-gitlab/app/services/users/activity_service.rb

38 lines
907 B
Ruby
Raw Normal View History

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
2017-08-17 22:00:37 +05:30
def initialize(author, activity)
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
@activity = activity
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?
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
timeout: LEASE_TIMEOUT)
return unless lease.try_obtain
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
@user.update_attribute(:last_activity_on, Date.today)
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@user.id} (username: #{@user.username})")
2017-08-17 22:00:37 +05:30
end
end
end