Welcome to mirror list, hosted at ThFree Co, Russian Federation.

async_indexes.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 581c7e7ff949506a9ae407950cfb90595712034a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true

module Gitlab
  module Database
    module AsyncIndexes
      DEFAULT_INDEXES_PER_INVOCATION = 2

      def self.create_pending_indexes!(how_many: DEFAULT_INDEXES_PER_INVOCATION)
        PostgresAsyncIndex.to_create.order(:id).limit(how_many).each do |async_index|
          IndexCreator.new(async_index).perform
        end
      end

      def self.drop_pending_indexes!(how_many: DEFAULT_INDEXES_PER_INVOCATION)
        PostgresAsyncIndex.to_drop.order(:id).limit(how_many).each do |async_index|
          IndexDestructor.new(async_index).perform
        end
      end

      def self.execute_pending_actions!(how_many: DEFAULT_INDEXES_PER_INVOCATION)
        queue_ids = PostgresAsyncIndex.ordered.limit(how_many).pluck(:id)
        removal_actions = PostgresAsyncIndex.where(id: queue_ids).to_drop.ordered
        creation_actions = PostgresAsyncIndex.where(id: queue_ids).to_create.ordered

        removal_actions.each { |async_index| IndexDestructor.new(async_index).perform }
        creation_actions.each { |async_index| IndexCreator.new(async_index).perform }
      end
    end
  end
end