From eb8e4021527fb8c417dca972e5ffe30d8699116f Mon Sep 17 00:00:00 2001 From: Earlopain Date: Sat, 15 Apr 2023 20:43:37 +0200 Subject: [PATCH] [Gems] Update to sidekiq 7 Moves the redis handling into the cache class because of a naming conflict with redis-client which is now being pulled in by sidekiq --- Gemfile | 2 +- Gemfile.lock | 25 ++++++++++++++----------- app/controllers/stats_controller.rb | 3 +-- app/logical/cache.rb | 6 ++++++ app/logical/danger_zone.rb | 4 ++-- app/logical/redis_client.rb | 5 ----- app/logical/stats_updater.rb | 2 +- app/logical/user_throttle.rb | 4 ++-- app/models/ticket.rb | 2 +- config/initializers/sidekiq.rb | 2 +- 10 files changed, 29 insertions(+), 26 deletions(-) delete mode 100644 app/logical/redis_client.rb diff --git a/Gemfile b/Gemfile index ef5134f18..ef7da9f70 100644 --- a/Gemfile +++ b/Gemfile @@ -22,7 +22,7 @@ gem 'httparty' gem 'recaptcha', require: "recaptcha/rails" gem 'webpacker', '>= 4.0.x' gem 'retriable' -gem 'sidekiq', '~> 6.0' +gem 'sidekiq', '~> 7.0' gem 'marcel' # bookmarks for later, if they are needed # gem 'sidekiq-worker-killer' diff --git a/Gemfile.lock b/Gemfile.lock index d8d45f542..41b1e00bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -105,7 +105,7 @@ GEM activesupport cityhash (0.9.0) concurrent-ruby (1.2.2) - connection_pool (2.3.0) + connection_pool (2.4.0) crack (0.4.5) rexml crass (1.0.6) @@ -282,7 +282,10 @@ GEM rbs (2.8.4) recaptcha (5.12.3) json - redis (4.8.1) + redis (5.0.6) + redis-client (>= 0.9.0) + redis-client (0.14.1) + connection_pool regexp_parser (2.7.0) request_store (1.5.1) rack (>= 1.4) @@ -326,16 +329,16 @@ GEM shoulda-context (2.0.0) shoulda-matchers (5.3.0) activesupport (>= 5.2.0) - sidekiq (6.5.8) - connection_pool (>= 2.2.5, < 3) - rack (~> 2.0) - redis (>= 4.5.0, < 5) - sidekiq-unique-jobs (7.1.29) + sidekiq (7.0.8) + concurrent-ruby (< 2) + connection_pool (>= 2.3.0) + rack (>= 2.2.4) + redis-client (>= 0.11.0) + sidekiq-unique-jobs (8.0.2) brpoplpush-redis_script (> 0.1.1, <= 2.0.0) concurrent-ruby (~> 1.0, >= 1.0.5) - redis (< 5.0) - sidekiq (>= 5.0, < 7.0) - thor (>= 0.20, < 3.0) + sidekiq (>= 7.0.0, < 8.0.0) + thor (>= 1.0, < 3.0) simple_form (5.2.0) actionpack (>= 5.2) activemodel (>= 5.2) @@ -427,7 +430,7 @@ DEPENDENCIES ruby-vips shoulda-context shoulda-matchers - sidekiq (~> 6.0) + sidekiq (~> 7.0) sidekiq-unique-jobs simple_form solargraph diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 33571c970..5188cb1b6 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -2,7 +2,6 @@ class StatsController < ApplicationController respond_to :html def index - client = RedisClient.client - @stats = JSON.parse(client.get('e6stats') || '{}') + @stats = JSON.parse(Cache.redis.get("e6stats") || "{}") end end diff --git a/app/logical/cache.rb b/app/logical/cache.rb index db0fe9c45..dafb91c64 100644 --- a/app/logical/cache.rb +++ b/app/logical/cache.rb @@ -27,4 +27,10 @@ class Cache def self.hash(string) CityHash.hash64(string).to_s(36) end + + def self.redis + # Using a shared variable like this here is OK + # since unicorn spawns a new process for each worker + @redis ||= Redis.new(url: Danbooru.config.redis_url) + end end diff --git a/app/logical/danger_zone.rb b/app/logical/danger_zone.rb index c6b75da2f..4d30eac7a 100644 --- a/app/logical/danger_zone.rb +++ b/app/logical/danger_zone.rb @@ -4,10 +4,10 @@ module DangerZone end def self.min_upload_level - (RedisClient.client.get("min_upload_level") || User::Levels::MEMBER).to_i + (Cache.redis.get("min_upload_level") || User::Levels::MEMBER).to_i end def self.min_upload_level=(min_upload_level) - RedisClient.client.set("min_upload_level", min_upload_level) + Cache.redis.set("min_upload_level", min_upload_level) end end diff --git a/app/logical/redis_client.rb b/app/logical/redis_client.rb deleted file mode 100644 index d7a36373a..000000000 --- a/app/logical/redis_client.rb +++ /dev/null @@ -1,5 +0,0 @@ -class RedisClient - def self.client - @@_client ||= ::Redis.new(url: Danbooru.config.redis_url) - end -end diff --git a/app/logical/stats_updater.rb b/app/logical/stats_updater.rb index 5b0dc46c2..5133187c5 100644 --- a/app/logical/stats_updater.rb +++ b/app/logical/stats_updater.rb @@ -77,6 +77,6 @@ class StatsUpdater stats["#{cat}_tags".to_sym] = Tag.where(category: TagCategory.mapping[cat]).count end - RedisClient.client.set("e6stats", stats.to_json) + Cache.redis.set("e6stats", stats.to_json) end end diff --git a/app/logical/user_throttle.rb b/app/logical/user_throttle.rb index 5702349a1..4a80f71ff 100644 --- a/app/logical/user_throttle.rb +++ b/app/logical/user_throttle.rb @@ -40,14 +40,14 @@ class UserThrottle ckey = current_key(t) pkey = previous_key(t) tdiff = t.to_i - ctime(t)*@duration.to_i - hits = RedisClient.client.mget(ckey, pkey) + hits = Cache.redis.mget(ckey, pkey) @cached_rate = (hits[1].to_f * ((@duration.to_i-tdiff)/@duration.to_f) + hits[0].to_f).to_i end def hit! t = Time.now ckey = current_key(t) - RedisClient.client.multi do |transaction| + Cache.redis.multi do |transaction| transaction.incr(ckey) transaction.expire(ckey, cache_duration.minutes) end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 4c890f398..a1834afba 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -361,7 +361,7 @@ class Ticket < ApplicationRecord end def push_pubsub(action) - RedisClient.client.publish("ticket_updates", pubsub_hash(action).to_json) + Cache.redis.publish("ticket_updates", pubsub_hash(action).to_json) end end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 7161f670f..b1f1d9d75 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,4 +1,4 @@ -require 'sidekiq' +require "sidekiq-unique-jobs" Sidekiq.configure_server do |config| config.redis = { url: Danbooru.config.redis_url }