Created
July 13, 2012 14:37
-
-
Save jjb/3105236 to your computer and use it in GitHub Desktop.
Problems with Timeout.timeout's use of Thread#raise
This file contains hidden or 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
require 'timeout' | |
def process_foos(error_to_rescue) | |
begin | |
# -> process Foos | |
# -> if we run out of Foos, raise | |
sleep 2 | |
rescue error_to_rescue | |
# -> email the manager that we ran out of Foos | |
puts <<-MESSAGE | |
There was a problem. The problem is we ran out of Foos. | |
That is definitely what the problem was. | |
Don't worry, we emailed the Foo manager and elegantly carried on | |
into the outer context. | |
MESSAGE | |
end | |
end | |
begin | |
puts "Calling some poorly-written code." | |
Timeout.timeout(1){ process_foos(Exception) } | |
rescue Timeout::Error | |
puts "Processing the Foos took too long." | |
end | |
begin | |
puts "\nCalling some well-written code." | |
# even better would be FooError | |
Timeout.timeout(1){ process_foos(StandardError) } | |
rescue Timeout::Error | |
puts "Processing the Foos took too long." | |
end |
This file contains hidden or 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
➔ ruby code.rb | |
Calling some poorly-written code. | |
There was a problem. The problem is we ran out of Foos. | |
That is definitely what the problem was. | |
Don't worry, we emailed the Foo manager and elegantly carried on | |
into the outer context. | |
Calling some well-written code. | |
Processing the Foos took too long. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment