summaryrefslogtreecommitdiff
path: root/app/services/projects/refresh_build_artifacts_size_statistics_service.rb
blob: 1f86e5f4ba9d05bba19eb35a3df71e933c26988c (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
# frozen_string_literal: true

module Projects
  class RefreshBuildArtifactsSizeStatisticsService
    BATCH_SIZE = 1000

    def execute
      refresh = Projects::BuildArtifactsSizeRefresh.process_next_refresh!
      return unless refresh

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

      if batch.any?
        # We are doing the sum in ruby because the query takes too long when done in SQL
        total_artifacts_size = batch.sum { |artifact| artifact.size.to_i }

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

          refresh.project.statistics.delayed_increment_counter(:build_artifacts_size, total_artifacts_size)
        end
      else
        # Remove the refresh job from the table if there are no more
        # remaining job artifacts to calculate for the given project.
        refresh.destroy!
      end

      refresh
    end
  end
end