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

41 lines
862 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?
2019-05-18 00:54:41 +05:30
today = Date.today
return if @user.last_activity_on == today
2018-11-18 11:00:15 +05:30
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
timeout: LEASE_TIMEOUT)
return unless lease.try_obtain
2017-08-17 22:00:37 +05:30
2019-05-18 00:54:41 +05:30
@user.update_attribute(:last_activity_on, today)
2017-08-17 22:00:37 +05:30
end
end
end