2023-03-17 16:20:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
|
|
|
class RunnerMachine < Ci::ApplicationRecord
|
|
|
|
include FromUnion
|
2023-04-23 21:23:45 +05:30
|
|
|
include RedisCacheable
|
2023-03-17 16:20:25 +05:30
|
|
|
include Ci::HasRunnerExecutor
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
# The `UPDATE_CONTACT_COLUMN_EVERY` defines how often the Runner Machine DB entry can be updated
|
2023-05-27 22:25:52 +05:30
|
|
|
UPDATE_CONTACT_COLUMN_EVERY = (40.minutes)..(55.minutes)
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
belongs_to :runner
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
has_many :runner_machine_builds, inverse_of: :runner_machine, class_name: 'Ci::RunnerMachineBuild'
|
|
|
|
has_many :builds, through: :runner_machine_builds, class_name: 'Ci::Build'
|
2023-04-23 21:23:45 +05:30
|
|
|
belongs_to :runner_version, inverse_of: :runner_machines, primary_key: :version, foreign_key: :version,
|
|
|
|
class_name: 'Ci::RunnerVersion'
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
validates :runner, presence: true
|
2023-04-23 21:23:45 +05:30
|
|
|
validates :system_xid, presence: true, length: { maximum: 64 }
|
2023-03-17 16:20:25 +05:30
|
|
|
validates :version, length: { maximum: 2048 }
|
|
|
|
validates :revision, length: { maximum: 255 }
|
|
|
|
validates :platform, length: { maximum: 255 }
|
|
|
|
validates :architecture, length: { maximum: 255 }
|
|
|
|
validates :ip_address, length: { maximum: 1024 }
|
|
|
|
validates :config, json_schema: { filename: 'ci_runner_config' }
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
cached_attr_reader :version, :revision, :platform, :architecture, :ip_address, :contacted_at, :executor_type
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
# The `STALE_TIMEOUT` constant defines the how far past the last contact or creation date a runner machine
|
|
|
|
# will be considered stale
|
|
|
|
STALE_TIMEOUT = 7.days
|
|
|
|
|
|
|
|
scope :stale, -> do
|
|
|
|
created_some_time_ago = arel_table[:created_at].lteq(STALE_TIMEOUT.ago)
|
|
|
|
contacted_some_time_ago = arel_table[:contacted_at].lteq(STALE_TIMEOUT.ago)
|
|
|
|
|
|
|
|
from_union(
|
|
|
|
where(contacted_at: nil),
|
|
|
|
where(contacted_some_time_ago),
|
|
|
|
remove_duplicates: false).where(created_some_time_ago)
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def self.online_contact_time_deadline
|
|
|
|
Ci::Runner.online_contact_time_deadline
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stale_deadline
|
|
|
|
STALE_TIMEOUT.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
def heartbeat(values, update_contacted_at: true)
|
2023-04-23 21:23:45 +05:30
|
|
|
##
|
|
|
|
# We can safely ignore writes performed by a runner heartbeat. We do
|
|
|
|
# not want to upgrade database connection proxy to use the primary
|
|
|
|
# database after heartbeat write happens.
|
|
|
|
#
|
|
|
|
::Gitlab::Database::LoadBalancing::Session.without_sticky_writes do
|
|
|
|
values = values&.slice(:version, :revision, :platform, :architecture, :ip_address, :config, :executor) || {}
|
2023-05-27 22:25:52 +05:30
|
|
|
values[:contacted_at] = Time.current if update_contacted_at
|
2023-04-23 21:23:45 +05:30
|
|
|
if values.include?(:executor)
|
|
|
|
values[:executor_type] = Ci::Runner::EXECUTOR_NAME_TO_TYPES.fetch(values.delete(:executor), :unknown)
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
new_version = values[:version]
|
|
|
|
schedule_runner_version_update(new_version) if new_version && values[:version] != version
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
merge_cache_attributes(values)
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
# We save data without validation, it will always change due to `contacted_at`
|
|
|
|
update_columns(values) if persist_cached_data?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def status
|
|
|
|
return :stale if stale?
|
|
|
|
return :never_contacted unless contacted_at
|
|
|
|
|
|
|
|
online? ? :online : :offline
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
private
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def online?
|
|
|
|
contacted_at && contacted_at > self.class.online_contact_time_deadline
|
|
|
|
end
|
|
|
|
|
|
|
|
def stale?
|
|
|
|
return false unless created_at
|
|
|
|
|
|
|
|
[created_at, contacted_at].compact.max <= self.class.stale_deadline
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def persist_cached_data?
|
|
|
|
# Use a random threshold to prevent beating DB updates.
|
|
|
|
contacted_at_max_age = Random.rand(UPDATE_CONTACT_COLUMN_EVERY)
|
|
|
|
|
|
|
|
real_contacted_at = read_attribute(:contacted_at)
|
|
|
|
real_contacted_at.nil? ||
|
|
|
|
(Time.current - real_contacted_at) >= contacted_at_max_age
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def schedule_runner_version_update(new_version)
|
|
|
|
return unless new_version && Gitlab::Ci::RunnerReleases.instance.enabled?
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
Ci::Runners::ProcessRunnerVersionUpdateWorker.perform_async(new_version)
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
end
|