diff --git a/Gemfile b/Gemfile index 9e5a801..e34d20b 100644 --- a/Gemfile +++ b/Gemfile @@ -7,4 +7,5 @@ group :test do gem 'json' gem 'timecop' gem 'rack-test' + gem 'activejob', '5.2.3' end diff --git a/lib/resque_cleaner.rb b/lib/resque_cleaner.rb index 4a8148b..b278efe 100644 --- a/lib/resque_cleaner.rb +++ b/lib/resque_cleaner.rb @@ -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 @@ -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. diff --git a/test/resque_cleaner_test.rb b/test/resque_cleaner_test.rb index 605f7be..ffd7f01 100644 --- a/test/resque_cleaner_test.rb +++ b/test/resque_cleaner_test.rb @@ -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 @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 698ac82..dfa3e6d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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' @@ -99,6 +102,13 @@ def self.perform end end +class BadJobWithSyntaxErrorActive < ActiveJob::Base + self.queue_adapter = :resque + def perform + raise SyntaxError, "Extra Bad job!" + end +end + # # helper methods # @@ -106,7 +116,11 @@ def self.perform 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