summaryrefslogtreecommitdiff
path: root/app/services/projects/refresh_build_artifacts_size_statistics_service.rb
blob: f11083d6c045618b6fc162a6040c4b732f935cca (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
31
32
33
34
35
# frozen_string_literal: true

module Projects
  class RefreshBuildArtifactsSizeStatisticsService
    BATCH_SIZE = 500
    REFRESH_INTERVAL_SECONDS = 0.1

    def execute
      refresh = Projects::BuildArtifactsSizeRefresh.process_next_refresh!

      return unless refresh&.running?

      batch = refresh.next_batch(limit: BATCH_SIZE).to_a

      if batch.any?
        increments = batch.map do |artifact|
          Gitlab::Counters::Increment.new(amount: artifact.size.to_i, ref: artifact.id)
        end

        Projects::BuildArtifactsSizeRefresh.transaction do
          # Mark the refresh ready for another worker to pick up and process the next batch
          refresh.requeue!(batch.last.id)

          ProjectStatistics.bulk_increment_statistic(refresh.project, :build_artifacts_size, increments)
        end

        sleep REFRESH_INTERVAL_SECONDS
      else
        refresh.schedule_finalize!
      end

      refresh
    end
  end
end