Skip to content

Commit

Permalink
Pass redis_version into scripts (#431)
Browse files Browse the repository at this point in the history
* Pass redis_version into scripts

Fixes #424 (comment)

Signed-off-by: Mika Hel <mikael@zoolutions.se>

* Remove mindless debug instructions
  • Loading branch information
mhenrixon authored Oct 8, 2019
1 parent 49ae903 commit 4265c0e
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inherit_mode:
- Exclude

AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4
Include:
- "**/Rakefile"
- "**/Gemfile"
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq_unique_jobs/batch_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def keys_for_digest(digest)
end

def redis_version
@redis_version ||= SidekiqUniqueJobs.redis_version
@redis_version ||= SidekiqUniqueJobs.config.redis_version
end
end
end
32 changes: 20 additions & 12 deletions lib/sidekiq_unique_jobs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module SidekiqUniqueJobs
:reaper_interval,
:reaper_timeout,
:lock_info,
:raise_on_config_error)
:raise_on_config_error,
:current_redis_version)

#
# Shared class for dealing with gem configuration
Expand Down Expand Up @@ -65,18 +66,19 @@ class Config < ThreadSafeConfig
reschedule: SidekiqUniqueJobs::OnConflict::Reschedule,
}.freeze

PREFIX = "uniquejobs"
LOCK_TIMEOUT = 0
LOCK_TTL = nil
ENABLED = true
DEBUG_LUA = false
MAX_HISTORY = 1_000
REAPER = :ruby # The type of cleanup to run. Possible values are [:ruby, :lua]
REAPER_COUNT = 1_000
REAPER_INTERVAL = 600 # Every 10 minutes
REAPER_TIMEOUT = 10 # 10 seconds
USE_LOCK_INFO = false
PREFIX = "uniquejobs"
LOCK_TIMEOUT = 0
LOCK_TTL = nil
ENABLED = true
DEBUG_LUA = false
MAX_HISTORY = 1_000
REAPER = :ruby # The type of cleanup to run. Possible values are [:ruby, :lua]
REAPER_COUNT = 1_000
REAPER_INTERVAL = 600 # Every 10 minutes
REAPER_TIMEOUT = 10 # 10 seconds
USE_LOCK_INFO = false
RAISE_ON_CONFIG_ERROR = false
REDIS_VERSION = "0.0.0"

#
# Returns a default configuration
Expand Down Expand Up @@ -139,6 +141,7 @@ def self.default # rubocop:disable Metrics/MethodLength
REAPER_TIMEOUT,
USE_LOCK_INFO,
RAISE_ON_CONFIG_ERROR,
REDIS_VERSION,
)
end

Expand Down Expand Up @@ -181,5 +184,10 @@ def add_strategy(name, klass)
new_strategies = strategies.dup.merge(strategy_sym => klass).freeze
self.strategies = new_strategies
end

def redis_version
self.current_redis_version = SidekiqUniqueJobs.fetch_redis_version if current_redis_version == REDIS_VERSION
current_redis_version
end
end
end
2 changes: 1 addition & 1 deletion lib/sidekiq_unique_jobs/locksmith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def delete
# Deletes the lock regardless of if it has a pttl set
#
def delete!
call_script(:delete, key.to_a, [job_id]).positive?
call_script(:delete, key.to_a, [job_id, config.pttl, config.type, config.limit]).positive?
end

#
Expand Down
5 changes: 3 additions & 2 deletions lib/sidekiq_unique_jobs/lua/delete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ local limit = tonumber(ARGV[4])

-------- BEGIN injected arguments --------
local current_time = tonumber(ARGV[5])
local debug_lua = ARGV[6] == "true"
local debug_lua = tostring(ARGV[6]) == "true"
local max_history = tonumber(ARGV[7])
local script_name = tostring(ARGV[8]) .. ".lua"
local redisversion = tostring(ARGV[9])
--------- END injected arguments ---------

-------- BEGIN local functions --------
Expand All @@ -30,7 +31,7 @@ local script_name = tostring(ARGV[8]) .. ".lua"
-------- BEGIN delete.lua --------
log_debug("BEGIN delete", digest)

local redis_version = redis_version()
local redis_version = toversion(redisversion)
local count = 0
local del_cmd = "DEL"

Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq_unique_jobs/lua/delete_by_digest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local current_time = tonumber(ARGV[1])
local debug_lua = ARGV[2] == "true"
local max_history = tonumber(ARGV[3])
local script_name = tostring(ARGV[4]) .. ".lua"
local redisversion = tostring(ARGV[5])
--------- END injected arguments ---------

-------- BEGIN local functions --------
Expand All @@ -27,7 +28,7 @@ local run_locked = digest .. ":RUN:LOCKED"

-------- BEGIN delete_by_digest.lua --------
local counter = 0
local redis_version = redis_version()
local redis_version = toversion(redisversion)
local del_cmd = "DEL"

log_debug("BEGIN delete_by_digest:", digest)
Expand Down
1 change: 1 addition & 0 deletions lib/sidekiq_unique_jobs/lua/lock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local current_time = tonumber(ARGV[5])
local debug_lua = ARGV[6] == "true"
local max_history = tonumber(ARGV[7])
local script_name = tostring(ARGV[8]) .. ".lua"
local redisversion = ARGV[9]
--------- END injected arguments ---------

-------- BEGIN local functions --------
Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq_unique_jobs/lua/reap_orphans.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local current_time = tonumber(ARGV[2])
local debug_lua = ARGV[3] == "true"
local max_history = tonumber(ARGV[4])
local script_name = ARGV[5] .. ".lua"
local redisversion = ARGV[6]
--------- END injected arguments ---------


Expand All @@ -32,7 +33,7 @@ local per = 50
local total = redis.call("ZCARD", digests_set)
local index = 0
local del_count = 0
local redis_ver = redis_version()
local redis_ver = toversion(redisversion)
local del_cmd = "DEL"

if tonumber(redis_ver["major"]) >= 4 then del_cmd = "UNLINK"; end
Expand Down
8 changes: 4 additions & 4 deletions lib/sidekiq_unique_jobs/lua/shared/_common.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local function redis_version()
local serverinfo = redis.call("INFO", "SERVER")
local _, _, maj, min, pat = string.find(serverinfo, "redis_version%:(%d+)%.(%d+)%.(%d+)")
local function toversion(version)
local _, _, maj, min, pat = string.find(version, "(%d+)%.(%d+)%.(%d+)")

return {
["version"] = maj .. "." .. min .. "." .. pat,
["version"] = version,
["major"] = tonumber(maj),
["minor"] = tonumber(min),
["patch"] = tonumber(pat)
Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq_unique_jobs/lua/unlock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local current_time = tonumber(ARGV[5])
local debug_lua = ARGV[6] == "true"
local max_history = tonumber(ARGV[7])
local script_name = tostring(ARGV[8]) .. ".lua"
local redisversion = ARGV[9]
--------- END injected arguments ---------

-------- BEGIN Variables --------
Expand Down Expand Up @@ -74,7 +75,7 @@ if pttl and pttl > 0 then
log_debug("PEXPIRE", info, pttl)
redis.call("PEXPIRE", info, pttl)
else
local redis_version = redis_version()
local redis_version = toversion(redisversion)
local del_cmd = "DEL"

if tonumber(redis_version["major"]) >= 4 then del_cmd = "UNLINK"; end
Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq_unique_jobs/lua/upgrade.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local current_time = tonumber(ARGV[5])
local debug_lua = ARGV[6] == "true"
local max_history = tonumber(ARGV[7])
local script_name = tostring(ARGV[8]) .. ".lua"
local redisversion = ARGV[9]
--------- END injected arguments ---------

-------- BEGIN local functions --------
Expand All @@ -19,7 +20,7 @@ local script_name = tostring(ARGV[8]) .. ".lua"

local new_version = redis.call("GET", live_version)
local old_version = redis.call("GET", dead_version)
local redis_version = redis_version()
local redis_version = toversion(redisversion)
local upgraded = 0
local del_cmd = "DEL"

Expand Down
15 changes: 14 additions & 1 deletion lib/sidekiq_unique_jobs/script/caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ def call_script(file_name, *args)
# Only used to reduce a little bit of duplication
# @see call_script
def do_call(file_name, conn, keys, argv)
argv = argv.dup.concat([now_f, debug_lua, max_history, file_name])
argv = argv.dup.concat([
now_f,
debug_lua,
max_history,
file_name,
redis_version,
])
Script.call(file_name, conn, keys: keys, argv: argv)
end

Expand Down Expand Up @@ -107,6 +113,13 @@ def debug_lua
def max_history
SidekiqUniqueJobs.config.max_history
end

#
# @see SidekiqUniqueJobs::Config#max_history
#
def redis_version
SidekiqUniqueJobs.config.redis_version
end
end
end
end
4 changes: 2 additions & 2 deletions lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ def configure(options = {})
#
# @return [String] a string like `5.0.2`
#
def redis_version
@redis_version ||= redis { |conn| conn.info("server")["redis_version"] }
def fetch_redis_version
redis { |conn| conn.info("server")["redis_version"] }
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq_unique_jobs/upgrade_locks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def now_f
end

def redis_version
@redis_version ||= SidekiqUniqueJobs.redis_version
@redis_version ||= SidekiqUniqueJobs.config.redis_version
end

def logging_context
Expand Down
4 changes: 2 additions & 2 deletions spec/sidekiq_unique_jobs/sidekiq_unique_jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
end
end

describe ".redis_version" do
subject(:redis_version) { described_class.redis_version }
describe ".fetch_redis_version" do
subject(:fetch_redis_version) { described_class.fetch_redis_version }

it { is_expected.to be_a(String) }
it { is_expected.to match(/(\d+\.?)+/) }
Expand Down

0 comments on commit 4265c0e

Please sign in to comment.