Welcome to mirror list, hosted at ThFree Co, Russian Federation.

activity_service.rb « users « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 85855f45e338de87d955e74be6590f131d795248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module Users
  class ActivityService
    LEASE_TIMEOUT = 1.minute.to_i

    def initialize(author)
      @user = if author.respond_to?(:username)
                author
              elsif author.respond_to?(:user)
                author.user
              end

      @user = nil unless @user.is_a?(User)
    end

    def execute
      return unless @user

      record_activity
    end

    private

    def record_activity
      return if Gitlab::Database.read_only?

      today = Date.today

      return if @user.last_activity_on == today

      lease = Gitlab::ExclusiveLease.new("activity_service:#{@user.id}",
                                         timeout: LEASE_TIMEOUT)
      return unless lease.try_obtain

      @user.update_attribute(:last_activity_on, today)
    end
  end
end