2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-20 08:21:15 -04:00
|
|
|
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?
|
2021-08-02 05:10:09 -04:00
|
|
|
return false if ::Gitlab::Database.read_only?
|
2017-09-19 03:44:58 -04:00
|
|
|
|
2017-09-20 08:21:15 -04:00
|
|
|
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
|