-
-
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 either when an event is logged, via log_event, or when the censor rules change, or when a body or user is updated. The entries are fixed paths to be purged, or regexes 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
21 changed files
with
407 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,68 @@ | ||
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 | ||
|
||
around_enqueue do |_, block| | ||
block.call if AlaveteliConfiguration.varnish_hosts.present? | ||
end | ||
|
||
def perform(object) | ||
urls = object.cached_urls | ||
locales = [''] + AlaveteliLocalization.available_locales.map { "/#{_1}" } | ||
hosts = AlaveteliConfiguration.varnish_hosts | ||
|
||
urls.product(locales, hosts).each do |url, locale, host| | ||
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 = connection_for_host(host).request(request) | ||
log_result = "#{request.method} #{url} at #{host}: #{response.code}" | ||
|
||
case response | ||
when Net::HTTPSuccess | ||
Rails.logger.debug('NotifyCacheJob: ' + log_result) | ||
else | ||
Rails.logger.warn('NotifyCacheJob: Unable to ' + log_result) | ||
end | ||
end | ||
|
||
ensure | ||
close_connections | ||
end | ||
|
||
def connections | ||
@connections ||= {} | ||
end | ||
|
||
def connection_for_host(host) | ||
connections[host] ||= Net::HTTP.start( | ||
AlaveteliConfiguration.domain, 80, host, 6081 | ||
) | ||
end | ||
|
||
def close_connections | ||
connections.values.each { _1.finish if _1.started? } | ||
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
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.