timeouter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9f10b6b4a402f7a2eda65f18f71e14bcf5745616a81f7793b9051a1dd3b09501
4
+ data.tar.gz: 916cb40b55973b7343abdb134ce16f2068828f5dee28a4c9205290b767046ea9
5
+ SHA512:
6
+ metadata.gz: 716982b408f28ff28d656917e6085c4731a9b57ac3238673997399285315311e96d3ddc2f48c6077f7a61de08808a561a0dc2b264ed184cbe28f48be30e42873
7
+ data.tar.gz: b89ee00299bf83fb6a7bfb269eff51727e24288346acae5f22dfb9630ad3c3a16576b71c2159e80acfbff8f13696741b206088007192998a40004886aa113de9
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Timeouter
2
+
3
+ Timeouter is advisory timeout helper without any background threads.
4
+
5
+ # Usage
6
+
7
+ Typical usage scenario:
8
+
9
+ ```ruby
10
+ require 'timeouter'
11
+
12
+ Timeouter::run(3) do |t|
13
+ sleep 1 # do some work
14
+
15
+ puts t.elapsed # 1.00011811
16
+ puts t.left # 3.99985717 or nil if timeout was 0
17
+ puts t.exhausted? # false or nil if timeout was 0
18
+ puts t.running? # true
19
+ puts t.running! # true
20
+
21
+ sleep 3 # do another work
22
+
23
+ puts t.elapsed # 4.000177464
24
+ puts t.left # 0 or nil if timeout was 0
25
+ puts t.exhausted? # true or nil if timeout was 0
26
+ puts t.running? # false
27
+ puts t.running! # raise Timeouter::TimeoutError.new('execution expired')
28
+ end
29
+ ```
30
+
31
+ You can pass exception class and message on creation or on checking:
32
+
33
+ ```ruby
34
+ Timeouter::run(1, eclass: RuntimeError, emessage: 'error') do |t|
35
+ sleep 2
36
+ puts t.running!(eclass: MyError, emessage: 'myerror')
37
+ end
38
+ ```
39
+
40
+ Loop helper:
41
+
42
+ ```ruby
43
+ # just loop 3 seconds
44
+ Timeouter::loop(3) do |t|
45
+ puts "i'am in loop"
46
+ sleep 1
47
+ end
48
+
49
+ # just loop 3 seconds and raise exception then
50
+ Timeouter::loop!(3, eclass: MyError) do |t|
51
+ puts "i'am in loop and not raised yet"
52
+ sleep 1
53
+ end
54
+
55
+ # Break the loop after some success and retuel value
56
+ result = Timeouter::loop!(3) do |t|
57
+ puts "i'am in loop and not raised yet"
58
+ if t.elapsed > 1
59
+ puts "work done breaking loop"
60
+ break "RESULT"
61
+ end
62
+ sleep 1
63
+ end
64
+ ```
65
+
66
+ # Installation
67
+
68
+ It's a gem:
69
+ ```bash
70
+ gem install timeouter
71
+ ```
72
+ There's also the wonders of [the Gemfile](http://bundler.io):
73
+ ```ruby
74
+ gem 'timeouter'
75
+ ```
76
+
77
+
@@ -0,0 +1,56 @@
1
+ require 'timeout'
2
+
3
+ module Timeouter
4
+ class Timer
5
+
6
+ attr_reader :started_at, :exhausted_at
7
+
8
+ def initialize(timeout = 0, eclass: Timeouter::TimeoutError, emessage: 'execution expired')
9
+ # ensure only positive timeouts
10
+ timeout ||= 0
11
+ timeout = [timeout, 0].max
12
+
13
+ @eclass = eclass
14
+ @emessage = emessage
15
+
16
+ @started_at = Time.now
17
+ @exhausted_at = timeout > 0 ? @started_at + timeout : nil
18
+ end
19
+
20
+ # elapsed time from creation
21
+ def elapsed
22
+ Time.now - @started_at
23
+ end
24
+
25
+ # time left to be exhausted or nil if timeout was 0
26
+ def left
27
+ @exhausted_at && [@exhausted_at - Time.now, 0].max
28
+ end
29
+
30
+ # is timeout exhausted? or nil when timeout was 0
31
+ def exhausted?
32
+ @exhausted_at && (@exhausted_at < Time.now)
33
+ end
34
+
35
+ # is timeout NOT exhausted? or true when timeout was 0
36
+ def running?
37
+ !exhausted?
38
+ end
39
+
40
+ # ensure timeout NOT exhausted raise exception otherwise
41
+ def running!(eclass = @eclass, message: @emessage)
42
+ !exhausted? || (raise eclass.new(message))
43
+ end
44
+
45
+ # run block in loop until timeout reached. Use break for returning result
46
+ def loop
47
+ yield(self) while self.running?
48
+ end
49
+
50
+ def loop!(eclass = @eclass, message: @emessage)
51
+ yield(self) while self.running!(eclass, message: message)
52
+ end
53
+
54
+ end
55
+ end
56
+
@@ -0,0 +1,6 @@
1
+ module Timeouter
2
+
3
+ VERSION = '0.1.0'.freeze
4
+
5
+ end
6
+
data/lib/timeouter.rb ADDED
@@ -0,0 +1,25 @@
1
+ require_relative 'timeouter/timer'
2
+
3
+
4
+ module Timeouter
5
+
6
+ TimeoutError = Timeout::Error
7
+
8
+ class << self
9
+
10
+ def run(timeout = 0, eclass: Timeouter::TimeoutError, emessage: 'execution expired')
11
+ yield(Timeouter::Timer.new(timeout, eclass: eclass, emessage: emessage))
12
+ end
13
+
14
+ def loop(timeout = 0, eclass: Timeouter::TimeoutError, emessage: 'execution expired', &block)
15
+ Timeouter::Timer.new(timeout, eclass: eclass, emessage: emessage).loop(&block)
16
+ end
17
+
18
+ def loop!(timeout = 0, eclass: Timeouter::TimeoutError, emessage: 'execution expired', &block)
19
+ Timeouter::Timer.new(timeout, eclass: eclass, emessage: emessage).loop!(&block)
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timeouter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samoilenko Yuri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.1
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: webmock
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: Timeouter is advisory timeout helper
90
+ email:
91
+ - kinnalru@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - README.md
97
+ - lib/timeouter.rb
98
+ - lib/timeouter/timer.rb
99
+ - lib/timeouter/version.rb
100
+ homepage: https://github.com/RnD-Soft/timeouter
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.7.9
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Timeouter is advisory timeout helper
124
+ test_files: []