-
-
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. TODO: www.whatdotheyknow.com is currently hard-coded in the job.
- Loading branch information
Showing
9 changed files
with
132 additions
and
0 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,50 @@ | ||
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 } | ||
Check warning on line 25 in app/jobs/notify_cache_job.rb GitHub Actions / build
|
||
hosts = AlaveteliConfiguration.varnish_hosts | ||
locales.each do |locale| | ||
Check warning on line 27 in app/jobs/notify_cache_job.rb GitHub Actions / build
|
||
hosts.each do |host| | ||
Net::HTTP.start('www.whatdotheyknow.com', 80, host, 6081) do |http| | ||
urls.each do |url| | ||
if url.include? '^' | ||
request = Net::HTTP::Ban.new(url) | ||
else | ||
request = Net::HTTP::Purge.new(url) | ||
end | ||
response = http.request(request) | ||
result = response.code | ||
if result == "200" | ||
Rails.logger.debug("PURGE: Purged URL #{url} at #{host}: #{result}") | ||
Check warning on line 39 in app/jobs/notify_cache_job.rb GitHub Actions / build
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -673,6 +673,12 @@ def flipper_id | |
"User;#{id}" | ||
end | ||
|
||
def cached_urls | ||
[ | ||
user_path(self) | ||
] | ||
end | ||
|
||
private | ||
|
||
def set_defaults | ||
|
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