Skip to content

Instantly share code, notes, and snippets.

@HunterMeyer
Created February 6, 2019 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HunterMeyer/5b62368f6f87f376f9bfc9dc40fa0bac to your computer and use it in GitHub Desktop.
Save HunterMeyer/5b62368f6f87f376f9bfc9dc40fa0bac to your computer and use it in GitHub Desktop.
Clear Failed Resque Jobs
class ClearFailedJobs
# Clear all the failed resque jobs for a specific class
# ==== Attributes
#
# * +klasses+ - The Ruby classes to clear as a String or Array of Strings
#
# ==== Examples
# ClearFailedJobs.by_class('Exports::ReportCSV')
# ClearFailedJobs.by_class(['Exports::ReportCSV', 'Exports::ReportPDF'])
def self.by_class(klasses, queue = 'failed')
klassess = [klasses].flatten
redis = Resque.redis
orig = nil
redis.llen(queue).times do
begin
popped = redis.lpop(queue)
if orig
break if orig == popped
else
orig = popped
end
data = JSON.parse(popped)
next if klasses.include?(data['payload']['class'])
redis.rpush(queue, popped)
rescue
next
end
end
end
# Clear all the failed resque jobs for a specific class containing specific arguments
# ==== Attributes
#
# * +klass+ - The Ruby class to clear as a String
# * +arg+ - A String used to match against the payload['args'] of the failed job
#
# ==== Examples
# ClearFailedJobs.by_class_and_argument('Exports::ReportCSV', "'organization_id: '1424167070374827009'")
def self.by_class_and_argument(klass, arg)
redis = Resque.redis
orig = nil
redis.llen('failed').times do
popped = redis.lpop('failed')
if orig
break if orig == popped
else
orig = popped
end
data = JSON.parse(popped)
next if data['payload']['class'] == klass && data['payload']['args'].to_s.include?(arg)
redis.rpush('failed', popped)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment