Skip to content

Commit

Permalink
wip cache banning
Browse files Browse the repository at this point in the history
No expectation this works like this, is basically pseudo-code in parts.
  • Loading branch information
dracos committed Sep 7, 2023
1 parent 9c2d9b6 commit 027bff0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app/jobs/notify_cache_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
##
# 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)

require 'net/http'

class Net::HTTP::Purge < Net::HTTP::Get
METHOD = 'PURGE'
end

class Net::HTTP::Ban < Net::HTTP::Get
METHOD = 'BAN'
end

class NotifyCacheJob < ApplicationJob

Check warning on line 20 in app/jobs/notify_cache_job.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Style/Documentation: Missing top-level documentation comment for class NotifyCacheJob. Raw Output: app/jobs/notify_cache_job.rb:20:1: C: Style/Documentation: Missing top-level documentation comment for class NotifyCacheJob. class NotifyCacheJob < ApplicationJob ^^^^^^^^^^^^^^^^^^^^
queue_as :default

def perform(object, method = nil)

Check warning on line 23 in app/jobs/notify_cache_job.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 [Correctable] Lint/UnusedMethodArgument: Unused method argument - method. If it's necessary, use _ or _method as an argument name to indicate that it won't be used. If it's unnecessary, remove it. (https://rubystyle.guide#underscore-unused-vars) Raw Output: app/jobs/notify_cache_job.rb:23:23: W: [Correctable] Lint/UnusedMethodArgument: Unused method argument - method. If it's necessary, use _ or _method as an argument name to indicate that it won't be used. If it's unnecessary, remove it. (https://rubystyle.guide#underscore-unused-vars) def perform(object, method = nil) ^^^^^^
urls = object.cached_urls
hosts = AlaveteliConfiguration.varnish_hosts
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}")
else
Rails.logger.warn("PURGE: Unable to purge URL #{url} at #{host}: status #{result}")

Check warning on line 39 in app/jobs/notify_cache_job.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Layout/LineLength: Line is too long. [95/80] (https://rubystyle.guide#max-line-length) Raw Output: app/jobs/notify_cache_job.rb:39:81: C: Layout/LineLength: Line is too long. [95/80] (https://rubystyle.guide#max-line-length) Rails.logger.warn("PURGE: Unable to purge URL #{url} at #{host}: status #{result}") ^^^^^^^^^^^^^^^
end
end
end
end
end
end
1 change: 1 addition & 0 deletions app/models/censor_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def is_global?
def expire_requests
if info_request
InfoRequestExpireJob.perform_later(info_request)
NotifyCacheJob.perform_later(info_request)
elsif user
InfoRequestExpireJob.perform_later(user, :info_requests)
elsif public_body
Expand Down
7 changes: 7 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,11 @@ def check_body_uses_mixed_capitals
errors.add(:body, msg)
end
end

def cached_urls
[
request_path(info_request),
show_user_wall_path(url_name: user.url_name),

Check warning on line 215 in app/models/comment.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 [Correctable] Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array. (https://rubystyle.guide#no-trailing-array-commas) Raw Output: app/models/comment.rb:215:51: C: [Correctable] Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array. (https://rubystyle.guide#no-trailing-array-commas) show_user_wall_path(url_name: user.url_name), ^
]
end
end
6 changes: 6 additions & 0 deletions app/models/foi_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,10 @@ def body_as_html(dir, opts = {})
def text_type?
AlaveteliTextMasker::TextMask.include?(content_type)
end

def cached_urls
[
request_path(incoming_message.info_request),

Check warning on line 329 in app/models/foi_attachment.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 [Correctable] Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array. (https://rubystyle.guide#no-trailing-array-commas) Raw Output: app/models/foi_attachment.rb:329:50: C: [Correctable] Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array. (https://rubystyle.guide#no-trailing-array-commas) request_path(incoming_message.info_request), ^
]
end
end
27 changes: 27 additions & 0 deletions app/models/info_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,15 @@ def log_event(type, params, options = {})
if !last_event_time || (event.created_at > last_event_time)
update_column(:last_event_time, event.created_at)
end
if AlaveteliConfiguration.varnish_hosts.present?
if type in ('comment', 'edit_comment', 'hide_comment')

Check warning on line 1167 in app/models/info_request.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Lint/Syntax: unexpected token tCOMMA Raw Output: app/models/info_request.rb:1167:28: F: Lint/Syntax: unexpected token tCOMMA (Using Ruby 3.0 parser; configure using TargetRubyVersion parameter, under AllCops) if type in ('comment', 'edit_comment', 'hide_comment') ^
NotifyCacheJob.perform_later(params.comment)
elsif type in ('edit_attachment')
NotifyCacheJob.perform_later(params.attachment)
else
NotifyCacheJob.perform_later(self)
end
end
event
end

Expand Down Expand Up @@ -1904,4 +1913,22 @@ def reindexable_attribute_changed?
saved_change_to_attribute?(attr)
end
end

def cached_urls
[
'/',
public_body_path(self.body),
request_path(self),
request_details_path(self),
'^/list*',
do_track_path({ track_type='request_updates', info_request=self }, feed='feed'),

Check warning on line 1924 in app/models/info_request.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Lint/Syntax: unexpected token tCOMMA Raw Output: app/models/info_request.rb:1924:51: F: Lint/Syntax: unexpected token tCOMMA (Using Ruby 3.0 parser; configure using TargetRubyVersion parameter, under AllCops) do_track_path({ track_type='request_updates', info_request=self }, feed='feed'), ^

Check warning on line 1924 in app/models/info_request.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Lint/Syntax: unexpected token tRCURLY Raw Output: app/models/info_request.rb:1924:71: F: Lint/Syntax: unexpected token tRCURLY (Using Ruby 3.0 parser; configure using TargetRubyVersion parameter, under AllCops) do_track_path({ track_type='request_updates', info_request=self }, feed='feed'), ^

Check warning on line 1924 in app/models/info_request.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Lint/Syntax: unexpected token tRPAREN Raw Output: app/models/info_request.rb:1924:85: F: Lint/Syntax: unexpected token tRPAREN (Using Ruby 3.0 parser; configure using TargetRubyVersion parameter, under AllCops) do_track_path({ track_type='request_updates', info_request=self }, feed='feed'), ^
'^/feed/list/*',
do_track_path({ track_type='public_body_updates', public_body=public_body }, feed='feed'),

Check warning on line 1926 in app/models/info_request.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Lint/Syntax: unexpected token tCOMMA Raw Output: app/models/info_request.rb:1926:55: F: Lint/Syntax: unexpected token tCOMMA (Using Ruby 3.0 parser; configure using TargetRubyVersion parameter, under AllCops) do_track_path({ track_type='public_body_updates', public_body=public_body }, feed='feed'), ^
do_track_path({ track_type='user_updates', tracked_user=user }, feed='feed'),
user_path(user),
show_user_wall_path(url_name: user.url_name),
public_body_path(self),
'^/body/list',
]
end
end
8 changes: 8 additions & 0 deletions app/models/public_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,12 @@ def update_missing_email_tag
def missing_email?
!has_request_email?
end

def cached_urls
[
public_body_path(self),
list_public_bodies_path,
'^/body/list',
]
end
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,10 @@ def update_pro_account
def content_limit(content)
content_limits[content]
end

def cached_urls
[
user_path(self),
]
end
end
14 changes: 14 additions & 0 deletions config/general.yml-example
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,20 @@ EXCEPTION_NOTIFICATIONS_TO:
# ---
MAX_REQUESTS_PER_USER_PER_DAY: 6

# If you're running behind Varnish set this to work out where to send purge
# requests. Otherwise, don't set it.
#
# VARNISH_HOSTS - Array of Strings (default: nil)
#
# Examples:
#
# VARNISH_HOSTS:
# - host1
# - host2
#
# ---
VARNISH_HOSTS: null

# Adding a value here will enable Google Analytics on all non-admin pages for
# non-admin users.
#
Expand Down

0 comments on commit 027bff0

Please sign in to comment.