mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
purge cache pubsub implementation
This commit is contained in:
parent
b786df44d1
commit
94b3fd6903
4 changed files with 39 additions and 0 deletions
7
app.rb
7
app.rb
|
@ -545,6 +545,9 @@ post '/change_name' do
|
|||
redirect '/settings'
|
||||
end
|
||||
|
||||
old_host = current_site.host
|
||||
old_file_paths = current_site.file_list.collect {|f| f.filename}
|
||||
|
||||
current_site.username = params[:name]
|
||||
|
||||
if current_site.valid?
|
||||
|
@ -553,6 +556,10 @@ post '/change_name' do
|
|||
current_site.move_files_from old_username
|
||||
}
|
||||
|
||||
old_file_paths.each do |file_path|
|
||||
PurgeCacheWorker.async_queue "#{old_host}/#{file_path}"
|
||||
end
|
||||
|
||||
flash[:success] = "Site/user name has been changed. You will need to use this name to login, <b>don't forget it</b>."
|
||||
redirect '/settings'
|
||||
else
|
||||
|
|
|
@ -50,6 +50,14 @@ Sidekiq.configure_client do |config|
|
|||
config.redis = sidekiq_redis_config
|
||||
end
|
||||
|
||||
if $config['pubsub_url']
|
||||
$pubsub = Redis.new url: $config['pubsub_url']
|
||||
end
|
||||
|
||||
if $config['pubsub_url'].nil? && ENV['RACK_ENV'] == 'production'
|
||||
raise 'pubsub_url is missing from config'
|
||||
end
|
||||
|
||||
require File.join(DIR_ROOT, 'workers', 'thumbnail_worker.rb')
|
||||
require File.join(DIR_ROOT, 'workers', 'screenshot_worker.rb')
|
||||
require File.join(DIR_ROOT, 'workers', 'email_worker.rb')
|
||||
|
|
|
@ -184,6 +184,7 @@ class Site < Sequel::Model
|
|||
|
||||
%w{index not_found}.each do |name|
|
||||
File.write file_path("#{name}.html"), render_template("#{name}.erb")
|
||||
PurgeCacheWorker.perform_async "#{host}/#{name}.html"
|
||||
ScreenshotWorker.perform_async values[:username], "#{name}.html"
|
||||
end
|
||||
|
||||
|
@ -205,6 +206,10 @@ class Site < Sequel::Model
|
|||
self.updated_at = Time.now
|
||||
save(validate: false)
|
||||
}
|
||||
|
||||
site_files.file_list.collect {|f| f.filename}.each do |f|
|
||||
PurgeCacheWorker.async_queue "#{host}/#{f}"
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
|
@ -279,6 +284,8 @@ class Site < Sequel::Model
|
|||
FileUtils.mv uploaded.path, file_path(filename)
|
||||
File.chmod(0640, file_path(filename))
|
||||
|
||||
PurgeCacheWorker.perform_async "#{host}/#{filename}"
|
||||
|
||||
ext = File.extname(filename).gsub(/^./, '')
|
||||
|
||||
if ext.match HTML_REGEX
|
||||
|
@ -324,6 +331,8 @@ class Site < Sequel::Model
|
|||
rescue Errno::ENOENT
|
||||
end
|
||||
|
||||
PurgeCacheWorker.perform_async "#{host}/#{filename}"
|
||||
|
||||
ext = File.extname(filename).gsub(/^./, '')
|
||||
|
||||
screenshots_delete(filename) if ext.match HTML_REGEX
|
||||
|
@ -340,6 +349,7 @@ class Site < Sequel::Model
|
|||
|
||||
def install_new_html_file(name)
|
||||
File.write file_path(name), render_template('index.erb')
|
||||
PurgeCacheWorker.perform_async "#{host}/#{name}"
|
||||
end
|
||||
|
||||
def file_exists?(filename)
|
||||
|
|
14
workers/purge_cache_worker.rb
Normal file
14
workers/purge_cache_worker.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
class PurgeCacheWorker
|
||||
include Sidekiq::Worker
|
||||
sidekiq_options queue: :purgecache, retry: 10, backtrace: true
|
||||
|
||||
def perform(url)
|
||||
begin
|
||||
$pubsub.publish 'purge', url
|
||||
rescue Redis::BaseConnectionError => error
|
||||
puts "Pubsub error: #{error}, retrying in 1s"
|
||||
sleep 1
|
||||
retry
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue