Skip to content
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ You could configure TireAsyncIndex in initializer:
config.use_queue :high # name of your queue
end

### Error Handling

You can specify an error handling class:

TireAsyncIndex.configure do |config|
config.error_handler TireAsyncHandler
end

It must respond to `handle`, for instance if you want to retry on a term
exception and you use Resque:

class TireAsyncHandler
def self.handle(klass, action_type, class_name, id, exception)
if exception.is_a?(Resque::TermException)
Resque.enqueue(klass, action_type, class_name, id)
else
raise exception
end
end
end

## Usage

Just add AsyncCallbacks to your model:
Expand Down
4 changes: 4 additions & 0 deletions lib/tire_async_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def engine
self.configuration.engine
end

def error_handler
self.configuration.error_handler
end

def worker
case configuration.engine
when :sidekiq
Expand Down
7 changes: 6 additions & 1 deletion lib/tire_async_index/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Configuration

attr_accessor :queue
attr_accessor :engine
attr_accessor :error_handler

def background_engine type
if AVALAIBLE_ENGINE.include?(type.to_sym)
Expand All @@ -17,10 +18,14 @@ def use_queue name
@queue = name.to_sym
end

def use_error_handler error_handler
@error_handler = error_handler
end

def initialize
@queue = :normal
@engine = :none
end

end
end
end
7 changes: 7 additions & 0 deletions lib/tire_async_index/workers/resque.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TireAsyncIndex
module Workers
class Resque < UpdateIndex

def self.queue; TireAsyncIndex.queue end

def self.run(action_type, class_name, id)
Expand All @@ -9,6 +10,12 @@ def self.run(action_type, class_name, id)

def self.perform(action_type, class_name, id)
self.new.process(action_type, class_name, id)
rescue Exception => e
if TireAsyncIndex.error_handler
TireAsyncIndex.error_handler.handle(self, action_type, class_name, id, e)
else
raise e
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/tire_async_index/workers/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def self.run(action_type, class_name, id)

def perform(action_type, class_name, id)
process(action_type, class_name, id)
rescue Exception => e
if TireAsyncIndex.error_handler
TireAsyncIndex.error_handler.handle(self, action_type, class_name, id, e)
else
raise e
end
end

end
Expand Down
9 changes: 9 additions & 0 deletions spec/active_record_tire_async_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def async_tire_object_id
end
}.to raise_error(TireAsyncIndex::EngineNotFound)
end

it "should be able to set a class as an error handler" do
TireAsyncIndex.configure do |config|
config.use_error_handler Object
end

TireAsyncIndex.error_handler.should eql Object
end
end

context "integration" do
Expand Down Expand Up @@ -158,3 +166,4 @@ def async_tire_object_id

end
end