diff --git a/changelogs/unreleased/44582-clear-pipeline-status-cache.yml b/changelogs/unreleased/44582-clear-pipeline-status-cache.yml new file mode 100644 index 00000000000..1777f2ffaab --- /dev/null +++ b/changelogs/unreleased/44582-clear-pipeline-status-cache.yml @@ -0,0 +1,5 @@ +--- +title: Now `rake cache:clear` will also clear pipeline status cache +merge_request: 18257 +author: +type: fixed diff --git a/lib/gitlab/cache/ci/project_pipeline_status.rb b/lib/gitlab/cache/ci/project_pipeline_status.rb index dba37892863..add048d671e 100644 --- a/lib/gitlab/cache/ci/project_pipeline_status.rb +++ b/lib/gitlab/cache/ci/project_pipeline_status.rb @@ -40,7 +40,7 @@ module Gitlab end def self.cache_key_for_project(project) - "projects/#{project.id}/pipeline_status" + "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}:projects/#{project.id}/pipeline_status" end def self.update_for_pipeline(pipeline) diff --git a/lib/tasks/cache.rake b/lib/tasks/cache.rake index 564aa141952..cb4d5abffbc 100644 --- a/lib/tasks/cache.rake +++ b/lib/tasks/cache.rake @@ -6,17 +6,22 @@ namespace :cache do desc "GitLab | Clear redis cache" task redis: :environment do Gitlab::Redis::Cache.with do |redis| - cursor = REDIS_SCAN_START_STOP - loop do - cursor, keys = redis.scan( - cursor, - match: "#{Gitlab::Redis::Cache::CACHE_NAMESPACE}*", - count: REDIS_CLEAR_BATCH_SIZE - ) + cache_key_pattern = %W[#{Gitlab::Redis::Cache::CACHE_NAMESPACE}* + projects/*/pipeline_status] - redis.del(*keys) if keys.any? + cache_key_pattern.each do |match| + cursor = REDIS_SCAN_START_STOP + loop do + cursor, keys = redis.scan( + cursor, + match: match, + count: REDIS_CLEAR_BATCH_SIZE + ) - break if cursor == REDIS_SCAN_START_STOP + redis.del(*keys) if keys.any? + + break if cursor == REDIS_SCAN_START_STOP + end end end end diff --git a/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb b/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb index 16704ff5e77..18658588a40 100644 --- a/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb +++ b/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Gitlab::Cache::Ci::ProjectPipelineStatus, :clean_gitlab_redis_cache do let!(:project) { create(:project, :repository) } let(:pipeline_status) { described_class.new(project) } - let(:cache_key) { "projects/#{project.id}/pipeline_status" } + let(:cache_key) { described_class.cache_key_for_project(project) } describe '.load_for_project' do it "loads the status" do diff --git a/spec/tasks/cache/clear/redis_spec.rb b/spec/tasks/cache/clear/redis_spec.rb new file mode 100644 index 00000000000..cca2b864e9b --- /dev/null +++ b/spec/tasks/cache/clear/redis_spec.rb @@ -0,0 +1,19 @@ +require 'rake_helper' + +describe 'clearing redis cache' do + before do + Rake.application.rake_require 'tasks/cache' + end + + describe 'clearing pipeline status cache' do + let(:pipeline_status) { create(:ci_pipeline).project.pipeline_status } + + before do + allow(pipeline_status).to receive(:loaded).and_return(nil) + end + + it 'clears pipeline status cache' do + expect { run_rake_task('cache:clear:redis') }.to change { pipeline_status.has_cache? } + end + end +end