Skip to content

Commit

Permalink
Merge pull request #1 from Fullscript/support-activejob-5.2
Browse files Browse the repository at this point in the history
Add mixed legacy and ActiveJob support
  • Loading branch information
glongman authored Jan 6, 2020
2 parents bb78ae7 + cff1b2a commit c57544d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 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
18 changes: 12 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 @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/resque_cleaner/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/resque_cleaner/server/views/cleaner_list.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<% end %>
</dd>
<dt>Class</dt>
<dd><code><%= job['payload'] ? job['payload']['class'] : 'nil' %></code></dd>
<dd><code><%= job.klass %></code></dd>
<dt>Arguments</dt>
<dd><pre><%=h job['payload'] ? show_job_args(job['payload']['args']) : 'nil' %></pre></dd>
<dt>Exception</dt>
Expand Down
6 changes: 3 additions & 3 deletions test/resque_cleaner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down 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 c57544d

Please sign in to comment.