Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
sidekiq-unique-jobs gem not deleting expired keys in its uniquejobs hash
# Even worse:
class SidekiqUniqueJobsHashCompactor
include Sidekiq::Worker
sidekiq_options queue: "slow"
def perform
# Skip if there are jobs queued...
return unless { |q| q.size > 100 }.blank?
# We need this to get a redis, db12 with no namespace gem
redis = $, port: $redis.client.port, db: 12, thread_safe: true)
to_be_deleted = []
offset_str = redis.get("uniquejobscursor")
offset = offset_str.try(:to_i) || 0
cursor, jobs = redis.hscan("uniquejobs", offset, count: 2_000)
redis.set("uniquejobscursor", cursor)
jobs.each do |job_array|
jid, unique_key = job_array
next if redis.get(unique_key)
to_be_deleted << jid
to_be_deleted.blank? ? num_del = 0 : num_del = redis.hdel("uniquejobs", to_be_deleted)
FDStats.count('uniquejobs_hash_compactor', num_del)
# start =;; puts "#{( - start) * 1000} milliseconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.