Skip to content

Commit

Permalink
Merge pull request #2 from jobscore/chore/add_ruby_3_support
Browse files Browse the repository at this point in the history
Add ruby 3 support
  • Loading branch information
GlauberrBatista authored Aug 2, 2023
2 parents 61546ce + d71138b commit 90ff3f1
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
resque-cleaner (0.4.1)
resque-cleaner (0.5)
resque

GEM
Expand Down Expand Up @@ -54,4 +54,4 @@ DEPENDENCIES
timecop

BUNDLED WITH
2.4.14
2.4.18
16 changes: 9 additions & 7 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_name
stats[klass] ||= 0
stats[klass] += 1
end
Expand Down Expand Up @@ -153,7 +153,7 @@ def clear_stale
c
end

# Exntends job(Hash instance) with some helper methods.
# Extends job(Hash instance) with some helper methods.
module FailedJobEx
# Returns true if the job has been already retried. Otherwise returns
# false.
Expand All @@ -180,11 +180,7 @@ def after?(time)

# 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
klass_name == klass_or_name.to_s
end

# Returns true if the exception raised by the failed job matches. Otherwise returns false.
Expand All @@ -196,6 +192,12 @@ def exception?(exception)
def queue?(queue)
self["queue"] == queue.to_s
end

def klass_name
@klass_name ||= (self.dig("payload", "args", 0).is_a?(Hash) && self.dig("payload", "args", 0, "job_class")) ||
self.dig("payload", "class") ||
"UNKNOWN"
end
end

# Through the Limiter class, you accesses only the last x(default 1000)
Expand Down
7 changes: 4 additions & 3 deletions lib/resque_cleaner/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def exception_filter(id, name, exceptions, value)
end

def show_job_args(args)
Array(args).map { |a| a.inspect }.join("\n")
arguments = (args[0].is_a?(Hash) && args.dig(0, "arguments")) || args
Array(arguments).map { |a| a.inspect }.join("\n")
end

def text_filter(id, name, value)
Expand All @@ -118,7 +119,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_name
exception = job["exception"] || 'UNKNOWN'
failed_at = Time.parse job["failed_at"]
@stats[:klass][klass] ||= Hash.new(0)
Expand Down Expand Up @@ -230,7 +231,7 @@ def build_urls
f: @from,
t: @to,
regex: @regex
}.map {|key,value| "#{key}=#{URI.encode(value.to_s)}"}.join("&")
}.map {|key,value| "#{key}=#{CGI.escape(value.to_s)}"}.join("&")

@list_url = "cleaner_list?#{params}"
@dump_url = "cleaner_dump?#{params}"
Expand Down
2 changes: 1 addition & 1 deletion lib/resque_cleaner/server/views/_stats.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</tr>
<% @stats[type.to_sym].each do |field,count| %>
<tr>
<% filter = "#{q}=#{URI.encode(field)}" %>
<% filter = "#{q}=#{CGI.escape(field)}" %>
<td><%= field %></td>
<td class="number">
<a href="cleaner_list?<%=filter%>"><%= count[:total] %></a>
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_name %></code></dd>
<dt>Arguments</dt>
<dd><pre><%=h job['payload'] ? show_job_args(job['payload']['args']) : 'nil' %></pre></dd>
<dt>Exception</dt>
Expand Down
2 changes: 1 addition & 1 deletion resque-cleaner.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "resque-cleaner"
s.version = "0.4.1"
s.version = "0.5"
s.date = Time.now.strftime('%Y-%m-%d')
s.summary = "Resque plugin cleaning up failed jobs."
s.homepage = "https://github.com/jobscore/resque-cleaner"
Expand Down
7 changes: 7 additions & 0 deletions test/resque_web_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def setup_some_failed_jobs
assert last_response.ok?, last_response.errors
end

it '#cleaner_list shows ActiveJob failed jobs properly formated' do
add_activejob_failure

get "/cleaner_list", :c => "ActiveJobGoodJob"
assert last_response.body.include?("ActiveJobGoodJob")
end

it '#cleaner_list shows the failed jobs' do
get "/cleaner_list"
assert last_response.body.include?('BadJob')
Expand Down
29 changes: 29 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,32 @@ def add_empty_payload_failure
data = Resque.encode(data)
Resque.redis.rpush(:failed, data)
end

def add_activejob_failure
data = {
:failed_at => Time.now.strftime("%Y/%m/%d %H:%M:%S %Z"),
:payload => {
:class => "ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper",
:args => [
:job_class => "ActiveJobGoodJob",
:job_id => "0bc036ab-32c0-4ad0-9138-abdfb06658c4",
:provider_job_id => nil,
:queue_name => "download_scrape_job",
:priority => nil,
:arguments => [:good_job],
:executions => 0,
:exception_executions => {},
:locale => "en",
:timezone => "UTC",
:enqueued_at => "2020-10-13T16:37:18Z"
]
},
:exception => "Resque::DirtyExit",
:error => "Resque::DirtyExit",
:backtrace => [],
:worker => "worker",
:queue => "queue"
}
data = Resque.encode(data)
Resque.redis.rpush(:failed, data)
end

0 comments on commit 90ff3f1

Please sign in to comment.