runner.rb 5.7 KB
Newer Older
D
Douwe Maan 已提交
1 2
module Ci
  class Runner < ActiveRecord::Base
3
    extend Gitlab::Ci::Model
4
    include Gitlab::SQL::Pattern
5
    include RedisCacheable
6

7
    RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
A
Alessio Caiazza 已提交
8
    ONLINE_CONTACT_TIMEOUT = 1.hour
9
    UPDATE_DB_RUNNER_INFO_EVERY = 40.minutes
D
Douwe Maan 已提交
10
    AVAILABLE_SCOPES = %w[specific shared active paused online].freeze
11
    FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level].freeze
12

13
    has_many :builds
14
    has_many :runner_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
K
Kamil Trzciński 已提交
15
    has_many :projects, through: :runner_projects
D
Douwe Maan 已提交
16 17 18 19 20 21 22 23 24

    has_one :last_build, ->() { order('id DESC') }, class_name: 'Ci::Build'

    before_validation :set_default_values

    scope :specific, ->() { where(is_shared: false) }
    scope :shared, ->() { where(is_shared: true) }
    scope :active, ->() { where(active: true) }
    scope :paused, ->() { where(active: false) }
A
Alessio Caiazza 已提交
25
    scope :online, ->() { where('contacted_at > ?', contact_time_deadline) }
K
Kamil Trzcinski 已提交
26
    scope :ordered, ->() { order(id: :desc) }
D
Douwe Maan 已提交
27

28
    scope :owned_or_shared, ->(project_id) do
D
Douwe Maan 已提交
29
      joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id')
K
Kamil Trzciński 已提交
30
        .where("ci_runner_projects.project_id = :project_id OR ci_runners.is_shared = true", project_id: project_id)
31 32
    end

L
Lin Jen-Shin 已提交
33
    scope :assignable_for, ->(project) do
L
Lin Jen-Shin 已提交
34 35
      # FIXME: That `to_sql` is needed to workaround a weird Rails bug.
      #        Without that, placeholders would miss one and couldn't match.
36 37
      where(locked: false)
        .where.not("id IN (#{project.runners.select(:id).to_sql})").specific
38 39
    end

40
    validate :tag_constraints
41
    validates :access_level, presence: true
42

D
Douwe Maan 已提交
43 44
    acts_as_taggable

45 46
    after_destroy :cleanup_runner_queue

47
    enum access_level: {
48 49
      not_protected: 0,
      ref_protected: 1
50 51
    }

52
    cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at, :ip_address
53

54 55 56 57 58 59 60 61 62 63 64 65
    # Searches for runners matching the given query.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # This method performs a *partial* match on tokens, thus a query for "a"
    # will match any runner where the token contains the letter "a". As a result
    # you should *not* use this method for non-admin purposes as otherwise users
    # might be able to query a list of all runners.
    #
    # query - The search query as a String
    #
    # Returns an ActiveRecord::Relation.
D
Douwe Maan 已提交
66
    def self.search(query)
67
      fuzzy_search(query, [:token, :description])
D
Douwe Maan 已提交
68 69
    end

A
Alessio Caiazza 已提交
70 71 72 73
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

D
Douwe Maan 已提交
74 75 76 77 78 79 80
    def set_default_values
      self.token = SecureRandom.hex(15) if self.token.blank?
    end

    def assign_to(project, current_user = nil)
      self.is_shared = false if shared?
      self.save
81
      project.runner_projects.create(runner_id: self.id)
D
Douwe Maan 已提交
82 83 84
    end

    def display_name
85
      return short_sha if description.blank?
D
Douwe Maan 已提交
86 87 88 89 90 91 92 93

      description
    end

    def shared?
      is_shared
    end

94
    def online?
95
      contacted_at && contacted_at > self.class.contact_time_deadline
96 97 98 99 100 101 102 103 104 105 106 107
    end

    def status
      if contacted_at.nil?
        :not_connected
      elsif active?
        online? ? :online : :offline
      else
        :paused
      end
    end

D
Douwe Maan 已提交
108 109 110 111 112 113 114 115
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

116
    def can_pick?(build)
117
      return false if self.ref_protected? && !build.protected?
S
Shinya Maeda 已提交
118

119
      assignable_for?(build.project_id) && accepting_tags?(build)
120 121
    end

D
Douwe Maan 已提交
122 123 124 125 126
    def only_for?(project)
      projects == [project]
    end

    def short_sha
K
Kamil Trzcinski 已提交
127
      token[0...8] if token
D
Douwe Maan 已提交
128
    end
129 130 131 132

    def has_tags?
      tag_list.any?
    end
133

134 135 136 137 138 139 140 141
    def predefined_variables
      [
        { key: 'CI_RUNNER_ID', value: id.to_s, public: true },
        { key: 'CI_RUNNER_DESCRIPTION', value: description, public: true },
        { key: 'CI_RUNNER_TAGS', value: tag_list.to_s, public: true }
      ]
    end

K
Kim "BKC" Carlbäcker 已提交
142
    def tick_runner_queue
143
      SecureRandom.hex.tap do |new_update|
K
Kamil Trzcinski 已提交
144
        ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
145
          expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
146
      end
147 148
    end

K
Kim "BKC" Carlbäcker 已提交
149
    def ensure_runner_queue_value
K
Kamil Trzcinski 已提交
150 151 152
      new_value = SecureRandom.hex
      ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_value,
        expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: false)
K
Kim "BKC" Carlbäcker 已提交
153 154
    end

155
    def runner_queue_value_latest?(value)
K
Kim "BKC" Carlbäcker 已提交
156
      ensure_runner_queue_value == value if value.present?
157 158
    end

159
    def update_cached_info(values)
160
      values = values&.slice(:version, :revision, :platform, :architecture, :ip_address) || {}
161
      values[:contacted_at] = Time.now
162

163
      cache_attributes(values)
164

165 166
      if persist_cached_data?
        self.assign_attributes(values)
167
        self.save if self.changed?
168
      end
169 170
    end

171 172
    private

173
    def cleanup_runner_queue
174
      Gitlab::Redis::Queues.with do |redis|
175 176 177 178
        redis.del(runner_queue_key)
      end
    end

K
Kim "BKC" Carlbäcker 已提交
179
    def runner_queue_key
K
Kim "BKC" Carlbäcker 已提交
180
      "runner:build_queue:#{self.token}"
181 182
    end

183 184 185
    def persist_cached_data?
      # Use a random threshold to prevent beating DB updates.
      # It generates a distribution between [40m, 80m].
186

187 188 189 190 191 192 193
      contacted_at_max_age = UPDATE_DB_RUNNER_INFO_EVERY + Random.rand(UPDATE_DB_RUNNER_INFO_EVERY)

      real_contacted_at = read_attribute(:contacted_at)
      real_contacted_at.nil? ||
        (Time.now - real_contacted_at) >= contacted_at_max_age
    end

194
    def tag_constraints
195 196 197 198 199
      unless has_tags? || run_untagged?
        errors.add(:tags_list,
          'can not be empty when runner is not allowed to pick untagged jobs')
      end
    end
200

201 202
    def assignable_for?(project_id)
      is_shared? || projects.exists?(id: project_id)
203 204
    end

205 206
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
207
    end
D
Douwe Maan 已提交
208 209
  end
end