-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to ask external caches to invalidate.
Add a cached_urls method to the Comment, FoiAttachment, InfoRequest, PublicBody and User models, containing the entries that those models wish to invalidate when an event is logged via log_event or when the censor rules change. Entries are either a fixed path to be purged or a regex beginning with '^' to be banned. Adds a NotifyCacheJob that for each of the object's cached_urls, for each locale, for each host in VARNISH_HOSTS, makes BAN or PURGE HTTP requests to the host, to invalidate the relevant entries.
- Loading branch information
Showing
18 changed files
with
290 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'net/http' | ||
|
||
class Net::HTTP::Purge < Net::HTTP::Get | ||
METHOD = 'PURGE' | ||
end | ||
|
||
class Net::HTTP::Ban < Net::HTTP::Get | ||
METHOD = 'BAN' | ||
end | ||
|
||
## | ||
# Job to notify a cache of URLs to be purged or banned, given an object | ||
# (that must have a cached_urls method). | ||
# | ||
# Examples: | ||
# NotifyCacheJob.perform(InfoRequest.first) | ||
# NotifyCacheJob.perform(FoiAttachment.first) | ||
# NotifyCacheJob.perform(Comment.first) | ||
# | ||
class NotifyCacheJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(object) | ||
urls = object.cached_urls | ||
locales = [''] + AlaveteliLocalization.available_locales.map { |locale| | ||
'/' + locale | ||
} | ||
hosts = AlaveteliConfiguration.varnish_hosts | ||
locales.each do |locale| | ||
hosts.each do |host| | ||
Net::HTTP.start(AlaveteliConfiguration.domain, 80, host, 6081) do |http| | ||
urls.each do |url| | ||
if url.start_with? '^' | ||
request = Net::HTTP::Ban.new('/') | ||
request['X-Invalidate-Pattern'] = '^' + locale + url[1..-1] | ||
else | ||
request = Net::HTTP::Purge.new(locale + url) | ||
end | ||
response = http.request(request) | ||
result = response.code | ||
if result == "200" | ||
Rails.logger.debug( | ||
"PURGE: Purged URL #{url} at #{host}: #{result}" | ||
) | ||
else | ||
Rails.logger.warn( | ||
"PURGE: Unable to purge URL #{url} at #{host}: status #{result}" | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe NotifyCacheJob, type: :job do | ||
let(:args) { [] } | ||
subject(:perform) { described_class.new.perform(*args) } | ||
|
||
let(:request) { FactoryBot.build(:info_request) } | ||
let(:comment) { FactoryBot.build(:comment) } | ||
let(:attachment) { FactoryBot.build(:pdf_attachment) } | ||
let(:im) { FactoryBot.create(:plain_incoming_message) } | ||
|
||
before do | ||
@old_include_default_locale_in_urls = | ||
AlaveteliConfiguration.include_default_locale_in_urls | ||
AlaveteliLocalization.set_default_locale_urls(false) | ||
|
||
allow(AlaveteliConfiguration).to receive(:varnish_hosts). | ||
and_return(['varnish']) | ||
|
||
stub_request(:purge, /^http:\/\/test\.host(\/(en|es|fr|en_GB))?\/(|((feed\/)?body|(feed\/|details\/)?request|(feed\/)?user)\/[a-z0-9_]+|user\/[a-z0-9_]+\/wall)$/). | ||
to_return(status: 200, body: "", headers: {}) | ||
stub_request(:ban, 'http://test.host/'). | ||
with(headers: | ||
{ | ||
'X-Invalidate-Pattern' => | ||
/^\^(\/(en|es|fr|en_GB))?\/(list|feed\/list\/)$/ | ||
}). | ||
to_return(status: 200, body: "", headers: {}) | ||
end | ||
|
||
after do | ||
AlaveteliLocalization.set_default_locale_urls( | ||
@old_include_default_locale_in_urls | ||
) | ||
end | ||
|
||
context 'when called with a request' do | ||
let(:args) { [request] } | ||
it 'calls out to varnish correctly' do | ||
expect(Rails.logger).to receive(:debug).exactly(55).times | ||
perform | ||
end | ||
end | ||
|
||
context 'when called with a comment' do | ||
let(:args) { [comment] } | ||
it 'calls out to varnish correctly' do | ||
expect(Rails.logger).to receive(:debug).exactly(10).times | ||
perform | ||
end | ||
end | ||
|
||
context 'when called with an attachment' do | ||
let(:args) { [attachment] } | ||
it 'calls out to varnish correctly' do | ||
attachment.incoming_message = im | ||
expect(Rails.logger).to receive(:debug).exactly(5).times | ||
perform | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.