Skip to content

Commit

Permalink
Add rake task to remove Redis namespacing
Browse files Browse the repository at this point in the history
In Sidekiq 7, `redis_namespace` is no longer supported. We therefore
need to run a migration at the point of upgrading to remove the
namespace from existing Redis keys.

If we don't run the migration, already queued jobs won't get processed.
  • Loading branch information
brucebolt committed Sep 24, 2024
1 parent a89c0b9 commit 749f82b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Switch from using `redis` gem to `redis-client`
* BREAKING: Remove `redis-namespace` dependency and support for Redis namespaces
* Run the `redis_namespace:remove_namespace` rake task immediately after upgrading to to this version, to retain existing queued jobs.
* BREAKING: Upgrade Sidekiq to version 7.0, follow these steps to upgrade:
1. `Sidekiq::Worker` has been deprecated in Sidekiq 7. Replace all instances of `Sidekiq::Worker` with `Sidekiq::Job`, then rename/move your workers to be `app/sidekiq/MyJob.rb` instead of `app/workers/MyWorker.rb`.
1. Remove the requirement for Sidekiq strict arguments from `config/initializers/sidekiq.rb`. This was added to include Sidekiq 7 strict arguments behaviour in Sidekiq 6, but is no longer needed to be explictly required, since this is now the default behaviour.
Expand Down
5 changes: 5 additions & 0 deletions lib/govuk_sidekiq/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ class Railtie < Rails::Railtie
{ url: ENV.fetch("REDIS_URL", "redis://127.0.0.1:6379") },
)
end

rake_tasks do
path = File.expand_path(__dir__)
Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
end
end
end
15 changes: 15 additions & 0 deletions lib/govuk_sidekiq/tasks/redis_namespace.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace :redis_namespace do
desc "Remove application specific namespacing for all Redis keys"
task :remove_namespace, :environment do
namespace = ENV["GOVUK_APP_NAME"]

redis = RedisClient.new(url: ENV["REDIS_URL"])

namespaced_keys = redis.call("KEYS", "#{namespace}:*")

namespaced_keys.each do |key|
new_key = key.gsub(/^#{namespace}:/, "")
redis.call("RENAME", key, new_key)
end
end
end

0 comments on commit 749f82b

Please sign in to comment.