From cff1b2a78ae31d529918146c2033905cba283601 Mon Sep 17 00:00:00 2001 From: Geoff Longman Date: Thu, 26 Dec 2019 22:41:30 -0500 Subject: [PATCH] add mixed legacy and ActiveJob support resque-cleaner is a dead project see here: https://github.com/ono/resque-cleaner/issues/47 --- Gemfile | 1 + lib/resque_cleaner.rb | 18 ++++++++++++------ lib/resque_cleaner/server.rb | 2 +- .../server/views/cleaner_list.erb | 2 +- test/resque_cleaner_test.rb | 6 +++--- test/test_helper.rb | 16 +++++++++++++++- 6 files changed, 33 insertions(+), 12 deletions(-) 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..57d1298 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 @@ -125,6 +125,7 @@ def requeue(clear_after_requeue=false, options={}, &block) value = redis.lindex(:failed, index) redis.multi do + # no change needed to support ActiveJob Job.create(queue||job['queue'], job['payload']['class'], *job['payload']['args']) if clear_after_requeue @@ -178,13 +179,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/lib/resque_cleaner/server.rb b/lib/resque_cleaner/server.rb index 855b485..a132a5f 100644 --- a/lib/resque_cleaner/server.rb +++ b/lib/resque_cleaner/server.rb @@ -118,7 +118,7 @@ def text_filter(id, name, value) @total = Hash.new(0) @jobs.each do |job| payload = job["payload"] || {} - klass = payload["class"] || 'UNKNOWN' + klass = job.klass exception = job["exception"] || 'UNKNOWN' failed_at = Time.parse job["failed_at"] @stats[:klass][klass] ||= Hash.new(0) diff --git a/lib/resque_cleaner/server/views/cleaner_list.erb b/lib/resque_cleaner/server/views/cleaner_list.erb index f38eaa8..4c056c1 100644 --- a/lib/resque_cleaner/server/views/cleaner_list.erb +++ b/lib/resque_cleaner/server/views/cleaner_list.erb @@ -89,7 +89,7 @@ <% end %>
Class
-
<%= job['payload'] ? job['payload']['class'] : 'nil' %>
+
<%= job.klass %>
Arguments
<%=h job['payload'] ? show_job_args(job['payload']['args']) : 'nil' %>
Exception
diff --git a/test/resque_cleaner_test.rb b/test/resque_cleaner_test.rb index 605f7be..4d662c3 100644 --- a/test/resque_cleaner_test.rb +++ b/test/resque_cleaner_test.rb @@ -15,7 +15,7 @@ create_and_process_jobs :jobs, @worker, 1, Time.parse('2009-03-13'), BadJob, "Johnson" # 7 BadJob at 2009-11-13 - create_and_process_jobs :jobs, @worker, 7, Time.parse('2009-11-13'), BadJobWithSyntaxError + create_and_process_jobs :jobs, @worker, 7, Time.parse('2009-11-13'), BadJobWithSyntaxErrorActive # 7 BadJob by Freddy at 2009-11-13 create_and_process_jobs :jobs2, @worker, 7, Time.parse('2009-11-13'), BadJob, "Freddy" @@ -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