1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

refactoring

This commit is contained in:
Friedhelm Filler 2018-05-30 22:20:28 +02:00 committed by Mike Perham
parent 849547c492
commit 2b20e4c93e
4 changed files with 36 additions and 49 deletions

View file

@ -1,10 +1,23 @@
# frozen_string_literal: true
require 'sidekiq'
require 'sidekiq/redis_iterator'
module Sidekiq
module RedisScanner
def sscan(conn, key)
cursor = '0'
result = []
loop do
cursor, values = conn.sscan(key, cursor)
result.push(*values)
break if cursor == '0'
end
result
end
end
class Stats
include RedisIterator
include RedisScanner
def initialize
fetch_stats!
@ -125,7 +138,7 @@ module Sidekiq
end
class Queues
include RedisIterator
include RedisScanner
def lengths
Sidekiq.redis do |conn|
@ -209,7 +222,7 @@ module Sidekiq
#
class Queue
include Enumerable
extend RedisIterator
extend RedisScanner
##
# Return all known queues within Redis.
@ -712,15 +725,15 @@ module Sidekiq
#
class ProcessSet
include Enumerable
extend RedisIterator
include RedisScanner
def initialize(clean_plz=true)
self.class.cleanup if clean_plz
cleanup if clean_plz
end
# Cleans up dead processes recorded in Redis.
# Returns the number of processes cleaned.
def self.cleanup
def cleanup
count = 0
Sidekiq.redis do |conn|
procs = sscan(conn, 'processes').sort
@ -743,7 +756,7 @@ module Sidekiq
end
def each
procs = Sidekiq.redis { |conn| self.class.sscan(conn, 'processes') }.sort
procs = Sidekiq.redis { |conn| sscan(conn, 'processes') }.sort
Sidekiq.redis do |conn|
# We're making a tradeoff here between consuming more memory instead of
@ -878,7 +891,7 @@ module Sidekiq
#
class Workers
include Enumerable
include RedisIterator
include RedisScanner
def each
Sidekiq.redis do |conn|

View file

@ -1,16 +0,0 @@
module Sidekiq
module RedisIterator
def sscan(conn, key)
cursor = '0'
result = []
loop do
cursor, values = conn.sscan(key, cursor)
result.push(*values)
break if cursor == '0'
end
result
end
end
end

View file

@ -10,6 +10,20 @@ class TestApi < Sidekiq::Test
Sidekiq.redis {|c| c.flushdb }
end
describe 'RedisScanner' do
it 'returns identical to smembers' do
test_obj = Object.new
test_obj.extend(Sidekiq::RedisScanner)
50.times do |i|
Sidekiq.redis { |conn| conn.sadd('processes', "test-process-#{i}") }
end
sscan = Sidekiq.redis { |c| test_obj.sscan(c, 'processes') }.sort!
smembers = Sidekiq.redis { |c| c.smembers('processes') }.sort!
assert_equal sscan.size, 50
assert_equal sscan, smembers
end
end
describe "stats" do
it "is initially zero" do
s = Sidekiq::Stats.new

View file

@ -1,24 +0,0 @@
# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/redis_iterator'
class TestRedisIterator < Sidekiq::Test
class Helper
include Sidekiq::RedisIterator
end
describe '.sscan' do
before do
50.times do |i|
Sidekiq.redis { |conn| conn.sadd('processes', "test-process-#{i}") }
end
end
it 'returns identical to smembers' do
sscan = Sidekiq.redis { |c| Helper.new.sscan(c, 'processes') }.sort!
smembers = Sidekiq.redis { |c| c.smembers('processes') }.sort!
assert_equal sscan.size, 50
assert_equal sscan, smembers
end
end
end