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

42 lines
996 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
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
2021-09-04 01:27:46 +05:30
::Gitlab::Database::LoadBalancing::Session.without_sticky_writes { record_activity }
2017-08-17 22:00:37 +05:30
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)
2022-08-13 15:12:31 +05:30
Gitlab::UsageDataCounters::HLLRedisCounter.track_event('unique_active_user', values: @user.id)
2017-08-17 22:00:37 +05:30
end
end
end