Skip to content

Instantly share code, notes, and snippets.

@riyad
Last active May 13, 2019 12:18
Show Gist options
  • Save riyad/9086d2b17ff1e8c091cdb1c7ac501b62 to your computer and use it in GitHub Desktop.
Save riyad/9086d2b17ff1e8c091cdb1c7ac501b62 to your computer and use it in GitHub Desktop.
Cleanup Sidekiq uniquejobs in Redis (see https://github.com/mhenrixon/sidekiq-unique-jobs/issues/195)
#!/usr/bin/env ruby
#
# Cleanup Sidekiq uniquejobs in Redis
# see https://github.com/mhenrixon/sidekiq-unique-jobs/issues/195
#
require 'bundler/setup'
require 'sidekiq/api'
DRY_RUN = true
def jobs_jids
queues = Sidekiq::Queue.all
queues << Sidekiq::ScheduledSet.new
queues << Sidekiq::RetrySet.new
queues << Sidekiq::DeadSet.new
queues.flat_map { |q| q.map(&:jid) }
end
puts "*** NOTE: This is a dry run. Nothing will be changed. ***" if DRY_RUN
# remove uniquejobs information for all "non-existing" jobs
Sidekiq.redis do |redis|
puts "Cleaning up 'uniquejobs:*' ..."
all_ujs = redis.keys('uniquejobs:*')
puts "There're #{all_ujs.count} 'uniquejobs:*' entries"
all_ujs.each do |ujs|
stored_jid = redis.get(ujs)
print "Examining #{ujs} => #{stored_jid} ... "
# adapted from https://github.com/mhenrixon/sidekiq-unique-jobs/blob/master/redis/release_lock.lua
if !stored_jid || stored_jid == 2 || !jobs_jids.include?(stored_jid)
redis.del(ujs) unless DRY_RUN
puts "removed"
else
puts "skipped"
end
end
puts "There're now #{redis.keys('uniquejobs:*').count} 'uniquejobs:*' entries"
puts "Done"
end
Sidekiq.redis do |redis|
puts "Cleaning up 'uniquejobs' ..."
puts "'uniquejobs' has #{redis.hlen('uniquejobs')} entries"
jids_to_remove = Set.new(redis.hkeys('uniquejobs')) - jobs_jids
puts "Will try to remove #{jids_to_remove.count} entries"
jids_to_remove.each do |jid|
ujs = redis.hget('uniquejobs', jid)
print "Examining jid:#{jid} => #{ujs} ... "
if redis.exists(ujs)
puts "skipped"
else
redis.del(ujs) unless DRY_RUN
redis.hdel('uniquejobs', jid) unless DRY_RUN
puts "removed"
end
end
puts "'uniquejobs' has #{redis.hlen('uniquejobs')} entries now"
puts "Done"
end
puts "*** NOTE: This was a dry run. Nothing was changed. ***" if DRY_RUN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment