Skip to content

Instantly share code, notes, and snippets.

@blotto
Created April 9, 2014 22:15
Show Gist options
  • Save blotto/10324119 to your computer and use it in GitHub Desktop.
Save blotto/10324119 to your computer and use it in GitHub Desktop.
Some Sidekiq helper methods
module AsyncTools
module Cleaners
def self.find_job_for_object_by_method(klass, method)
jobs = Sidekiq::ScheduledSet.new
jobs.select { |job|
job.klass == 'Sidekiq::Extensions::DelayedClass' &&
((job_klass, job_method, args) = YAML.load(job.args[0])) &&
job_klass == klass &&
job_method == method
}
end
def self.delete_job_for_object_by_method(klass, method, id)
jobs = Sidekiq::ScheduledSet.new
jobs.select do |job|
job.klass == 'Sidekiq::Extensions::DelayedClass' &&
((job_klass, job_method, args) = YAML.load(job.args[0])) &&
job_klass == klass &&
job_method == method &&
args[0] == id
end.map(&:delete)
end
def self.delete_jobs_for_object(klass, id)
jobs = Sidekiq::ScheduledSet.new
jobs.select do |job|
p job
job.klass == 'Sidekiq::Extensions::DelayedClass' &&
((job_klass, job_method, args) = YAML.load(job.args[0])) &&
job_klass == klass &&
args[0] == id
end.map(&:delete)
end
def self.clear_retries()
Sidekiq::RetrySet.new.clear
end
def self.clear_queue(queue=nil)
if queue.nil?
Sidekiq::ScheduledSet.new.clear
else
Sidekiq::Queue.new(queue).clear
end
end
def self.sidekiq_stats()
summary = Hash.new
stats = Sidekiq::Stats.new
summary = { processed: stats.processed,
failed: stats.failed,
enqueued: stats.enqueued,
queues: stats.queues}
end
def self.get_items_by_class(klass)
Sidekiq::ScheduledSet.new.select{|job| job.klass == klass}
end
def self.get_items_in_queue(queue)
Sidekiq::ScheduledSet.new.select{|job| job.queue == queue}
end
def self.queue_stats(queue)
summary = Hash.new
queue = Sidekiq::Queue.new(queue)
summary = { size: queue.size,
latency: queue.latency}
end
#Sidekiq::ScheduledSet.new.select{|job| job.queue == 'default'}.map(&:delete)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment