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
|
|
|
|
|
2020-01-06 04:07:42 -05:00
|
|
|
def initialize(author)
|
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
|
|
|
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?
|
|
|
|
|
2019-02-07 09:36:50 -05:00
|
|
|
today = Date.today
|
|
|
|
|
|
|
|
return if @user.last_activity_on == today
|
|
|
|
|
2019-04-11 18:38:28 -04:00
|
|
|
lease = Gitlab::ExclusiveLease.new("activity_service:#{@user.id}",
|
2018-07-12 07:21:08 -04:00
|
|
|
timeout: LEASE_TIMEOUT)
|
|
|
|
return unless lease.try_obtain
|
2016-10-05 10:41:32 -04:00
|
|
|
|
2019-02-07 09:36:50 -05:00
|
|
|
@user.update_attribute(:last_activity_on, today)
|
2016-10-05 10:41:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|