Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set blacklisted exceptions #12

Open
wants to merge 7 commits into
base: skroutz
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
language: ruby
rvm:
- 1.9.3
- 2.2
- 2.3.0
- 2.3.3
- 2.5.5
sudo: false
matrix:
include:
- rvm: 1.8.7
gemfile: Gemfiles/Gemfile.1.8.7
4 changes: 4 additions & 0 deletions lib/resque/failure/base.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require 'resque/failure/blacklist_exceptions_registrar'

module Resque
module Failure
# All Failure classes are expected to subclass Base.
#
# When a job fails, a new instance of your Failure backend is created
# and #save is called.
class Base
include BlacklistExceptionsRegistrar

# The exception object raised by the failed job
attr_accessor :exception

Expand Down
50 changes: 50 additions & 0 deletions lib/resque/failure/blacklist_exceptions_registrar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Resque
module Failure
# A module to access blacklisted exceptions
# on a class level
module BlacklistExceptionsRegistrar
def self.included(base)
base.extend ClassMethods
end

# Class methods for accessing
# blacklisted exceptions
module ClassMethods
# Set a list of blacklisted exceptions
#
# The method sets a list of exceptions which will
# not be pushed to the failed queue when a job fails and it's
# exception is included to the blacklisted ones.
#
# @param [Array<String>] exceptions the list of exceptions to blacklist
# @raise [StandardError] if the exceptions argument is not an array
# @return [void]
def set_blacklisted_exceptions(exceptions)
unless exceptions.is_a?(Array)
raise StandardError.new("The exceptions argument must be an array")
end

@blacklist = exceptions
konstantinoskostis marked this conversation as resolved.
Show resolved Hide resolved
end

# Fetch the blacklisted exceptions of a failure backend class
#
# @return [Array<String>]
def get_blacklisted_exceptions
return @blacklist if defined?(@blacklist)

[]
end
end

# Check if the raised exception belongs
# to the backend's blacklisted exceptions.
#
# @return [true, false] true if raised exception is blacklisted,
# false otherwise
def blacklisted_exception_raised?
self.class&.get_blacklisted_exceptions&.include?(@exception.class.to_s)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/resque/failure/multiple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def save
exception = nil
@backends.each do |b|
begin
b.save
b.save unless b.blacklisted_exception_raised?
rescue => e
exception = e
log("Failed saving to #{b.class.name}, error #{e.to_s}")
Expand Down
2 changes: 2 additions & 0 deletions lib/resque/failure/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def self.data_store
end

def save
return if blacklisted_exception_raised?

data = {
:failed_at => UTF8Util.clean(Time.now.strftime("%Y/%m/%d %H:%M:%S %Z")),
:payload => payload,
Expand Down
2 changes: 2 additions & 0 deletions lib/resque/failure/redis_multi_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def self.data_store
end

def save
return if blacklisted_exception_raised?

data = {
:failed_at => Time.now.strftime("%Y/%m/%d %H:%M:%S %Z"),
:payload => payload,
Expand Down
2 changes: 1 addition & 1 deletion lib/resque/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Resque
Version = VERSION = '1.27.4.skroutz.1'
Version = VERSION = '1.27.4.skroutz.2'
end
21 changes: 21 additions & 0 deletions test/resque_failure_multiple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def save
end
end

class CustomException < StandardError; end

describe "Resque::Failure::Multiple" do

it 'requeue_all and does not raise an exception' do
Expand Down Expand Up @@ -62,3 +64,22 @@ def save
end
end
end

describe "blacklisted_exceptions" do
it "sets blacklisted_exceptions on the multiple backend" do
blacklisted_exceptions = ["CustomException"]
Resque::Failure::Redis.set_blacklisted_exceptions(blacklisted_exceptions)
Resque::Failure::Multiple.classes = [Resque::Failure::Redis]

exception = CustomException.new("Custom error")
worker = Resque::Worker.new(:test)
payload = { "class" => Object, "args" => 3 }

multiple_backend = Resque::Failure::Multiple.new(exception, worker, 'test', payload)
backends = multiple_backend.instance_variable_get(:@backends)
backends.each do |b|
assert_equal blacklisted_exceptions, b.class.get_blacklisted_exceptions
end
end
end

80 changes: 80 additions & 0 deletions test/resque_failure_redis_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,83 @@
end

end

class CustomException < StandardError; end

describe "blacklisted exceptions" do

let(:exception) { CustomException.new("Custom error") }
let(:queue) { "test" }
let(:worker) { Resque::Worker.new([queue]) }
let(:payload) { { "class" => Object, "args" => 3 } }
let(:blacklisted_exceptions) { ["CustomException"] }
let(:redis_backend) do
Resque::Failure::Redis.new(exception, worker, queue, payload)
end

describe "set blacklisted exceptions to empty array" do
before do
Resque::Failure::Redis.clear
Resque::Failure::Redis.set_blacklisted_exceptions([])
end

it "returns blacklisted exceptions" do
assert_equal [], redis_backend.class.get_blacklisted_exceptions
end

it "pushes to failed queue" do
redis_backend.save

assert_equal 1, Resque::Failure::Redis.count
end
end

describe "exception during set_blacklisted_exceptions" do
it "raises exception due to wrong argument type" do
assert_raises StandardError do
Resque::Failure::Redis.set_blacklisted_exceptions("CustomException")
end
end
end

describe "setting and using blacklisted exceptions" do
before do
Resque::Failure::Redis.clear
Resque::Failure::Redis.set_blacklisted_exceptions(blacklisted_exceptions)
end

it "returns blacklisted exceptions" do
assert_equal blacklisted_exceptions, redis_backend.class.get_blacklisted_exceptions
end

it "does not push to failed queue" do
redis_backend.save

assert_equal 0, Resque::Failure::Redis.count
end
end

describe "blacklisted_exception_raised?" do
describe "when raised exception is blacklisted" do
before do
Resque::Failure::Redis.clear
Resque::Failure::Redis.set_blacklisted_exceptions(blacklisted_exceptions)
end

it "returns true" do
assert_equal true, redis_backend.blacklisted_exception_raised?
end
end

describe "when raised exception is not blacklisted" do
before do
Resque::Failure::Redis.clear
Resque::Failure::Redis.set_blacklisted_exceptions(["AnotherException"])
end

it "returns false" do
assert_equal false, redis_backend.blacklisted_exception_raised?
end
end
end
end