diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 445247f1b417d4880fe3544577dc9492f7530f6f..bd5b0ed3cffa1cdc89aed5a44dca7482cf921d7a 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -96,8 +96,7 @@ module Ci pipeline.run_after_commit do PipelineHooksWorker.perform_async(id) - Ci::ExpirePipelineCacheService.new(project, nil) - .execute(pipeline) + ExpirePipelineCacheWorker.perform_async(pipeline.id) end end @@ -385,6 +384,13 @@ module Ci .select { |merge_request| merge_request.head_pipeline.try(:id) == self.id } end + # All the merge requests for which the current pipeline runs/ran against + def all_merge_requests + @all_merge_requests ||= project.merge_requests + .where(source_branch: ref) + .select { |merge_request| merge_request.all_pipelines.includes(self) } + end + def detailed_status(current_user) Gitlab::Ci::Status::Pipeline::Factory .new(self, current_user) diff --git a/app/services/ci/expire_pipeline_cache_service.rb b/app/services/ci/expire_pipeline_cache_service.rb deleted file mode 100644 index 91d9c1d2ba1b0d612d0859216091cff59b703437..0000000000000000000000000000000000000000 --- a/app/services/ci/expire_pipeline_cache_service.rb +++ /dev/null @@ -1,51 +0,0 @@ -module Ci - class ExpirePipelineCacheService < BaseService - attr_reader :pipeline - - def execute(pipeline) - @pipeline = pipeline - store = Gitlab::EtagCaching::Store.new - - store.touch(project_pipelines_path) - store.touch(commit_pipelines_path) if pipeline.commit - store.touch(new_merge_request_pipelines_path) - merge_requests_pipelines_paths.each { |path| store.touch(path) } - - Gitlab::Cache::Ci::ProjectPipelineStatus.update_for_pipeline(@pipeline) - end - - private - - def project_pipelines_path - Gitlab::Routing.url_helpers.namespace_project_pipelines_path( - project.namespace, - project, - format: :json) - end - - def commit_pipelines_path - Gitlab::Routing.url_helpers.pipelines_namespace_project_commit_path( - project.namespace, - project, - pipeline.commit.id, - format: :json) - end - - def new_merge_request_pipelines_path - Gitlab::Routing.url_helpers.new_namespace_project_merge_request_path( - project.namespace, - project, - format: :json) - end - - def merge_requests_pipelines_paths - pipeline.merge_requests.collect do |merge_request| - Gitlab::Routing.url_helpers.pipelines_namespace_project_merge_request_path( - project.namespace, - project, - merge_request, - format: :json) - end - end - end -end diff --git a/app/workers/expire_pipeline_cache_worker.rb b/app/workers/expire_pipeline_cache_worker.rb new file mode 100644 index 0000000000000000000000000000000000000000..65e7b09150693eb929afcdeea080662de9b7ca86 --- /dev/null +++ b/app/workers/expire_pipeline_cache_worker.rb @@ -0,0 +1,51 @@ +class ExpirePipelineCacheWorker + include Sidekiq::Worker + include PipelineQueue + + def perform(id) + pipeline = Ci::Pipeline.find(id) + project = pipeline.project + store = Gitlab::EtagCaching::Store.new + + store.touch(project_pipelines_path(project)) + store.touch(commit_pipelines_path(project, pipeline.commit)) if pipeline.commit + store.touch(new_merge_request_pipelines_path(project)) + merge_requests_pipelines_paths(project, pipeline).each { |path| store.touch(path) } + + Gitlab::Cache::Ci::ProjectPipelineStatus.update_for_pipeline(pipeline) + end + + private + + def project_pipelines_path(project) + Gitlab::Routing.url_helpers.namespace_project_pipelines_path( + project.namespace, + project, + format: :json) + end + + def commit_pipelines_path(project, commit) + Gitlab::Routing.url_helpers.pipelines_namespace_project_commit_path( + project.namespace, + project, + commit.id, + format: :json) + end + + def new_merge_request_pipelines_path(project) + Gitlab::Routing.url_helpers.new_namespace_project_merge_request_path( + project.namespace, + project, + format: :json) + end + + def merge_requests_pipelines_paths(project, pipeline) + pipeline.all_merge_requests.collect do |merge_request| + Gitlab::Routing.url_helpers.pipelines_namespace_project_merge_request_path( + project.namespace, + project, + merge_request, + format: :json) + end + end +end diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index d7d6a75d38d1d95eaefff21f69ee263cf61635e9..42c0791fba1c9c25fd2ea0989f61c306e108ce41 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -376,8 +376,8 @@ describe Ci::Pipeline, models: true do end describe 'pipeline caching' do - it 'executes ExpirePipelinesCacheService' do - expect_any_instance_of(Ci::ExpirePipelineCacheService).to receive(:execute).with(pipeline) + it 'performs ExpirePipelinesCacheWorker' do + expect(ExpirePipelineCacheWorker).to receive(:perform_async).with(pipeline.id) pipeline.cancel end diff --git a/spec/services/ci/expire_pipeline_cache_service_spec.rb b/spec/workers/expire_pipeline_cache_worker_spec.rb similarity index 80% rename from spec/services/ci/expire_pipeline_cache_service_spec.rb rename to spec/workers/expire_pipeline_cache_worker_spec.rb index 166c6dfc93ef2dd8104906319c76bfd6d1c84637..0138bfa435988e4e71a90e4be514138175c0100f 100644 --- a/spec/services/ci/expire_pipeline_cache_service_spec.rb +++ b/spec/workers/expire_pipeline_cache_worker_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' -describe Ci::ExpirePipelineCacheService, services: true do +describe ExpirePipelineCacheWorker do let(:user) { create(:user) } let(:project) { create(:empty_project) } let(:pipeline) { create(:ci_pipeline, project: project) } - subject { described_class.new(project, user) } + subject { described_class.new } - describe '#execute' do + describe '#perform' do it 'invalidate Etag caching for project pipelines path' do pipelines_path = "/#{project.full_path}/pipelines.json" new_mr_pipelines_path = "/#{project.full_path}/merge_requests/new.json" @@ -14,14 +14,14 @@ describe Ci::ExpirePipelineCacheService, services: true do expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(pipelines_path) expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(new_mr_pipelines_path) - subject.execute(pipeline) + subject.perform(pipeline.id) end it 'updates the cached status for a project' do expect(Gitlab::Cache::Ci::ProjectPipelineStatus).to receive(:update_for_pipeline). with(pipeline) - subject.execute(pipeline) + subject.perform(pipeline.id) end end end