runner.rb 5.9 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
K
Kamil Trzciński 已提交
16
    has_many :projects, through: :runner_projects
D
Douwe Maan 已提交
17 18 19 20 21 22 23 24 25

    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 已提交
26
    scope :online, ->() { where('contacted_at > ?', contact_time_deadline) }
K
Kamil Trzcinski 已提交
27
    scope :ordered, ->() { order(id: :desc) }
D
Douwe Maan 已提交
28

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

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

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

D
Douwe Maan 已提交
44 45
    acts_as_taggable

46 47
    after_destroy :cleanup_runner_queue

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

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

55
    chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout
56

57 58 59 60 61 62 63 64 65 66 67 68
    # 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 已提交
69
    def self.search(query)
70
      fuzzy_search(query, [:token, :description])
D
Douwe Maan 已提交
71 72
    end

A
Alessio Caiazza 已提交
73 74 75 76
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

D
Douwe Maan 已提交
77 78 79 80 81 82 83
    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
84
      project.runner_projects.create(runner_id: self.id)
D
Douwe Maan 已提交
85 86 87
    end

    def display_name
88
      return short_sha if description.blank?
D
Douwe Maan 已提交
89 90 91 92 93 94 95 96

      description
    end

    def shared?
      is_shared
    end

97
    def online?
98
      contacted_at && contacted_at > self.class.contact_time_deadline
99 100 101 102 103 104 105 106 107 108 109 110
    end

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

D
Douwe Maan 已提交
111 112 113 114 115 116 117 118
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

119
    def can_pick?(build)
120
      return false if self.ref_protected? && !build.protected?
S
Shinya Maeda 已提交
121

122
      assignable_for?(build.project_id) && accepting_tags?(build)
123 124
    end

D
Douwe Maan 已提交
125 126 127 128 129
    def only_for?(project)
      projects == [project]
    end

    def short_sha
K
Kamil Trzcinski 已提交
130
      token[0...8] if token
D
Douwe Maan 已提交
131
    end
132 133 134 135

    def has_tags?
      tag_list.any?
    end
136

137
    def predefined_variables
138 139 140 141
      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)
142 143
    end

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

K
Kim "BKC" Carlbäcker 已提交
151
    def ensure_runner_queue_value
K
Kamil Trzcinski 已提交
152 153 154
      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 已提交
155 156
    end

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

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

165
      cache_attributes(values)
166

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

173 174
    private

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

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

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

189 190 191 192 193 194 195
      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

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

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

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