Add Rake task to clean up unused multisite Redis keys.

This commit is contained in:
Guo Xiang Tan 2016-12-05 11:46:34 +08:00
parent ce36f54dcd
commit 22059d4df9
3 changed files with 85 additions and 1 deletions

View 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

View File

@ -371,9 +371,11 @@ module Discourse
end
end
SIDEKIQ_NAMESPACE ||= 'sidekiq'.freeze
def self.sidekiq_redis_config
conf = GlobalSetting.redis_config.dup
conf[:namespace] = 'sidekiq'
conf[:namespace] = SIDEKIQ_NAMESPACE
conf
end

45
spec/tasks/redis_spec.rb Normal file
View 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