Add rake task to clean up orphane Redis keys when a multisite has been removed.

This commit is contained in:
Guo Xiang Tan 2016-12-05 11:39:08 +08:00
parent 96183dbf6b
commit ce36f54dcd
1 changed files with 30 additions and 0 deletions

30
lib/tasks/redis.rake Normal file
View File

@ -0,0 +1,30 @@
task 'redis:clean_up' => ['environment'] do
return unless Rails.configuration.multisite
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