Commit 2783d34d authored by charlie ablett's avatar charlie ablett

Merge branch 'mv-update-build-minutes-service-to-ci' into 'master'

Move UpdateBuildMinutesService under CI

See merge request gitlab-org/gitlab!52807
parents b3bdd985 51fe0bd6
......@@ -198,7 +198,6 @@ Rails/SaveBang:
- 'ee/spec/services/start_pull_mirroring_service_spec.rb'
- 'ee/spec/services/status_page/trigger_publish_service_spec.rb'
- 'ee/spec/services/todo_service_spec.rb'
- 'ee/spec/services/update_build_minutes_service_spec.rb'
- 'ee/spec/services/vulnerability_feedback/create_service_spec.rb'
- 'ee/spec/support/protected_tags/access_control_shared_examples.rb'
- 'ee/spec/support/shared_examples/features/protected_branches_access_control_shared_examples.rb'
......@@ -2530,7 +2529,6 @@ Gitlab/NamespacedClass:
- 'ee/app/services/ldap_group_reset_service.rb'
- 'ee/app/services/start_pull_mirroring_service.rb'
- 'ee/app/services/timebox_report_service.rb'
- 'ee/app/services/update_build_minutes_service.rb'
- 'ee/app/uploaders/issuable_metric_image_uploader.rb'
- 'ee/app/validators/host_validator.rb'
- 'ee/app/validators/ldap_filter_validator.rb'
......
# frozen_string_literal: true
module Ci
module Minutes
class UpdateBuildMinutesService < BaseService
def execute(build)
return unless build.shared_runners_minutes_limit_enabled?
return unless build.complete?
return unless build.duration&.positive?
count_projects_based_on_cost_factors(build)
end
private
def count_projects_based_on_cost_factors(build)
cost_factor = build.runner.minutes_cost_factor(project.visibility_level)
duration_with_cost_factor = (build.duration * cost_factor).to_i
return unless duration_with_cost_factor > 0
ProjectStatistics.update_counters(project_statistics,
shared_runners_seconds: duration_with_cost_factor)
NamespaceStatistics.update_counters(namespace_statistics,
shared_runners_seconds: duration_with_cost_factor)
end
def namespace_statistics
namespace.namespace_statistics || namespace.create_namespace_statistics
end
def project_statistics
project.statistics || project.create_statistics(namespace: project.namespace)
end
def namespace
project.shared_runners_limit_namespace
end
end
end
end
# frozen_string_literal: true
class UpdateBuildMinutesService < BaseService
def execute(build)
return unless build.shared_runners_minutes_limit_enabled?
return unless build.complete?
return unless build.duration&.positive?
count_projects_based_on_cost_factors(build)
end
private
def count_projects_based_on_cost_factors(build)
cost_factor = build.runner.minutes_cost_factor(project.visibility_level)
duration_with_cost_factor = (build.duration * cost_factor).to_i
return unless duration_with_cost_factor > 0
ProjectStatistics.update_counters(project_statistics,
shared_runners_seconds: duration_with_cost_factor)
NamespaceStatistics.update_counters(namespace_statistics,
shared_runners_seconds: duration_with_cost_factor)
end
def namespace_statistics
namespace.namespace_statistics || namespace.create_namespace_statistics
end
def project_statistics
project.statistics || project.create_statistics(namespace: project.namespace)
end
def namespace
project.shared_runners_limit_namespace
end
end
......@@ -3,7 +3,7 @@
module EE
module BuildFinishedWorker
def process_build(build)
UpdateBuildMinutesService.new(build.project, nil).execute(build)
::Ci::Minutes::UpdateBuildMinutesService.new(build.project, nil).execute(build)
# We need to use `reset` on `project` because their AR associations have been cached
# and `Namespace#namespace_statistics` will return stale data.
::Ci::Minutes::EmailNotificationService.new(build.project.reset).execute if ::Gitlab.com?
......
......@@ -98,7 +98,7 @@ RSpec.describe Ci::Build do
%w(success drop cancel).each do |event|
it "for event #{event}", :sidekiq_might_not_need_inline do
expect(UpdateBuildMinutesService)
expect(Ci::Minutes::UpdateBuildMinutesService)
.to receive(:new).and_call_original
job.public_send(event)
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe UpdateBuildMinutesService do
RSpec.describe Ci::Minutes::UpdateBuildMinutesService do
describe '#perform' do
let(:namespace) { create(:namespace, shared_runners_minutes_limit: 100) }
let(:project) { create(:project, :public, namespace: namespace) }
......@@ -31,7 +31,7 @@ RSpec.describe UpdateBuildMinutesService do
context 'when statistics are created' do
before do
project.statistics.update(shared_runners_seconds: 100)
project.statistics.update!(shared_runners_seconds: 100)
namespace.create_namespace_statistics(shared_runners_seconds: 100)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment