Timeouter
Timeouter is advisory timeout helper without any background threads.
Usage
Typical usage scenario:
require 'timeouter'
Timeouter::run(3) do |t|
sleep 1
puts t.elapsed
puts t.left
puts t.exhausted?
puts t.running?
puts t.running!
sleep 3
puts t.elapsed
puts t.left
puts t.exhausted?
puts t.running?
puts t.running!
end
You can pass exception class and message on creation or on checking:
Timeouter::run(1, eclass: RuntimeError, message: 'error') do |t|
sleep 2
puts t.running!(eclass: MyError, message: 'myerror')
end
Loop helper:
Timeouter::loop(3) do |t|
puts "i'am in loop"
sleep 1
end
Timeouter::loop!(3, eclass: MyError) do |t|
puts "i'am in loop and not raised yet"
sleep 1
end
result = Timeouter::loop!(3) do |t|
puts "i'am in loop and not raised yet"
if t.elapsed > 1
puts "work done breaking loop"
break "RESULT"
end
sleep 1
end
Installation
It's a gem:
gem install timeouter
There's also the wonders of the Gemfile:
gem 'timeouter'