Skip to content

Instantly share code, notes, and snippets.

@shiroemons
Last active October 29, 2019 14:50
Show Gist options
  • Save shiroemons/e87acfb85e83d5eba926379097ddca51 to your computer and use it in GitHub Desktop.
Save shiroemons/e87acfb85e83d5eba926379097ddca51 to your computer and use it in GitHub Desktop.
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