soft_timeout 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6dd318cdb43083bbd0126ee660d0fc9a3bbbe93
4
- data.tar.gz: cd3cfaa19f1b9e22d8143e58d5c95b9ef0ec34ec
3
+ metadata.gz: 3663665657cf0c578776b56b809c364534d28f1d
4
+ data.tar.gz: c0b0705764ffef24afe1f2fb0eb8861fba512419
5
5
  SHA512:
6
- metadata.gz: 343a01526882dd69c21fe660a2660bb67e145b0acf44de11fedc4e5b4bc255f5ed024ead2faa4117d2466878f6cb036ce44ee83094653aa664509b41902f8ae2
7
- data.tar.gz: 27563618cabfbe4379ae3ac93636a04c6db12ea13077162e6d15e5bc568e432898973f23a46ed4aef79da945d43481f6f65fbbba4999820ef15edca6633bf3b9
6
+ metadata.gz: 915aca7255047a1e83eaaaec1d21933d778e7f6ae92ca2bc1c4956e8748f9c06c9e93eeea5c210715deaef4d8db8991b10fff7c0057c459426b1e2f427e49e1f
7
+ data.tar.gz: b08db2bbd08c9b41bfb82746d9d7e4740d6a22739f36de89db84e659cfa81adba387e4ceacc73e6b009216df3f9bf2e908585620ec32524712975addf6f3da74
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # SoftTimeout
2
2
 
3
- SoftTimeout provides feature to set soft expiry time before raising actual Timeout Exception.
3
+ SoftTimeout provides feature to set soft expiry time before raising actual Timeout Exception. It allows you to run custom code before raising Timeout::Error.
4
+
5
+ Takes soft expiry, hard exipry and a block as argument. Executes the block after soft expiry time(so that you can set flags to start wrapping up. See the example below). Raises Timeout error after hard expiry time seconds(as normal Timout behaviour).
4
6
  ## Installation
5
7
 
6
8
  Add this line to your application's Gemfile:
@@ -25,20 +27,27 @@ class MyClass
25
27
  end
26
28
 
27
29
  def some_important_task
30
+ # Soft timeout at 10 sec. and Raise Timeout::Error after 20 sec
28
31
  timeout = SoftTimeout::Timeout.new(10, 20) do
32
+ #This block will be executed after soft expiry time is reached(10 secs here)
29
33
  @continue_running = false
30
34
  end
31
35
 
36
+ # Keep checkig if flag is still set to true and then process the chunk. else exit gracefully
37
+ # It will raise Timeout::Error if following block runs for more than hard expiry time(20 secs)
32
38
  timeout.soft_timeout do
33
- 100.times do |n|
39
+ 10.times do |n|
40
+ # After soft expiry seconds, the (above)block will be executed and the flag will be set to false.
34
41
  if @continue_running
35
42
  ...
36
- some heavy processing (sleep(2))
43
+ some heavy but critical processing which should not be interepted in between
37
44
  ...
38
45
  else
39
46
  puts 'soft timeout reached'
47
+ ..Finish work, release locks etc...
40
48
  return
41
49
  end
50
+
42
51
  end
43
52
  end
44
53
  end
data/lib/soft_timeout.rb CHANGED
@@ -27,39 +27,19 @@ module SoftTimeout
27
27
  # +error_class+:: Exception class to raise
28
28
  # +block+:: Proc to be run at end of +soft_expiry+
29
29
  #
30
- def initialize(soft_expiry, hard_expiry, error_class = ::Timeout::Error, &block)
30
+ def initialize(soft_expiry, hard_expiry, error_class = nil, &block)
31
31
  self.soft_expiry = soft_expiry
32
32
  self.hard_expiry = hard_expiry
33
33
  self.error_class = error_class
34
34
  self.on_soft_timeout = block if block_given?
35
35
  end
36
36
 
37
- def soft_timeout(&block)
37
+ def soft_timeout
38
38
  return yield(soft_expiry) if soft_expiry == nil || soft_expiry < 0 || hard_expiry == nil || hard_expiry < 0 || hard_expiry <= soft_expiry
39
- run_proc_with_error_handling(build_timeout_proc(&block))
40
- end
41
-
42
- #nodoc
43
- def run_proc_with_error_handling(timeout_proc)
44
39
  message = "execution expired".freeze
45
- begin
46
- return timeout_proc.call(error_class)
47
- rescue error_class => e
48
- bt = e.backtrace
49
- end
50
40
 
51
- level = -caller(CALLER_OFFSET).size - 1
52
- while THIS_FILE =~ bt[level]
53
- bt.delete_at(level)
54
- end
55
- raise(error_class, message, bt)
56
- end
57
-
58
- #nodoc
59
- def build_timeout_proc
60
- message = "execution expired".freeze
61
-
62
- proc do |exception|
41
+ e = ::Timeout::Error
42
+ bl = proc do |exception|
63
43
  begin
64
44
  original_thread = Thread.current
65
45
  timeout_thread = Thread.start {
@@ -79,7 +59,7 @@ module SoftTimeout
79
59
  original_thread.raise exception, message
80
60
  end
81
61
  }
82
- yield(soft_expiry)
62
+ return yield(soft_expiry)
83
63
  ensure
84
64
  if timeout_thread
85
65
  timeout_thread.kill
@@ -87,8 +67,25 @@ module SoftTimeout
87
67
  end
88
68
  end
89
69
  end
90
- end
91
- end
70
+
71
+ # Why this strange Error catch and all. checkout http://stackoverflow.com/questions/35447230/ruby-timeout-behaves-differently-between-2-0-and-2-1
72
+ if error_class
73
+ begin
74
+ bl.call(error_class)
75
+ rescue error_class => l
76
+ bt = l.backtrace
77
+ end
78
+ else
79
+ bt = ::Timeout::Error.catch(message, &bl)
80
+ end
92
81
 
93
82
 
83
+ level = -caller(CALLER_OFFSET).size-2
84
+ while THIS_FILE =~ bt[level]
85
+ bt.delete_at(level)
86
+ end
87
+ raise(e, message, bt)
88
+ end
89
+
90
+ end
94
91
  end
@@ -1,3 +1,3 @@
1
1
  module SoftTimeout
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soft_timeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abhishek Patel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-28 00:00:00.000000000 Z
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler