Skip to content

Instantly share code, notes, and snippets.

@jjb
Created July 13, 2012 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjb/3105236 to your computer and use it in GitHub Desktop.
Save jjb/3105236 to your computer and use it in GitHub Desktop.
Problems with Timeout.timeout's use of Thread#raise
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
➔ 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