Skip to content

Commit

Permalink
fixup! Add ability to ask external caches to invalidate.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Sep 14, 2023
1 parent cb5ec47 commit fcbd7ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
54 changes: 28 additions & 26 deletions app/jobs/notify_cache_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,35 @@ class NotifyCacheJob < ApplicationJob

def perform(object)
urls = object.cached_urls
locales = [''] + AlaveteliLocalization.available_locales.map { |locale|
'/' + locale
}
locales = [''] + AlaveteliLocalization.available_locales.map { "/#{_1}" }
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

urls.product(locales, hosts).each do |url, locale, host|
if url.start_with? '^'
expire_url(host, '^' + locale + url[1..-1])
else
expire_url(host, locale + url)
end
end
end

def expire_url(host, url)
Net::HTTP.start(AlaveteliConfiguration.domain, 80, host, 6081) do |http|
if url.start_with? '^'
request = Net::HTTP::Ban.new('/')
request['X-Invalidate-Pattern'] = url
else
request = Net::HTTP::Purge.new(url)
end

response = http.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
end
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/notify_cache_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
AlaveteliLocalization.set_default_locale_urls(false)

allow(AlaveteliConfiguration).to receive(:varnish_hosts).
and_return(['varnish' ])
and_return(['varnish'])

stub_request(:purge, /^http:\/\/test\.host(\/(en|es|fr|en_GB))?\/(|body|((feed\/)?body|(feed\/|details\/)?request|(feed\/)?user)\/[a-z0-9_]+|user\/[a-z0-9_]+\/wall)$/).
to_return(status: 200, body: "", headers: {})
Expand Down

0 comments on commit fcbd7ea

Please sign in to comment.