diff options
author | Toon Claes <toon@gitlab.com> | 2017-04-04 18:38:16 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2017-04-07 00:50:36 +0300 |
commit | 847b9c82326d4fa1c4ab28f0f500a374e92728cb (patch) | |
tree | 044f6fb772c80a2604e79ebf6ffabb3610d58172 | |
parent | ebae10467d1bfa03370d131533f2905673c03f71 (diff) |
Use Etag caching for pipelines json
Enable caching in the Etag::Middleware and when a pipeline changes
status, expire the cache for the project pipelines path.
-rw-r--r-- | app/models/ci/pipeline.rb | 2 | ||||
-rw-r--r-- | app/services/ci/expire_pipeline_cache_service.rb | 18 | ||||
-rw-r--r-- | lib/gitlab/etag_caching/middleware.rb | 4 | ||||
-rw-r--r-- | spec/models/ci/pipeline_spec.rb | 8 | ||||
-rw-r--r-- | spec/services/ci/expire_pipeline_cache_service_spec.rb | 18 |
5 files changed, 50 insertions, 0 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 7f9b0aaa0f1..c197e50ed69 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -88,6 +88,8 @@ module Ci pipeline.run_after_commit do PipelineHooksWorker.perform_async(id) + Ci::ExpirePipelineCacheService.new(project, nil) + .execute(pipeline) end end diff --git a/app/services/ci/expire_pipeline_cache_service.rb b/app/services/ci/expire_pipeline_cache_service.rb new file mode 100644 index 00000000000..95afe72e735 --- /dev/null +++ b/app/services/ci/expire_pipeline_cache_service.rb @@ -0,0 +1,18 @@ +module Ci + class ExpirePipelineCacheService < BaseService + def execute(pipeline) + @pipeline = pipeline + + Gitlab::EtagCaching::Store.new.touch(project_pipelines_path) + end + + private + + def project_pipelines_path + Gitlab::Routing.url_helpers.namespace_project_pipelines_path( + project.namespace, + project, + format: :json) + end + end +end diff --git a/lib/gitlab/etag_caching/middleware.rb b/lib/gitlab/etag_caching/middleware.rb index 630fe4fa849..6a58d483781 100644 --- a/lib/gitlab/etag_caching/middleware.rb +++ b/lib/gitlab/etag_caching/middleware.rb @@ -10,6 +10,10 @@ module Gitlab { regexp: %r(^(?!.*(#{RESERVED_WORDS})).*/issues/\d+/rendered_title\z), name: 'issue_title' + }, + { + regexp: %r(^(?!.*(#{RESERVED_WORDS})).*/pipelines\.json\z), + name: 'project_pipelines' } ].freeze diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb index e4a24fd63c2..02c47b3efc4 100644 --- a/spec/models/ci/pipeline_spec.rb +++ b/spec/models/ci/pipeline_spec.rb @@ -335,6 +335,14 @@ describe Ci::Pipeline, models: true do end end + describe 'pipeline ETag caching' do + it 'executes ExpirePipelinesCacheService' do + expect_any_instance_of(Ci::ExpirePipelineCacheService).to receive(:execute).with(pipeline) + + pipeline.cancel + end + end + def create_build(name, queued_at = current, started_from = 0) create(:ci_build, name: name, diff --git a/spec/services/ci/expire_pipeline_cache_service_spec.rb b/spec/services/ci/expire_pipeline_cache_service_spec.rb new file mode 100644 index 00000000000..b8250f99c5a --- /dev/null +++ b/spec/services/ci/expire_pipeline_cache_service_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe Ci::ExpirePipelineCacheService, services: true do + let(:user) { create(:user) } + let(:project) { create(:empty_project) } + let(:pipeline) { create(:ci_pipeline, project: project) } + subject { described_class.new(project, user) } + + describe '#execute' do + it 'invalidate Etag caching for project pipelines path' do + path = "/#{project.full_path}/pipelines.json" + + expect_any_instance_of(Gitlab::EtagCaching::Store).to receive(:touch).with(path) + + subject.execute(pipeline) + end + end +end |