mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Add Rake task to clean up unused multisite Redis keys.
This commit is contained in:
parent
ce36f54dcd
commit
22059d4df9
37
app/jobs/scheduled/clean_up_redis_keys.rb
Normal file
37
app/jobs/scheduled/clean_up_redis_keys.rb
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
module Jobs
|
||||||
|
class CleanUpRedisKeys < Jobs::Scheduled
|
||||||
|
every 1.week
|
||||||
|
|
||||||
|
def execute(args)
|
||||||
|
return unless Rails.configuration.multisite
|
||||||
|
return unless SiteSetting.clean_up_redis_keys
|
||||||
|
|
||||||
|
dbs = RailsMultisite::ConnectionManagement.all_dbs
|
||||||
|
dbs << Discourse::SIDEKIQ_NAMESPACE
|
||||||
|
|
||||||
|
regexp = /((\$(?<message_bus>\w+)$)|(^?(?<namespace>\w+):))/
|
||||||
|
|
||||||
|
cursor = 0
|
||||||
|
redis = $redis.without_namespace
|
||||||
|
|
||||||
|
loop do
|
||||||
|
cursor, keys = redis.scan(cursor)
|
||||||
|
cursor = cursor.to_i
|
||||||
|
|
||||||
|
redis.multi do
|
||||||
|
keys.each do |key|
|
||||||
|
if match = key.match(regexp)
|
||||||
|
db_name = match[:message_bus] || match[:namespace]
|
||||||
|
|
||||||
|
if !dbs.include?(db_name)
|
||||||
|
redis.del(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
break if cursor == 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -371,9 +371,11 @@ module Discourse
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
SIDEKIQ_NAMESPACE ||= 'sidekiq'.freeze
|
||||||
|
|
||||||
def self.sidekiq_redis_config
|
def self.sidekiq_redis_config
|
||||||
conf = GlobalSetting.redis_config.dup
|
conf = GlobalSetting.redis_config.dup
|
||||||
conf[:namespace] = 'sidekiq'
|
conf[:namespace] = SIDEKIQ_NAMESPACE
|
||||||
conf
|
conf
|
||||||
end
|
end
|
||||||
|
|
||||||
|
45
spec/tasks/redis_spec.rb
Normal file
45
spec/tasks/redis_spec.rb
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe "Redis rake tasks" do
|
||||||
|
let(:redis) { $redis.without_namespace }
|
||||||
|
|
||||||
|
before do
|
||||||
|
@multisite = Rails.configuration.multisite
|
||||||
|
Rails.configuration.multisite = true
|
||||||
|
Discourse::Application.load_tasks
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Rails.configuration.multisite = @multisite
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'clean up' do
|
||||||
|
it 'should clean up orphan Redis keys' do
|
||||||
|
active_keys = [
|
||||||
|
'__mb_backlog_id_n_/users/someusername$|$default',
|
||||||
|
'default:user-last-seen:607',
|
||||||
|
'sidekiq:something:do:something',
|
||||||
|
'somekeytonotbetouched'
|
||||||
|
]
|
||||||
|
|
||||||
|
orphan_keys = [
|
||||||
|
'tgxworld:user-last-seen:607',
|
||||||
|
'__mb_backlog_id_n_/users/someusername$|$tgxworld'
|
||||||
|
]
|
||||||
|
|
||||||
|
(active_keys | orphan_keys).each do |key|
|
||||||
|
redis.set(key, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
Rake::Task['redis:clean_up'].invoke
|
||||||
|
|
||||||
|
active_keys.each do |key|
|
||||||
|
expect(redis.get(key)).to eq('1')
|
||||||
|
end
|
||||||
|
|
||||||
|
orphan_keys.each do |key|
|
||||||
|
expect(redis.get(key)).to eq(nil)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user