Skip to content

Commit

Permalink
Imrove code quality and reduce method size.
Browse files Browse the repository at this point in the history
Cleaning up `Digests#delete_by_digest`. Instead of assembling all keys
inside of the method, we introduced a const and two helper methods to
make the code easier on the eyes.
  • Loading branch information
bigzed committed Jul 27, 2023
1 parent 31f19b0 commit 232f6ea
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions lib/sidekiq_unique_jobs/digests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ module SidekiqUniqueJobs
# @author Mikael Henriksson <[email protected]>
#
class Digests < Redis::SortedSet
#
# @return [Integer] the number of matches to return by default
DEFAULT_COUNT = 1_000
#
# @return [String] the default pattern to use for matching
SCAN_PATTERN = "*"
#
# @return [Array(String, String, String, String)] The empty runtime or queuetime keys.
EMPTY_KEYS_SEGMENT = ["", "", "", ""].freeze

def initialize(digests_key = DIGESTS)
super(digests_key)
end
Expand Down Expand Up @@ -43,27 +53,12 @@ def delete_by_pattern(pattern, count: DEFAULT_COUNT)
# @param [String] digest a unique digest to delete
# @param queuetime [Boolean] Whether to delete queue locks.
# @param runtime [Boolean] Whether to delete run locks.
def delete_by_digest(digest, queuetime: true, runtime: true) # rubocop:disable Metrics/MethodLength
def delete_by_digest(digest, queuetime: true, runtime: true)
result, elapsed = timed do
queue = queuetime ? digest : ""
queue_queued = queuetime ? "#{digest}:QUEUED" : ""
queue_primed = queuetime ? "#{digest}:PRIMED" : ""
queue_locked = queuetime ? "#{digest}:LOCKED" : ""
run = runtime ? "#{digest}:RUN" : ""
run_queued = runtime ? "#{digest}:RUN:QUEUED" : ""
run_primed = runtime ? "#{digest}:RUN:PRIMED" : ""
run_locked = runtime ? "#{digest}:RUN:LOCKED" : ""
call_script(:delete_by_digest, [
queue,
queue_queued,
queue_primed,
queue_locked,
run,
run_queued,
run_primed,
run_locked,
key,
])
call_script(
:delete_by_digest,
queuetime_keys(queuetime ? digest : nil) + runtime_keys(runtime ? digest : nil) + [key],
)
end

log_info("#{__method__}(#{digest}) completed in #{elapsed}ms")
Expand Down Expand Up @@ -107,5 +102,33 @@ def page(cursor: 0, pattern: SCAN_PATTERN, page_size: 100)
]
end
end

private

# @param digest [String, nil] The digest to form runtime keys from.
# @return [Array(String, String, String, String)] The list of runtime keys or empty strings if +digest+ was +nil+.
def runtime_keys(digest)
return EMPTY_KEYS_SEGMENT unless digest

[
"#{digest}:RUN",
"#{digest}:RUN:QUEUED",
"#{digest}:RUN:PRIMED",
"#{digest}:RUN:LOCKED"
]
end

# @param digest [String, nil] The digest to form queuetime keys from.
# @return [Array(String, String, String, String)] The list of queuetime keys or empty strings if +digest+ was +nil+.
def queuetime_keys(digest)
return EMPTY_KEYS_SEGMENT unless digest

[
digest,
"#{digest}:QUEUED",
"#{digest}:PRIMED",
"#{digest}:LOCKED"
]
end
end
end

0 comments on commit 232f6ea

Please sign in to comment.