mirror of
https://github.com/neocities/neocities.git
synced 2025-04-25 09:42:36 +02:00
purge for delete and purge for refreshing
This commit is contained in:
parent
12a543d2aa
commit
985a2f8b60
7 changed files with 184 additions and 3 deletions
|
@ -196,7 +196,7 @@ post '/settings/:username/change_name' do
|
||||||
}
|
}
|
||||||
|
|
||||||
old_file_paths.each do |file_path|
|
old_file_paths.each do |file_path|
|
||||||
@site.purge_cache file_path
|
@site.delete_cache file_path
|
||||||
end
|
end
|
||||||
|
|
||||||
flash[:success] = "Site/user name has been changed. You will need to use this name to login, <b>don't forget it</b>."
|
flash[:success] = "Site/user name has been changed. You will need to use this name to login, <b>don't forget it</b>."
|
||||||
|
|
|
@ -458,7 +458,7 @@ class Site < Sequel::Model
|
||||||
}
|
}
|
||||||
|
|
||||||
file_list.each do |path|
|
file_list.each do |path|
|
||||||
purge_cache path
|
delete_cache path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -601,6 +601,22 @@ class Site < Sequel::Model
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# TODO DRY this up
|
||||||
|
|
||||||
|
def delete_cache(path)
|
||||||
|
relative_path = path.gsub base_files_path, ''
|
||||||
|
|
||||||
|
DeleteCacheOrderWorker.perform_async username, relative_path
|
||||||
|
|
||||||
|
# We gotta flush the dirname too if it's an index file.
|
||||||
|
if relative_path != '' && relative_path.match(/\/$|index\.html?$/i)
|
||||||
|
purge_file_path = Pathname(relative_path).dirname.to_s
|
||||||
|
|
||||||
|
DeleteCacheOrderWorker.perform_async username, '/?surf=1' if purge_file_path == '/'
|
||||||
|
DeleteCacheOrderWorker.perform_async username, purge_file_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Rye::Cmd.add_command :ipfs, nil, 'add', :r
|
Rye::Cmd.add_command :ipfs, nil, 'add', :r
|
||||||
|
|
||||||
def add_to_ipfs
|
def add_to_ipfs
|
||||||
|
@ -1182,7 +1198,7 @@ class Site < Sequel::Model
|
||||||
rescue Errno::ENOENT
|
rescue Errno::ENOENT
|
||||||
end
|
end
|
||||||
|
|
||||||
purge_cache path
|
delete_cache path
|
||||||
|
|
||||||
ext = File.extname(path).gsub(/^./, '')
|
ext = File.extname(path).gsub(/^./, '')
|
||||||
|
|
||||||
|
|
|
@ -24,16 +24,39 @@ describe 'site_files' do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'delete' do
|
describe 'delete' do
|
||||||
|
before do
|
||||||
|
DeleteCacheWorker.jobs.clear
|
||||||
|
DeleteCacheOrderWorker.jobs.clear
|
||||||
|
end
|
||||||
|
|
||||||
it 'works' do
|
it 'works' do
|
||||||
uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
|
uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
|
||||||
upload 'files[]' => uploaded_file
|
upload 'files[]' => uploaded_file
|
||||||
|
|
||||||
|
PurgeCacheOrderWorker.jobs.clear
|
||||||
|
|
||||||
@site.reload.space_used.must_equal uploaded_file.size
|
@site.reload.space_used.must_equal uploaded_file.size
|
||||||
file_path = @site.files_path 'test.jpg'
|
file_path = @site.files_path 'test.jpg'
|
||||||
File.exists?(file_path).must_equal true
|
File.exists?(file_path).must_equal true
|
||||||
delete_file filename: 'test.jpg'
|
delete_file filename: 'test.jpg'
|
||||||
|
|
||||||
File.exists?(file_path).must_equal false
|
File.exists?(file_path).must_equal false
|
||||||
SiteFile[site_id: @site.id, path: 'test.jpg'].must_be_nil
|
SiteFile[site_id: @site.id, path: 'test.jpg'].must_be_nil
|
||||||
@site.reload.space_used.must_equal 0
|
@site.reload.space_used.must_equal 0
|
||||||
|
|
||||||
|
PurgeCacheOrderWorker.jobs.length.must_equal 0
|
||||||
|
DeleteCacheOrderWorker.jobs.length.must_equal 1
|
||||||
|
args = DeleteCacheOrderWorker.jobs.first['args']
|
||||||
|
args[0].must_equal @site.username
|
||||||
|
args[1].must_equal 'test.jpg'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'flushes surf for index.html' do
|
||||||
|
uploaded_file = Rack::Test::UploadedFile.new('./tests/files/index.html', 'text/html')
|
||||||
|
upload 'files[]' => uploaded_file
|
||||||
|
delete_file filename: '/index.html'
|
||||||
|
DeleteCacheOrderWorker.jobs.length.must_equal 3
|
||||||
|
DeleteCacheOrderWorker.jobs.collect {|j| j['args'].last}.must_equal ['/index.html', '/?surf=1', '/']
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'deletes a directory and all files in it' do
|
it 'deletes a directory and all files in it' do
|
||||||
|
|
21
tests/workers/delete_cache_order_worker_tests.rb
Normal file
21
tests/workers/delete_cache_order_worker_tests.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
require_relative '../environment.rb'
|
||||||
|
|
||||||
|
describe DeleteCacheWorker do
|
||||||
|
before do
|
||||||
|
PurgeCacheOrderWorker.jobs.clear
|
||||||
|
PurgeCacheWorker.jobs.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'queues up purges' do
|
||||||
|
DeleteCacheOrderWorker.new.perform('kyledrake', '/test.jpg')
|
||||||
|
|
||||||
|
job_one_args = DeleteCacheWorker.jobs.first['args']
|
||||||
|
job_two_args = DeleteCacheWorker.jobs.last['args']
|
||||||
|
job_one_args[0].must_equal '10.0.0.1'
|
||||||
|
job_one_args[1].must_equal 'kyledrake'
|
||||||
|
job_one_args[2].must_equal '/test.jpg'
|
||||||
|
job_two_args[0].must_equal '10.0.0.2'
|
||||||
|
job_two_args[1].must_equal 'kyledrake'
|
||||||
|
job_two_args[2].must_equal '/test.jpg'
|
||||||
|
end
|
||||||
|
end
|
64
tests/workers/delete_cache_worker_tests.rb
Normal file
64
tests/workers/delete_cache_worker_tests.rb
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
require_relative '../environment.rb'
|
||||||
|
|
||||||
|
describe DeleteCacheWorker do
|
||||||
|
before do
|
||||||
|
@test_ip = '10.0.0.1'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'throws exception without 200 or 404 http status' do
|
||||||
|
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg").
|
||||||
|
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||||
|
.to_return(status: 503)
|
||||||
|
|
||||||
|
worker = DeleteCacheWorker.new
|
||||||
|
|
||||||
|
proc {
|
||||||
|
worker.perform @test_ip, 'kyledrake', '/test.jpg'
|
||||||
|
}.must_raise RestClient::ServiceUnavailable
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles 404 without exception' do
|
||||||
|
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg").
|
||||||
|
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||||
|
.to_return(status: 404)
|
||||||
|
|
||||||
|
worker = DeleteCacheWorker.new
|
||||||
|
worker.perform @test_ip, 'kyledrake', '/test.jpg'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sends a purge request' do
|
||||||
|
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg").
|
||||||
|
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||||
|
.to_return(status: 200)
|
||||||
|
|
||||||
|
worker = DeleteCacheWorker.new
|
||||||
|
worker.perform @test_ip, 'kyledrake', '/test.jpg'
|
||||||
|
|
||||||
|
assert_requested :get, "http://#{@test_ip}/:cache/purge/test.jpg"
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles spaces correctly' do
|
||||||
|
stub_request(:get, "http://#{@test_ip}/:cache/purge/te st.jpg").
|
||||||
|
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||||
|
.to_return(status: 200)
|
||||||
|
|
||||||
|
url = Addressable::URI.encode_component(
|
||||||
|
"http://#{@test_ip}/:cache/purge/te st.jpg",
|
||||||
|
Addressable::URI::CharacterClasses::QUERY
|
||||||
|
)
|
||||||
|
|
||||||
|
worker = DeleteCacheWorker.new
|
||||||
|
worker.perform @test_ip, 'kyledrake', '/te st.jpg'
|
||||||
|
|
||||||
|
assert_requested :get, url
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'works without forward slash' do
|
||||||
|
stub_request(:get, "http://#{@test_ip}/:cache/purge/test.jpg").
|
||||||
|
with(headers: {'Host' => 'kyledrake.neocities.org'})
|
||||||
|
.to_return(status: 200)
|
||||||
|
|
||||||
|
worker = DeleteCacheWorker.new
|
||||||
|
worker.perform @test_ip, 'kyledrake', 'test.jpg'
|
||||||
|
end
|
||||||
|
end
|
23
workers/delete_cache_order_worker.rb
Normal file
23
workers/delete_cache_order_worker.rb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
class DeleteCacheOrderWorker
|
||||||
|
include Sidekiq::Worker
|
||||||
|
sidekiq_options queue: :deletecacheorder, retry: 1000, backtrace: true, average_scheduled_poll_interval: 1
|
||||||
|
|
||||||
|
sidekiq_retry_in do |count|
|
||||||
|
return 10 if count < 10
|
||||||
|
180
|
||||||
|
end
|
||||||
|
|
||||||
|
RESOLVER = Dnsruby::Resolver.new
|
||||||
|
|
||||||
|
def perform(username, path)
|
||||||
|
if ENV['RACK_ENV'] == 'test'
|
||||||
|
proxy_ips = ['10.0.0.1', '10.0.0.2']
|
||||||
|
else
|
||||||
|
proxy_ips = RESOLVER.query($config['cache_purge_ips_uri']).answer.collect {|a| a.address.to_s}
|
||||||
|
end
|
||||||
|
|
||||||
|
proxy_ips.each do |proxy_ip|
|
||||||
|
DeleteCacheWorker.perform_async proxy_ip, username, path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
34
workers/delete_cache_worker.rb
Normal file
34
workers/delete_cache_worker.rb
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
require 'open-uri'
|
||||||
|
|
||||||
|
# PurgeCacheWorker refreshes the cache, this actually deletes it.
|
||||||
|
# This is because when the file is 404ing the PurgeCacheWorker
|
||||||
|
# will just sit on the stale cache, even though it's not supposed to.
|
||||||
|
# It's some nginx bug. I'm not going to deal with it.
|
||||||
|
|
||||||
|
class DeleteCacheWorker
|
||||||
|
HTTP_TIMEOUT = 5
|
||||||
|
include Sidekiq::Worker
|
||||||
|
sidekiq_options queue: :deletecache, retry: 1000, backtrace: false, average_scheduled_poll_interval: 1
|
||||||
|
|
||||||
|
sidekiq_retry_in do |count|
|
||||||
|
return 10 if count < 10
|
||||||
|
180
|
||||||
|
end
|
||||||
|
|
||||||
|
def perform(proxy_ip, username, path)
|
||||||
|
# Must always have a forward slash
|
||||||
|
path = '/' + path if path[0] != '/'
|
||||||
|
|
||||||
|
url = Addressable::URI.encode_component(
|
||||||
|
"http://#{proxy_ip}/:cache/purge#{path}",
|
||||||
|
Addressable::URI::CharacterClasses::QUERY
|
||||||
|
)
|
||||||
|
begin
|
||||||
|
RestClient::Request.execute method: :get, url: url, timeout: HTTP_TIMEOUT, headers: {
|
||||||
|
host: URI::encode("#{username}.neocities.org")
|
||||||
|
}
|
||||||
|
rescue RestClient::ResourceNotFound
|
||||||
|
rescue RestClient::Forbidden
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue