2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-05 11:39:08 +08:00
|
|
|
task 'redis:clean_up' => ['environment'] do
|
2021-11-10 23:53:55 +08:00
|
|
|
next unless Rails.configuration.multisite
|
2016-12-05 11:39:08 +08:00
|
|
|
|
|
|
|
dbs = RailsMultisite::ConnectionManagement.all_dbs
|
|
|
|
dbs << Discourse::SIDEKIQ_NAMESPACE
|
|
|
|
|
|
|
|
regexp = /((\$(?<message_bus>\w+)$)|(^?(?<namespace>\w+):))/
|
|
|
|
|
|
|
|
cursor = 0
|
2019-12-03 17:05:53 +08:00
|
|
|
redis = Discourse.redis.without_namespace
|
2016-12-05 11:39:08 +08:00
|
|
|
|
|
|
|
loop do
|
|
|
|
cursor, keys = redis.scan(cursor)
|
|
|
|
cursor = cursor.to_i
|
|
|
|
|
2022-05-10 06:19:02 +08:00
|
|
|
redis.multi do |transaction|
|
2016-12-05 11:39:08 +08:00
|
|
|
keys.each do |key|
|
|
|
|
if match = key.match(regexp)
|
|
|
|
db_name = match[:message_bus] || match[:namespace]
|
|
|
|
|
|
|
|
if !dbs.include?(db_name)
|
2022-05-10 06:19:02 +08:00
|
|
|
transaction.del(key)
|
2016-12-05 11:39:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
break if cursor == 0
|
|
|
|
end
|
|
|
|
end
|