Skip to content

Commit

Permalink
add mixed legacy and ActiveJob support
Browse files Browse the repository at this point in the history
resque-cleaner is a dead project
see here: ono#47
  • Loading branch information
glongman committed Dec 27, 2019
1 parent bb78ae7 commit 03be6d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ group :test do
gem 'json'
gem 'timecop'
gem 'rack-test'
gem 'activejob', '5.2.3'
end
17 changes: 11 additions & 6 deletions lib/resque_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def stats_by_date(&block)
def stats_by_class(&block)
jobs, stats = select(&block), {}
jobs.each do |job|
klass = job["payload"] && job["payload"]["class"] ? job["payload"]["class"] : "UNKNOWN"
klass = job.klass
stats[klass] ||= 0
stats[klass] += 1
end
Expand Down Expand Up @@ -178,13 +178,18 @@ def after?(time)
Time.parse(self['failed_at']) >= time
end

# Returns true job name - incl wrapped ActiveJobs
def klass
payload_klass = self.dig("payload", "class")
if payload_klass == "ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper"
payload_klass = self.dig("payload", "args").first.try :[], "job_class"
end
payload_klass || 'UNKNOWN'
end

# Returns true if the class of the job matches. Otherwise returns false.
def klass?(klass_or_name)
if self["payload"] && self["payload"]["class"]
self["payload"]["class"] == klass_or_name.to_s
else
klass_or_name=="UNKNOWN"
end
self.klass == klass_or_name.to_s
end

# Returns true if the exception raised by the failed job matches. Otherwise returns false.
Expand Down
4 changes: 2 additions & 2 deletions test/resque_cleaner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
assert_equal 22, ret.size

# filter by class
ret = @cleaner.select {|j| j.klass?(BadJobWithSyntaxError)}
ret = @cleaner.select {|j| j.klass?(BadJobWithSyntaxErrorActive)}
assert_equal 7, ret.size

# filter by exception
Expand Down Expand Up @@ -162,7 +162,7 @@
it "#stats_by_class returns stats grouped by class" do
ret = @cleaner.stats_by_class
assert_equal 35, ret['BadJob']
assert_equal 7, ret['BadJobWithSyntaxError']
assert_equal 7, ret['BadJobWithSyntaxErrorActive']
end

it "#stats_by_class works with broken log" do
Expand Down
16 changes: 15 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
require 'minitest/autorun'
require 'resque'
require 'timecop'
require 'active_job'

ActiveJob::Base.logger = ActiveSupport::Logger.new(IO::NULL)

begin
require 'leftright'
Expand Down Expand Up @@ -99,14 +102,25 @@ def self.perform
end
end

class BadJobWithSyntaxErrorActive < ActiveJob::Base
self.queue_adapter = :resque
def perform
raise SyntaxError, "Extra Bad job!"
end
end

#
# helper methods
#

def create_and_process_jobs(queue,worker,num,date,job,*args)
Timecop.freeze(date) do
num.times do
Resque::Job.create(queue, job, *args)
if job.ancestors.include?(ActiveJob::Base)
job.set(queue: queue).perform_later(*args)
else
Resque::Job.create(queue, job, *args)
end
end
worker.work(0)
end
Expand Down

0 comments on commit 03be6d4

Please sign in to comment.