Skip to content

Commit

Permalink
Upgrade Sidekiq (#684)
Browse files Browse the repository at this point in the history
* Remove redis gem and resque pool.  Upgrade sidekiq (adds redis client).  Update redis in docker compose.  Modify sidekiq initializer to not use namespaces.

* Uncomment

* Adds rake task to remove namespaces

* Adds comments

* Update readme and upgrade redis version in docker compose file

* Update readme

* Removes old redis migration script
  • Loading branch information
ajkiessl authored Aug 2, 2023
1 parent c04cc9f commit d095a22
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 88 deletions.
9 changes: 1 addition & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,8 @@ gem 'puma', ">= 4.3.0"
gem 'shakapacker', '~> 7.0.2'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.5.0'
# Resque pool
gem 'resque-pool', '~> 0.7.0'
# SideKiq for queueing jobs
gem 'sidekiq', '~> 5.2.10'
# When downgrading Sidekiq, rack needed to be downgraded as well.
# This can be removed for Sidekiq 6 or greater.
gem 'rack', '~> 2.2.6.3'
gem 'sidekiq', '~> 7.1.2'
# Use SCSS for stylesheets
gem 'sassc-rails', '~> 2.1.0'
# Jquery for rails
Expand Down
41 changes: 9 additions & 32 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ GEM
clamby (1.6.8)
cocoon (1.2.15)
concurrent-ruby (1.2.2)
connection_pool (2.3.0)
connection_pool (2.4.1)
countries (5.3.1)
unaccent (~> 0.3)
crack (0.4.5)
Expand Down Expand Up @@ -195,12 +195,8 @@ GEM
mini_mime (1.1.2)
mini_portile2 (2.8.2)
minitest (5.18.0)
mono_logger (1.1.1)
msgpack (1.6.1)
multi_json (1.15.0)
multi_xml (0.6.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
mysql2 (0.5.5)
net-imap (0.3.4)
date
Expand Down Expand Up @@ -243,8 +239,6 @@ GEM
nio4r (~> 2.0)
racc (1.7.1)
rack (2.2.6.4)
rack-protection (3.0.5)
rack
rack-proxy (0.7.6)
rack
rack-test (2.1.0)
Expand Down Expand Up @@ -284,23 +278,14 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
redis (4.5.1)
redis-namespace (1.10.0)
redis (>= 4)
redis-client (0.14.1)
connection_pool
regexp_parser (2.8.1)
request_store (1.5.1)
rack (>= 1.4)
responders (3.1.0)
actionpack (>= 5.2)
railties (>= 5.2)
resque (2.5.0)
mono_logger (~> 1.0)
multi_json (~> 1.0)
redis-namespace (~> 1.6)
sinatra (>= 0.9.2)
resque-pool (0.7.1)
rake (>= 10.0, < 14.0)
resque (>= 1.22, < 3)
rexml (3.2.5)
rsolr (2.5.0)
builder (>= 2.1.2)
Expand Down Expand Up @@ -370,11 +355,11 @@ GEM
semantic_range (>= 2.3.0)
shoulda-matchers (4.3.0)
activesupport (>= 4.2.0)
sidekiq (5.2.10)
connection_pool (~> 2.2, >= 2.2.2)
rack (~> 2.0)
rack-protection (>= 1.5.0)
redis (~> 4.5, < 4.6.0)
sidekiq (7.1.2)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.14.0)
simple_form (5.2.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
Expand All @@ -383,11 +368,6 @@ GEM
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
sinatra (3.0.5)
mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.0.5)
tilt (~> 2.0)
snaky_hash (2.0.1)
hashie
version_gem (~> 1.1, >= 1.1.1)
Expand Down Expand Up @@ -479,11 +459,8 @@ DEPENDENCIES
prawn (~> 2.4.0)
psych (< 4)
puma (>= 4.3.0)
rack (~> 2.2.6.3)
rails (~> 6.1)
rails-controller-testing (~> 1.0.0)
redis (~> 4.5.0)
resque-pool (~> 0.7.0)
rsolr (~> 2.5.0)
rspec-rails (~> 4.0.0)
rspec-retry (~> 0.6.0)
Expand All @@ -495,7 +472,7 @@ DEPENDENCIES
selenium-webdriver (~> 4.0)
shakapacker (~> 7.0.2)
shoulda-matchers (~> 4.3.0)
sidekiq (~> 5.2.10)
sidekiq (~> 7.1.2)
simple_form (>= 5.0.0)
simplecov (~> 0.17.0)
spring (~> 2.1.0)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* Ruby version: 3.1
* Node version 16 (using yarn as npm)
* Rails 6.1
* Sidekiq 5
* Sidekiq 7
* Redis 6.2+

## Setup

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ApplicationController < ActionController::Base
rescue_from ActiveRecord::StatementInvalid, with: :render_500
rescue_from Mysql2::Error, with: :render_500
rescue_from Net::LDAP::LdapError, with: :render_500
rescue_from Redis::CannotConnectError, with: :render_500
rescue_from RedisClient::CannotConnectError, with: :render_500
rescue_from Errno::ECONNREFUSED, with: :render_500
rescue_from ActionDispatch::Cookies::CookieOverflow, with: :render_500
rescue_from RuntimeError, with: :render_500
Expand Down
13 changes: 0 additions & 13 deletions config/initializers/redis_config.rb

This file was deleted.

1 change: 0 additions & 1 deletion config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
sidekiq_config = Hash.new

sidekiq_config['password'] = redis_config['password'] if redis_config['password']
sidekiq_config['namespace'] = "etda_workflow_#{current_partner.id}"
sidekiq_url = "redis://#{redis_config.fetch(:host, 'localhost')}:#{redis_config.fetch(:port, 6379)}"
sidekiq_config['url'] = sidekiq_url

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
ports:
- "4444:4444"
redis:
image: redis:5.0.5
image: redis:7.0.12
volumes:
- redis-data:/data
ports:
Expand Down
45 changes: 14 additions & 31 deletions lib/tasks/redis_migrate.rake
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
namespace :redis_migrate do

desc 'Migrate redis db to keys with namespaces identified'
task 'to_namespaces' => :environment do
start = Time.now
graduate_ns = "etda_workflow_graduate:"
honors_ns = "etda_workflow_honors:"
redis = Redis.current
desc 'Migrate redis db to remove namespaces'
task 'remove_namespaces' => :environment do
# This script migrates data from a single namespaced redis
# instance to multiple distinct redis instances for each partner

schedule_key = redis.keys("schedule")
graduate_schedule_key_contents = []
honors_schedule_key_contents = []
old_schedule_key_contents = Hash.new
redis.zscan_each(schedule_key) do |value, score|
old_schedule_key_contents[score] = value
end
# Setup redis connection
redis_config = RedisClient.config(**Rails.application.config_for(:redis))
redis = redis_config.new_pool(timeout: 0.5, size: 5)

count = 0
grad_count = 0
honors_count = 0
old_schedule_key_contents.each do |score, value|
if eval(value)[:args][0] > 10000
graduate_schedule_key_contents << [score, value]
grad_count += 1
else
honors_schedule_key_contents << [score, value]
honors_count += 1
end
count += 1
# Rename all keys including this partner's namespace to the original key with namespace removed
redis.call("KEYS", "etda_workflow_#{current_partner.id}*").each do |key|
redis.call("RENAME", key, key.gsub("etda_workflow_#{current_partner.id}:", ''))
end

redis.del(schedule_key)

redis.zadd([graduate_ns, schedule_key].join(""), graduate_schedule_key_contents)
redis.zadd([honors_ns, schedule_key].join(""), honors_schedule_key_contents)

puts "Process completed in #{(Time.now - start)} sec. #{count} records migrated. #{grad_count} records migrated to graduate namespace. #{honors_count} records migrated to honors namespace."
# Delete all the keys still containing a namespace, hence deleting other partner's keys
redis.call("KEYS", "*").each do |key|
redis.call("DEL", key) if key.include?('etda_workflow')
end
end
end

0 comments on commit d095a22

Please sign in to comment.