debian-mirror-gitlab/app/services/keys/last_used_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

36 lines
750 B
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Keys
class LastUsedService
2023-07-09 08:55:56 +05:30
TIMEOUT = 1.day
2018-03-17 18:26:18 +05:30
attr_reader :key
# key - The Key for which to update the last used timestamp.
def initialize(key)
@key = key
end
def execute
2023-07-09 08:55:56 +05:30
return unless update?
2018-03-17 18:26:18 +05:30
# We _only_ want to update last_used_at and not also updated_at (which
# would be updated when using #touch).
2023-07-09 08:55:56 +05:30
key.update_column(:last_used_at, Time.zone.now)
2018-03-17 18:26:18 +05:30
end
2023-07-09 08:55:56 +05:30
def execute_async
return unless update?
2018-03-17 18:26:18 +05:30
2023-07-09 08:55:56 +05:30
::SshKeys::UpdateLastUsedAtWorker.perform_async(key.id)
2018-03-17 18:26:18 +05:30
end
2023-07-09 08:55:56 +05:30
def update?
return false if ::Gitlab::Database.read_only?
2018-03-17 18:26:18 +05:30
2023-07-09 08:55:56 +05:30
last_used = key.last_used_at
last_used.blank? || last_used <= TIMEOUT.ago
2018-03-17 18:26:18 +05:30
end
end
end