Commit 9db1d178 authored by Illya Klymov's avatar Illya Klymov

Expire relevant ETag caches for realtime_changes endpoints

* ensure that updating import status will flush relevant cache

Changelog: fixed
parent d31c0d1a
......@@ -70,7 +70,7 @@ class Import::BaseController < ApplicationController
end
def already_added_projects
@already_added_projects ||= filtered(find_already_added_projects(provider_name))
@already_added_projects ||= find_already_added_projects(provider_name)
end
# rubocop: disable CodeReuse/ActiveRecord
......
......@@ -38,6 +38,16 @@ class ProjectImportState < ApplicationRecord
state :finished
state :failed
after_transition any => any do |state|
realtime_changes_path = Gitlab::Routing.url_helpers.polymorphic_path([:realtime_changes_import, state.project.import_type.to_sym], format: :json) rescue nil
if realtime_changes_path
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(realtime_changes_path)
end
end
end
after_transition [:none, :finished, :failed] => :scheduled do |state, _|
state.run_after_commit do
job_id = project.add_import_job
......
......@@ -24,6 +24,8 @@ module Gitlab
increment_project_counter(project, object_type, operation, integer)
increment_global_counter(object_type, operation, integer)
expire_etag_cache(project.import_type)
end
def summary(project)
......@@ -42,6 +44,16 @@ module Gitlab
private
def expire_etag_cache(import_type)
realtime_changes_path = Gitlab::Routing.url_helpers.polymorphic_path([:realtime_changes_import, import_type.to_sym], format: :json) rescue nil
if realtime_changes_path
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(realtime_changes_path)
end
end
end
# Global counters are long lived, in Prometheus,
# and it's used to report the health of the Github Importer
# in the Grafana Dashboard
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe Gitlab::GithubImport::ObjectCounter, :clean_gitlab_redis_cache do
let_it_be(:project) { create(:project) }
let_it_be(:project) { create(:project, import_type: 'github') }
it 'validates the operation being incremented' do
expect { described_class.increment(project, :issue, :unknown) }
......@@ -49,4 +49,12 @@ RSpec.describe Gitlab::GithubImport::ObjectCounter, :clean_gitlab_redis_cache do
'imported' => {}
})
end
it 'expires etag cache of relevant realtime change endpoints on increment' do
expect_next_instance_of(Gitlab::EtagCaching::Store) do |instance|
expect(instance).to receive(:touch).with(Gitlab::Routing.url_helpers.realtime_changes_import_github_path(format: :json))
end
described_class.increment(project, :issue, :fetched)
end
end
......@@ -115,6 +115,46 @@ RSpec.describe ProjectImportState, type: :model do
end
describe 'import state transitions' do
context 'state transition: any => any' do
context 'when project import type has realtime changes endpoint' do
before do
import_state.project.import_type = 'github'
end
it 'expires revelant etag cache' do
described_class.state_machines[:status].states.each do |initial_state|
import_state.status = initial_state.name
import_state.status_transitions.each do |transition|
expect_next_instance_of(Gitlab::EtagCaching::Store) do |instance|
expect(instance).to receive(:touch).with(Gitlab::Routing.url_helpers.realtime_changes_import_github_path(format: :json))
end
transition.perform
end
end
end
end
context 'when project import type has no realtime changes endpoint' do
before do
import_state.project.import_type = 'jira'
end
it 'does not touch etag caches' do
described_class.state_machines[:status].states.each do |initial_state|
import_state.status = initial_state.name
import_state.status_transitions.each do |transition|
expect(Gitlab::EtagCaching::Store).not_to receive(:new)
transition.perform
end
end
end
end
end
context 'state transition: [:started] => [:finished]' do
let(:after_import_service) { spy(:after_import_service) }
let(:housekeeping_service) { spy(:housekeeping_service) }
......
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