runner.rb 6.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
    include ChronicDurationAttribute
7

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

14
    has_many :builds
15
    has_many :runner_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
16
    has_many :projects, through: :runner_projects
A
Alexis Reigel 已提交
17 18
    has_many :runner_groups
    has_many :groups, through: :runner_groups
D
Douwe Maan 已提交
19 20 21 22 23

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

    before_validation :set_default_values

24 25 26 27 28 29 30 31 32 33 34
    scope :specific, -> { where(is_shared: false) }
    scope :shared, -> { where(is_shared: true) }
    scope :active, -> { where(active: true) }
    scope :paused, -> { where(active: false) }
    scope :online, -> { where('contacted_at > ?', contact_time_deadline) }
    scope :ordered, -> { order(id: :desc) }
    scope :belonging_to_project, -> (project_id) {
      joins(:runner_projects).where(ci_runner_projects: { project_id: project_id })
    }
    scope :belonging_to_group, -> (project_id) {
      joins(
35
        %{
36 37
          INNER JOIN ci_runner_groups ON ci_runner_groups.runner_id = ci_runners.id
          INNER JOIN namespaces ON namespaces.id = ci_runner_groups.group_id
38
          INNER JOIN projects ON projects.namespace_id = namespaces.id
A
Alexis Reigel 已提交
39
        }
40
      ).where('projects.id = :project_id', project_id: project_id)
41
    }
42

43 44
    scope :owned_or_shared, -> (project_id) do
      union = Gitlab::SQL::Union.new([belonging_to_project(project_id), belonging_to_group(project_id), shared])
45
      from("(#{union.to_sql}) ci_runners")
46 47
    end

L
Lin Jen-Shin 已提交
48
    scope :assignable_for, ->(project) do
L
Lin Jen-Shin 已提交
49 50
      # FIXME: That `to_sql` is needed to workaround a weird Rails bug.
      #        Without that, placeholders would miss one and couldn't match.
51 52
      where(locked: false)
        .where.not("id IN (#{project.runners.select(:id).to_sql})").specific
53 54
    end

55
    validate :tag_constraints
56
    validates :access_level, presence: true
57

D
Douwe Maan 已提交
58 59
    acts_as_taggable

60 61
    after_destroy :cleanup_runner_queue

62
    enum access_level: {
63 64
      not_protected: 0,
      ref_protected: 1
65 66
    }

67
    cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at, :ip_address
68

69
    chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout
70

71 72 73 74
    validates :maximum_timeout, allow_nil: true,
                                numericality: { greater_than_or_equal_to: 600,
                                                message: 'needs to be at least 10 minutes' }

75 76 77 78 79 80 81 82 83 84 85 86
    # 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 已提交
87
    def self.search(query)
88
      fuzzy_search(query, [:token, :description])
D
Douwe Maan 已提交
89 90
    end

A
Alessio Caiazza 已提交
91 92 93 94
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

D
Douwe Maan 已提交
95 96 97 98 99 100 101
    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
102
      project.runner_projects.create(runner_id: self.id)
D
Douwe Maan 已提交
103 104 105
    end

    def display_name
106
      return short_sha if description.blank?
D
Douwe Maan 已提交
107 108 109 110 111 112 113 114

      description
    end

    def shared?
      is_shared
    end

115
    def online?
116
      contacted_at && contacted_at > self.class.contact_time_deadline
117 118 119 120 121 122 123 124 125 126 127 128
    end

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

D
Douwe Maan 已提交
129 130 131 132 133 134 135 136
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

A
Alexis Reigel 已提交
137 138 139 140
    def group?
      runner_groups.any?
    end

A
Alexis Reigel 已提交
141 142 143 144
    def project?
      runner_projects.any?
    end

145
    def can_pick?(build)
146
      return false if self.ref_protected? && !build.protected?
S
Shinya Maeda 已提交
147

148
      assignable_for?(build.project_id) && accepting_tags?(build)
149 150
    end

D
Douwe Maan 已提交
151 152 153 154 155
    def only_for?(project)
      projects == [project]
    end

    def short_sha
K
Kamil Trzcinski 已提交
156
      token[0...8] if token
D
Douwe Maan 已提交
157
    end
158 159 160 161

    def has_tags?
      tag_list.any?
    end
162

163
    def predefined_variables
164 165 166 167
      Gitlab::Ci::Variables::Collection.new
        .append(key: 'CI_RUNNER_ID', value: id.to_s)
        .append(key: 'CI_RUNNER_DESCRIPTION', value: description)
        .append(key: 'CI_RUNNER_TAGS', value: tag_list.to_s)
168 169
    end

K
Kim "BKC" Carlbäcker 已提交
170
    def tick_runner_queue
171
      SecureRandom.hex.tap do |new_update|
K
Kamil Trzcinski 已提交
172
        ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
173
          expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
174
      end
175 176
    end

K
Kim "BKC" Carlbäcker 已提交
177
    def ensure_runner_queue_value
K
Kamil Trzcinski 已提交
178 179 180
      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 已提交
181 182
    end

183
    def runner_queue_value_latest?(value)
K
Kim "BKC" Carlbäcker 已提交
184
      ensure_runner_queue_value == value if value.present?
185 186
    end

187
    def update_cached_info(values)
188
      values = values&.slice(:version, :revision, :platform, :architecture, :ip_address) || {}
189
      values[:contacted_at] = Time.now
190

191
      cache_attributes(values)
192

193 194
      if persist_cached_data?
        self.assign_attributes(values)
195
        self.save if self.changed?
196
      end
197 198
    end

199 200
    private

201
    def cleanup_runner_queue
202
      Gitlab::Redis::Queues.with do |redis|
203 204 205 206
        redis.del(runner_queue_key)
      end
    end

K
Kim "BKC" Carlbäcker 已提交
207
    def runner_queue_key
K
Kim "BKC" Carlbäcker 已提交
208
      "runner:build_queue:#{self.token}"
209 210
    end

211 212 213
    def persist_cached_data?
      # Use a random threshold to prevent beating DB updates.
      # It generates a distribution between [40m, 80m].
214

215 216 217 218 219 220 221
      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

222
    def tag_constraints
223 224 225 226 227
      unless has_tags? || run_untagged?
        errors.add(:tags_list,
          'can not be empty when runner is not allowed to pick untagged jobs')
      end
    end
228

229
    def assignable_for?(project_id)
230
      self.class.owned_or_shared(project_id).where(id: self.id).any?
231 232
    end

233 234
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
235
    end
D
Douwe Maan 已提交
236 237
  end
end