Class: Ci::Runner

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Model
Defined in:
app/models/ci/runner.rb

Constant Summary collapse

LAST_CONTACT_TIME =
5.minutes.ago
AVAILABLE_SCOPES =
['specific', 'shared', 'active', 'paused', 'online']

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

model_name, table_name_prefix

Class Method Details

.search(query) ⇒ Object

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.


61
62
63
64
65
66
# File 'app/models/ci/runner.rb', line 61

def self.search(query)
  t = arel_table
  pattern = "%#{query}%"

  where(t[:token].matches(pattern).or(t[:description].matches(pattern)))
end

Instance Method Details

#assign_to(project, current_user = nil) ⇒ Object


72
73
74
75
76
# File 'app/models/ci/runner.rb', line 72

def assign_to(project, current_user = nil)
  self.is_shared = false if shared?
  self.save
  project.runner_projects.create!(runner_id: self.id)
end

#belongs_to_one_project?Boolean

Returns:

  • (Boolean)

102
103
104
# File 'app/models/ci/runner.rb', line 102

def belongs_to_one_project?
  runner_projects.count == 1
end

#display_nameObject


78
79
80
81
82
# File 'app/models/ci/runner.rb', line 78

def display_name
  return short_sha unless !description.blank?

  description
end

#online?Boolean

Returns:

  • (Boolean)

88
89
90
# File 'app/models/ci/runner.rb', line 88

def online?
  contacted_at && contacted_at > LAST_CONTACT_TIME
end

#only_for?(project) ⇒ Boolean

Returns:

  • (Boolean)

110
111
112
# File 'app/models/ci/runner.rb', line 110

def only_for?(project)
  projects == [project]
end

#set_default_valuesObject


68
69
70
# File 'app/models/ci/runner.rb', line 68

def set_default_values
  self.token = SecureRandom.hex(15) if self.token.blank?
end

#shared?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'app/models/ci/runner.rb', line 84

def shared?
  is_shared
end

#short_shaObject


114
115
116
# File 'app/models/ci/runner.rb', line 114

def short_sha
  token[0...8] if token
end

#specific?Boolean

Returns:

  • (Boolean)

106
107
108
# File 'app/models/ci/runner.rb', line 106

def specific?
  !shared?
end

#statusObject


92
93
94
95
96
97
98
99
100
# File 'app/models/ci/runner.rb', line 92

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