class Resque::Plugins::ResqueCleaner
ResqueCleaner class provides useful functionalities to retry or clean failed jobs. Let's clean up your failed list!
Attributes
ResqueCleaner fetches all elements from Redis and checks them by linear when filtering them. Since there is a performance concern, ResqueCleaner handles only the latest x(default 1000) jobs.
You can change the value through limiter attribute. e.g. cleaner.limiter.maximum = 5000
Set false if you don't show any message.
Public Class Methods
Initializes instance
# File lib/resque_cleaner.rb, line 22 def initialize @failure = Resque::Failure.backend @print_message = true @limiter = Limiter.new self end
Public Instance Methods
Clears every jobs for which block evaluates to true.
# File lib/resque_cleaner.rb, line 100 def clear(&block) cleared = 0 @limiter.lock do @limiter.jobs.each_with_index do |job,i| if !block_given? || block.call(job) index = @limiter.start_index + i - cleared # fetches again since you can't ensure that it is always true: # a == endode(decode(a)) value = redis.lindex(:failed, index) redis.lrem(:failed, 1, value) cleared += 1 end end end cleared end
Clears all jobs except the last X jobs
# File lib/resque_cleaner.rb, line 149 def clear_stale return 0 unless @limiter.on? c = @limiter.maximum redis.ltrim(:failed, -c, -1) c end
Returns failure backend. Only supports redis backend.
# File lib/resque_cleaner.rb, line 34 def failure @failure end
Outputs message. Overrides this method when you want to change a output stream.
# File lib/resque_cleaner.rb, line 291 def log(msg) puts msg if print? end
# File lib/resque_cleaner.rb, line 295 def print? @print_message end
Print stats
# File lib/resque_cleaner.rb, line 78 def print_stats(stats) log too_many_message if @limiter.on? stats.keys.sort.each do |k| log "%15s: %4d" % [k,stats[k]] end log "%15s: %4d" % ["total", @limiter.count] end
Returns redis instance.
# File lib/resque_cleaner.rb, line 29 def redis Resque.redis end
Retries every jobs for which block evaluates to true.
# File lib/resque_cleaner.rb, line 118 def requeue(clear_after_requeue=false, options={}, &block) requeued = 0 queue = options["queue"] || options[:queue] @limiter.lock do @limiter.jobs.each_with_index do |job,i| if !block_given? || block.call(job) index = @limiter.start_index + i - requeued value = redis.lindex(:failed, index) redis.multi do Job.create(queue||job['queue'], job['payload']['class'], *job['payload']['args']) if clear_after_requeue # remove job # TODO: should use ltrim. not sure why i used lrem here... redis.lrem(:failed, 1, value) else # mark retried job['retried_at'] = Time.now.strftime("%Y/%m/%d %H:%M:%S") redis.lset(:failed, @limiter.start_index+i, Resque.encode(job)) end end requeued += 1 end end end requeued end
Returns every jobs for which block evaluates to true.
# File lib/resque_cleaner.rb, line 87 def select(&block) jobs = @limiter.jobs block_given? ? @limiter.jobs.select(&block) : jobs end
# File lib/resque_cleaner.rb, line 93 def select_by_regex(regex) select do |job| job.to_s =~ regex end end
Stats by class.
# File lib/resque_cleaner.rb, line 52 def stats_by_class(&block) jobs, stats = select(&block), {} jobs.each do |job| klass = job["payload"] && job["payload"]["class"] ? job["payload"]["class"] : "UNKNOWN" stats[klass] ||= 0 stats[klass] += 1 end print_stats(stats) if print? stats end
Stats by date.
# File lib/resque_cleaner.rb, line 39 def stats_by_date(&block) jobs, stats = select(&block), {} jobs.each do |job| date = job["failed_at"][0,10] stats[date] ||= 0 stats[date] += 1 end print_stats(stats) if print? stats end
Stats by exception.
# File lib/resque_cleaner.rb, line 65 def stats_by_exception(&block) jobs, stats = select(&block), {} jobs.each do |job| exception = job["exception"] stats[exception] ||= 0 stats[exception] += 1 end print_stats(stats) if print? stats end
# File lib/resque_cleaner.rb, line 299 def too_many_message "There are too many failed jobs(count=#{@failure.count}). This only looks at last #{@limiter.maximum} jobs." end