2020-09-29 11:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module Reindexing
|
|
|
|
class ReindexAction < ActiveRecord::Base
|
|
|
|
self.table_name = 'postgres_reindex_actions'
|
|
|
|
|
2020-12-10 07:09:43 -05:00
|
|
|
belongs_to :index, foreign_key: :index_identifier, class_name: 'Gitlab::Database::PostgresIndex'
|
2020-09-29 11:10:08 -04:00
|
|
|
enum state: { started: 0, finished: 1, failed: 2 }
|
|
|
|
|
2020-12-10 07:09:43 -05:00
|
|
|
# Amount of time to consider a previous reindexing *recent*
|
|
|
|
RECENT_THRESHOLD = 7.days
|
|
|
|
|
|
|
|
scope :recent, -> { where(state: :finished).where('action_end > ?', Time.zone.now - RECENT_THRESHOLD) }
|
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
def self.create_for(index)
|
|
|
|
create!(
|
2020-09-29 11:10:08 -04:00
|
|
|
index_identifier: index.identifier,
|
|
|
|
action_start: Time.zone.now,
|
2020-12-11 16:10:13 -05:00
|
|
|
ondisk_size_bytes_start: index.ondisk_size_bytes,
|
|
|
|
bloat_estimate_bytes_start: index.bloat_size
|
2020-09-29 11:10:08 -04:00
|
|
|
)
|
2020-12-31 04:10:34 -05:00
|
|
|
end
|
2020-09-29 11:10:08 -04:00
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
def finish
|
2020-09-29 11:10:08 -04:00
|
|
|
index.reload # rubocop:disable Cop/ActiveRecordAssociationReload
|
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
self.state = :finished unless failed?
|
|
|
|
self.action_end = Time.zone.now
|
|
|
|
self.ondisk_size_bytes_end = index.ondisk_size_bytes
|
2020-09-29 11:10:08 -04:00
|
|
|
|
2020-12-31 04:10:34 -05:00
|
|
|
save!
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|