debian-mirror-gitlab/lib/gitlab/database/reindexing/reindex_action.rb

39 lines
1.1 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Reindexing
class ReindexAction < ActiveRecord::Base
self.table_name = 'postgres_reindex_actions'
2021-02-22 17:27:13 +05:30
belongs_to :index, foreign_key: :index_identifier, class_name: 'Gitlab::Database::PostgresIndex'
2021-01-03 14:25:43 +05:30
enum state: { started: 0, finished: 1, failed: 2 }
2021-02-22 17:27:13 +05:30
# Amount of time to consider a previous reindexing *recent*
2021-09-30 23:02:18 +05:30
RECENT_THRESHOLD = 10.days
2021-02-22 17:27:13 +05:30
scope :recent, -> { where(state: :finished).where('action_end > ?', Time.zone.now - RECENT_THRESHOLD) }
2021-03-08 18:12:59 +05:30
def self.create_for(index)
create!(
2021-01-03 14:25:43 +05:30
index_identifier: index.identifier,
action_start: Time.zone.now,
2021-02-22 17:27:13 +05:30
ondisk_size_bytes_start: index.ondisk_size_bytes,
bloat_estimate_bytes_start: index.bloat_size
2021-01-03 14:25:43 +05:30
)
2021-03-08 18:12:59 +05:30
end
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
def finish
2021-01-03 14:25:43 +05:30
index.reload # rubocop:disable Cop/ActiveRecordAssociationReload
2021-03-08 18:12:59 +05:30
self.state = :finished unless failed?
self.action_end = Time.zone.now
self.ondisk_size_bytes_end = index.ondisk_size_bytes
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
save!
2021-01-03 14:25:43 +05:30
end
end
end
end
end