2021-04-17 20:07:23 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module BackgroundMigration
|
|
|
|
class BatchedJob < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
|
2021-06-08 01:23:25 +05:30
|
|
|
include FromUnion
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
self.table_name = :batched_background_migration_jobs
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
MAX_ATTEMPTS = 3
|
|
|
|
STUCK_JOBS_TIMEOUT = 1.hour.freeze
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
belongs_to :batched_migration, foreign_key: :batched_background_migration_id
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
scope :active, -> { where(status: [:pending, :running]) }
|
|
|
|
scope :stuck, -> { active.where('updated_at <= ?', STUCK_JOBS_TIMEOUT.ago) }
|
|
|
|
scope :retriable, -> {
|
|
|
|
failed_jobs = where(status: :failed).where('attempts < ?', MAX_ATTEMPTS)
|
|
|
|
|
|
|
|
from_union([failed_jobs, self.stuck])
|
|
|
|
}
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
enum status: {
|
|
|
|
pending: 0,
|
|
|
|
running: 1,
|
|
|
|
failed: 2,
|
|
|
|
succeeded: 3
|
|
|
|
}
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
scope :successful_in_execution_order, -> { where.not(finished_at: nil).succeeded.order(:finished_at) }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
delegate :aborted?, :job_class, :table_name, :column_name, :job_arguments,
|
|
|
|
to: :batched_migration, prefix: :migration
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
attribute :pause_ms, :integer, default: 100
|
|
|
|
|
|
|
|
def time_efficiency
|
|
|
|
return unless succeeded?
|
|
|
|
return unless finished_at && started_at
|
|
|
|
|
|
|
|
duration = finished_at - started_at
|
|
|
|
|
|
|
|
# TODO: Switch to individual job interval (prereq: https://gitlab.com/gitlab-org/gitlab/-/issues/328801)
|
|
|
|
duration.to_f / batched_migration.interval
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|