diff options
Diffstat (limited to 'app/services/projects/refresh_build_artifacts_size_statistics_service.rb')
-rw-r--r-- | app/services/projects/refresh_build_artifacts_size_statistics_service.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/services/projects/refresh_build_artifacts_size_statistics_service.rb b/app/services/projects/refresh_build_artifacts_size_statistics_service.rb new file mode 100644 index 00000000000..794c042ea39 --- /dev/null +++ b/app/services/projects/refresh_build_artifacts_size_statistics_service.rb @@ -0,0 +1,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(&:size) + + 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 |