mirror of
https://github.com/neocities/neocities.git
synced 2025-04-25 01:32:36 +02:00
implement the cache purger
This commit is contained in:
parent
4bfaa32431
commit
3a6ca6c12b
5 changed files with 67 additions and 28 deletions
3
Gemfile
3
Gemfile
|
@ -29,7 +29,7 @@ gem 'rest-client'
|
|||
gem 'geoip'
|
||||
gem 'io-extra', require: 'io/extra'
|
||||
gem 'rye'
|
||||
gem 'timecop'
|
||||
gem 'dnsruby'
|
||||
|
||||
platform :mri, :rbx do
|
||||
gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic
|
||||
|
@ -76,6 +76,7 @@ group :test do
|
|||
gem 'rack_session_access', require: nil
|
||||
gem 'webmock', require: nil
|
||||
gem 'stripe-ruby-mock', '~> 2.0.1', require: 'stripe_mock'
|
||||
gem 'timecop'
|
||||
|
||||
platform :mri, :rbx do
|
||||
gem 'simplecov', require: nil
|
||||
|
|
|
@ -49,6 +49,7 @@ GEM
|
|||
rack (>= 1.1.0)
|
||||
uuidtools (~> 2.1.1)
|
||||
debugger-linecache (1.2.0)
|
||||
dnsruby (1.58.0)
|
||||
docile (1.1.3)
|
||||
domain_name (0.5.23)
|
||||
unf (>= 0.0.5, < 1.0.0)
|
||||
|
@ -261,6 +262,7 @@ DEPENDENCIES
|
|||
capybara_minitest_spec
|
||||
cocaine
|
||||
dav4rack
|
||||
dnsruby
|
||||
erubis
|
||||
fabrication
|
||||
faker
|
||||
|
|
|
@ -75,18 +75,6 @@ if ENV['RACK_ENV'] == 'development'
|
|||
end
|
||||
# :nocov:
|
||||
|
||||
# :nocov:
|
||||
if $config['pubsub_url']
|
||||
$pubsub_pool = ConnectionPool.new(size: 10, timeout: 5) {
|
||||
Redis.new url: $config['pubsub_url']
|
||||
}
|
||||
end
|
||||
|
||||
if $config['pubsub_url'].nil? && ENV['RACK_ENV'] == 'production'
|
||||
raise 'pubsub_url is missing from config'
|
||||
end
|
||||
# :nocov:
|
||||
|
||||
Sequel.datetime_class = Time
|
||||
Sequel.extension :core_extensions
|
||||
Sequel.extension :migration
|
||||
|
|
47
tests/workers/purge_cache_worker_tests.rb
Normal file
47
tests/workers/purge_cache_worker_tests.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require_relative '../environment.rb'
|
||||
|
||||
describe PurgeCacheWorker do
|
||||
before do
|
||||
@test_ips = ['10.0.0.1', '10.0.0.2']
|
||||
end
|
||||
|
||||
it 'throws exception without 200 or 404 http status' do
|
||||
@test_ips.each do |ip|
|
||||
stub_request(:get, "https://#{ip}/:cache/purgetest.jpg").
|
||||
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||
.to_return(status: 503)
|
||||
end
|
||||
|
||||
worker = PurgeCacheWorker.new
|
||||
|
||||
proc {
|
||||
worker.perform 'kyledrake', 'test.jpg'
|
||||
}.must_raise RestClient::ServiceUnavailable
|
||||
end
|
||||
|
||||
it 'handles 404 without exception' do
|
||||
@test_ips.each do |ip|
|
||||
stub_request(:get, "https://#{ip}/:cache/purgetest.jpg").
|
||||
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||
.to_return(status: 404)
|
||||
end
|
||||
|
||||
worker = PurgeCacheWorker.new
|
||||
worker.perform 'kyledrake', 'test.jpg'
|
||||
end
|
||||
|
||||
it 'sends a purge to each dns ip' do
|
||||
@test_ips.each do |ip|
|
||||
stub_request(:get, "https://#{ip}/:cache/purgetest.jpg").
|
||||
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||
.to_return(status: 200)
|
||||
end
|
||||
|
||||
worker = PurgeCacheWorker.new
|
||||
worker.perform 'kyledrake', 'test.jpg'
|
||||
|
||||
@test_ips.each do |ip|
|
||||
assert_requested :get, "https://#{ip}/:cache/purgetest.jpg"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,21 +2,22 @@ class PurgeCacheWorker
|
|||
include Sidekiq::Worker
|
||||
sidekiq_options queue: :purgecache, retry: 10, backtrace: true
|
||||
|
||||
def perform(subdomain, path)
|
||||
res = Dnsruby::Resolver.new
|
||||
|
||||
if ENV['RACK_ENV'] == 'test'
|
||||
proxy_ips = ['10.0.0.1', '10.0.0.2']
|
||||
else
|
||||
proxy_ips = res.query($config['cache_purge_ips_uri']).answer.collect {|a| a.address.to_s}
|
||||
end
|
||||
|
||||
proxy_ips.each do |proxy_ip|
|
||||
url = "http://#{proxy_ip}/:cache/purge#{path}"
|
||||
|
||||
def perform(payload)
|
||||
# :nocov:
|
||||
attempt = 0
|
||||
begin
|
||||
attempt += 1
|
||||
$pubsub_pool.with do |redis|
|
||||
redis.publish 'purgecache', payload.to_json
|
||||
end
|
||||
rescue Redis::BaseConnectionError => error
|
||||
raise if attempt > 3
|
||||
puts "pubsub error: #{error}, retrying in 1s"
|
||||
sleep 1
|
||||
retry
|
||||
end
|
||||
# :nocov:
|
||||
RestClient.get(url, host: "#{subdomain}.neocities.org")
|
||||
rescue RestClient::ResourceNotFound
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue