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 <citizen428@dev.to>

* Update app/services/edge_cache/bust/fastly.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Update spec/lib/data_update_scripts/add_fastly_http_purge_feature_flag_spec.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Improve specs applying the feedback

* Deploy to BHC

* Undo Travis changes

Co-authored-by: Michael Kohl <citizen428@dev.to>
Co-authored-by: Mac Siri <krairit.siri@gmail.com>
This commit is contained in:
rhymes 2021-02-17 19:34:37 +01:00 committed by GitHub
parent 1be3602724
commit 09abd1af81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 15 deletions

View file

@ -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

View file

@ -0,0 +1,7 @@
module DataUpdateScripts
class AddFastlyHttpPurgeFeatureFlag
def run
FeatureFlag.add(:fastly_http_purge)
end
end
end

View file

@ -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