summaryrefslogtreecommitdiff
path: root/app/models/clusters/agent_token.rb
blob: 87dba50cd69f1ea08cf59080be57aa201e792613 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

module Clusters
  class AgentToken < ApplicationRecord
    include RedisCacheable
    include TokenAuthenticatable

    add_authentication_token_field :token, encrypted: :required, token_generator: -> { Devise.friendly_token(50) }
    cached_attr_reader :last_used_at

    self.table_name = 'cluster_agent_tokens'

    # The `UPDATE_USED_COLUMN_EVERY` defines how often the token DB entry can be updated
    UPDATE_USED_COLUMN_EVERY = (40.minutes..55.minutes).freeze

    belongs_to :agent, class_name: 'Clusters::Agent', optional: false
    belongs_to :created_by_user, class_name: 'User', optional: true

    before_save :ensure_token

    validates :description, length: { maximum: 1024 }
    validates :name, presence: true, length: { maximum: 255 }

    scope :order_last_used_at_desc, -> { order(::Gitlab::Database.nulls_last_order('last_used_at', 'DESC')) }

    def track_usage
      track_values = { last_used_at: Time.current.utc }

      cache_attributes(track_values)

      if can_update_track_values?
        log_activity_event!(track_values[:last_used_at]) unless agent.active?

        # Use update_column so updated_at is skipped
        update_columns(track_values)
      end
    end

    private

    def can_update_track_values?
      # Use a random threshold to prevent beating DB updates.
      last_used_at_max_age = Random.rand(UPDATE_USED_COLUMN_EVERY)

      real_last_used_at = read_attribute(:last_used_at)

      # Handle too many updates from high token traffic
      real_last_used_at.nil? ||
        (Time.current - real_last_used_at) >= last_used_at_max_age
    end

    def log_activity_event!(recorded_at)
      agent.activity_events.create!(
        kind: :agent_connected,
        level: :info,
        recorded_at: recorded_at,
        agent_token: self
      )
    end
  end
end