summaryrefslogtreecommitdiff
path: root/app/services/keys/last_used_service.rb
blob: dbd79f7da5534d2c01742a1c845dc0fd2c046a13 (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
module Keys
  class LastUsedService
    TIMEOUT = 1.day.to_i

    attr_reader :key

    # key - The Key for which to update the last used timestamp.
    def initialize(key)
      @key = key
    end

    def execute
      # We _only_ want to update last_used_at and not also updated_at (which
      # would be updated when using #touch).
      key.update_column(:last_used_at, Time.zone.now) if update?
    end

    def update?
      return false if ::Gitlab::Database.read_only?

      last_used = key.last_used_at

      return false if last_used && (Time.zone.now - last_used) <= TIMEOUT

      !!redis_lease.try_obtain
    end

    private

    def redis_lease
      Gitlab::ExclusiveLease
        .new("key_update_last_used_at:#{key.id}", timeout: TIMEOUT)
    end
  end
end