From 09abd1af81f65a32b0d3f9e2c370bff65eac2c2f Mon Sep 17 00:00:00 2001 From: rhymes Date: Wed, 17 Feb 2021 19:34:37 +0100 Subject: [PATCH] Fastly edge caching: use PURGE HTTP method instead of POST (#12627) * Add :fastly_http_purge feature flag * Add fastly.purge to EdgeCache::Bust::Fasty * Update app/services/edge_cache/bust/fastly.rb Co-authored-by: Michael Kohl * Update app/services/edge_cache/bust/fastly.rb Co-authored-by: Michael Kohl * Update spec/lib/data_update_scripts/add_fastly_http_purge_feature_flag_spec.rb Co-authored-by: Michael Kohl * Improve specs applying the feedback * Deploy to BHC * Undo Travis changes Co-authored-by: Michael Kohl Co-authored-by: Mac Siri --- app/services/edge_cache/bust/fastly.rb | 43 ++++++++++++------- ...2726_add_fastly_http_purge_feature_flag.rb | 7 +++ ...add_fastly_http_purge_feature_flag_spec.rb | 24 +++++++++++ 3 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 lib/data_update_scripts/20210209152726_add_fastly_http_purge_feature_flag.rb create mode 100644 spec/lib/data_update_scripts/add_fastly_http_purge_feature_flag_spec.rb diff --git a/app/services/edge_cache/bust/fastly.rb b/app/services/edge_cache/bust/fastly.rb index ee8e03e9a..ed7f055a1 100644 --- a/app/services/edge_cache/bust/fastly.rb +++ b/app/services/edge_cache/bust/fastly.rb @@ -1,24 +1,37 @@ module EdgeCache class Bust class Fastly + # [@forem/systems] Fastly-enabled Forems don't need "flexible" domains. def self.call(path) - # TODO: (Alex Smith) - It would be "nice to have" the ability to use the - # Fastly gem here instead of custom API calls. + api_key = ApplicationConfig["FASTLY_API_KEY"] + return fastly_purge(api_key, path) if FeatureFlag.enabled?(:fastly_http_purge) - # @forem/systems Fastly-enabled forems don't need "flexible" domains. - HTTParty.post( - "https://api.fastly.com/purge/https://#{URL.domain}#{path}", - headers: { - "Fastly-Key" => ApplicationConfig["FASTLY_API_KEY"] - }, - ) - HTTParty.post( - "https://api.fastly.com/purge/https://#{URL.domain}#{path}?i=i", - headers: { - "Fastly-Key" => ApplicationConfig["FASTLY_API_KEY"] - }, - ) + fastly_post(api_key, path) end + + def self.fastly_post(api_key, path) + urls(path).map do |url| + HTTParty.post("https://api.fastly.com/purge/#{url}", headers: { "Fastly-Key" => api_key }) + end + end + private_class_method :fastly_post + + def self.fastly_purge(api_key, path) + fastly = ::Fastly.new(api_key: api_key) + + urls(path).map do |url| + fastly.purge(url) + end + end + private_class_method :fastly_purge + + def self.urls(path) + [ + URL.url(path), + URL.url("#{path}?i=i"), + ] + end + private_class_method :fastly_purge end end end diff --git a/lib/data_update_scripts/20210209152726_add_fastly_http_purge_feature_flag.rb b/lib/data_update_scripts/20210209152726_add_fastly_http_purge_feature_flag.rb new file mode 100644 index 000000000..3c1af28c2 --- /dev/null +++ b/lib/data_update_scripts/20210209152726_add_fastly_http_purge_feature_flag.rb @@ -0,0 +1,7 @@ +module DataUpdateScripts + class AddFastlyHttpPurgeFeatureFlag + def run + FeatureFlag.add(:fastly_http_purge) + end + end +end diff --git a/spec/lib/data_update_scripts/add_fastly_http_purge_feature_flag_spec.rb b/spec/lib/data_update_scripts/add_fastly_http_purge_feature_flag_spec.rb new file mode 100644 index 000000000..7ed42758a --- /dev/null +++ b/spec/lib/data_update_scripts/add_fastly_http_purge_feature_flag_spec.rb @@ -0,0 +1,24 @@ +require "rails_helper" +require Rails.root.join( + "lib/data_update_scripts/20210209152726_add_fastly_http_purge_feature_flag.rb", +) + +describe DataUpdateScripts::AddFastlyHttpPurgeFeatureFlag do + after do + FeatureFlag.remove(:fastly_http_purge) + end + + it "adds the :fastly_http_purge flag", :aggregate_failures do + expect do + described_class.new.run + end.to change { FeatureFlag.exist?(:fastly_http_purge) }.from(false).to(true) + end + + it "works if the flag is already available" do + FeatureFlag.add(:fastly_http_purge) + + expect do + described_class.new.run + end.not_to change { FeatureFlag.exist?(:fastly_http_purge) } + end +end