Last active
October 29, 2019 14:50
-
-
Save shiroemons/e87acfb85e83d5eba926379097ddca51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFatalError < StandardError; end | |
class MyRetryableError < StandardError; end | |
class MyBatchExecutor | |
def initialize(*items) | |
@items = items | |
end | |
def execute | |
@items.each.with_index(1) do |item, i| | |
retry_count = 0 | |
begin | |
item.call | |
rescue MyFatalError => e | |
message(i, e) | |
return | |
rescue MyRetryableError => e | |
message(i, e) | |
retry_count += 1 | |
retry_count == 1 ? retry : next | |
rescue => e | |
message(i, e) | |
next | |
end | |
end | |
puts 'Batch complete' | |
end | |
def message(i, e) | |
puts "Item #{i} failed: #{e.class}, #{e.message}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment