Skip to content

Instantly share code, notes, and snippets.

@Koronen
Last active August 29, 2015 13:57
Show Gist options
  • Save Koronen/9738152 to your computer and use it in GitHub Desktop.
Save Koronen/9738152 to your computer and use it in GitHub Desktop.
Resque statistics hacks
# Print statistics
failures = Resque::Failure.all(0, -1).each_with_index.map { |ex, i| ex['id'] = i; ex }; nil
fbe = Hash.new { |h, k| h[k] = Array.new }
fbc = Hash.new { |h, k| h[k] = Array.new }
failures.each { |ex| fbe[ex['exception']] << ex; fbc[ex['payload']['class']] << ex }; nil
puts fbe.map { |e,jj| [e, jj.size] }.inspect
puts fbc.map { |e,jj| [e, jj.size] }.inspect
# Dump failures as JSON
failures = Resque::Failure.all(0, -1).each_with_index.map { |ex, i| ex['id'] = i; ex }; nil
File.open("resque_failures_#{Time.now.strftime("%FT%T")}.json", 'wb') { |f| f.write JSON.dump(failures) }
# Dump low queue as JSON
low = Resque.redis.lrange 'queue:low', 0, -1; nil
File.open("low_#{Time.now.strftime("%FT%T")}.json", "wb") { |f| f.write JSON.dump(low.map{|x| JSON.parse(x)}) }
# Remove duplicates(?)
key = 'queue:low'
members = Resque.redis.lrange(key, 0, -1)
duplicate_members = members.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
if duplicate_members.count > 0
puts "#{key}: has #{duplicate_members.count} duplicates"
duplicate_members.each do |duplicate_member|
puts "Removing #{duplicate_member}"
Resque.redis.lrem(key, -1, duplicate_member)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment