gitlab-org--gitlab-foss/app/services/users/activity_service.rb
Stan Hu 1436423a49 Fix failing 500 errors when deploy tokens are used to clone
A DeployToken responds to `:username`, but it returns the username for the
token, not a User object. Don't attempt to log user activity in this case.

Closes gitlab-org/gitlab-ee#7080
2018-08-02 11:18:31 -07:00

37 lines
907 B
Ruby

# frozen_string_literal: true
module Users
class ActivityService
LEASE_TIMEOUT = 1.minute.to_i
def initialize(author, activity)
@user = if author.respond_to?(:username)
author
elsif author.respond_to?(:user)
author.user
end
@user = nil unless @user.is_a?(User)
@activity = activity
end
def execute
return unless @user
record_activity
end
private
def record_activity
return if Gitlab::Database.read_only?
lease = Gitlab::ExclusiveLease.new("acitvity_service:#{@user.id}",
timeout: LEASE_TIMEOUT)
return unless lease.try_obtain
@user.update_attribute(:last_activity_on, Date.today)
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@user.id} (username: #{@user.username})")
end
end
end