Rescue_each is a project mainly written in Ruby, based on the MIT license.
Rescue multiple exceptions when enumerating over Enumerable or ActiveRecord objects
rescue_each
Say you have a batch rake task which runs from cron which consists of many independent tasks ran in a loop. These tasks could be anything from updating cached database entries to file conversions.
Once of these tasks fails, perhaps there's a corrupt image. Normally this would mean the entire batch task fails. But with rescue_each the other items can be processed and any errors will be re-raised at the end to be caught by your cron script.
You can install from Gemcutter by running:
sudo gem install rescue_each
Simply replace your each
calls with rescue_each
:
BatchTasks.all.each &:process!
transforms into:
BatchTasks.all.rescue_each &:process!
rescue_each
also supports a option to output error summary info to stderr
during the loop:
(1..5).rescue_each :stderr => true do |i|
sleep 1
raise 'example'
end
You'll probably find this handy if your batch task has its own status output as this mode will output an error summary inline.
rescue_each
also provides proxies for map
, each_with_index
, find_each
and find_in_batches
.
You can also use rescue_each
on any method taking a block by calling rescue_send
:
odds = (1..5).rescue_send(:reject) { |i| i%2 == 0 }
Copyright (c) 2010 Jason Weathered. See LICENSE for details.