https://github.com/rnd-soft/timeouter
[MIRROR] Timeouter is advisory timeout ruby helper without any background threads.
https://github.com/rnd-soft/timeouter
gem loop ruby ruby-on-rails timer
Last synced: 2 months ago
JSON representation
[MIRROR] Timeouter is advisory timeout ruby helper without any background threads.
- Host: GitHub
- URL: https://github.com/rnd-soft/timeouter
- Owner: RND-SOFT
- License: mit
- Created: 2019-09-23T07:17:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-03T15:02:13.000Z (about 2 years ago)
- Last Synced: 2025-04-08T16:17:29.047Z (2 months ago)
- Topics: gem, loop, ruby, ruby-on-rails, timer
- Language: Ruby
- Homepage: https://br.rnds.pro/ruby/timeouter
- Size: 22.5 KB
- Stars: 9
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Timeouter
[](https://rubygems.org/gems/timeouter)
[](https://rubygems.org/gems/timeouter/versions)
[](http://www.rubydoc.info/gems/timeouter)[](https://lysander.rnds.pro/api/v1/badges/timeouter_coverage.html)
[](https://lysander.rnds.pro/api/v1/badges/timeouter_quality.html)
[](https://lysander.rnds.pro/api/v1/badges/timeouter_outdated.html)
[](https://lysander.rnds.pro/api/v1/badges/timeouter_vulnerable.html)Timeouter is advisory timeout helper without any background threads.
# Usage
Typical usage scenario:
```ruby
require 'timeouter'Timeouter::run(3) do |t|
sleep 1 # do some workputs t.elapsed # 1.00011811
puts t.left # 1.99985717 or nil if timeout was 0
puts t.exhausted? # false or nil if timeout was 0
puts t.running? # true
puts t.running! # truesleep 3 # do another work
puts t.elapsed # 4.000177464
puts t.left # 0 or nil if timeout was 0
puts t.exhausted? # true or nil if timeout was 0
puts t.running? # false
puts t.running! # raise Timeouter::TimeoutError.new('execution expired')
end
```You can pass exception class and message on creation or on checking:
```ruby
Timeouter::run(1, eclass: RuntimeError, message: 'error') do |t|
sleep 2
puts t.running!(eclass: MyError, message: 'myerror')
end
```Loop helper:
```ruby
# just loop 3 seconds
Timeouter::loop(3) do |t|
puts "i'am in loop"
sleep 1
end# just loop 3 seconds and raise exception then
Timeouter::loop!(3, eclass: MyError) do |t|
puts "i'am in loop and not raised yet"
sleep 1
end# Break the loop after some success and retuel value
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:
```bash
gem install timeouter
```
There's also the wonders of [the Gemfile](http://bundler.io):
```ruby
gem 'timeouter'
```