execution_deadline 0.2.1 → 0.3.0

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
  SHA256:
3
- metadata.gz: 26752e57bfd495b67049f6e31c7a5e22c7328d725a9e3495bc386a83245fd048
4
- data.tar.gz: 2935a78236bbc642be49b0d25762683de050d894899e94ea15ff6808d83caa85
3
+ metadata.gz: 7ce574cf89fbb0b9cf89f8fc2df628aa171e89eef75b85f4e3ef5e3fcc5a8cd5
4
+ data.tar.gz: b1d29c6a81aae730323444e6a302262239d72df16652fd2a592a4f3ea99f1662
5
5
  SHA512:
6
- metadata.gz: b46c4316e81573501116f7f66a82a95fef09d9461b147b10adf1804954f197f9f343b7a3d157cc63f25e2b1b6406f3d4cabfa863204acef449fd904e8bb8bf47
7
- data.tar.gz: ba245b71d80da4eb4cb47e91f70b4973af7cd681053cc3887cb7e321e005d2b81d0cd60ef357126e75c93143e1fbfd7d5305730eb9033d67a5f8a7050fe04dc6
6
+ metadata.gz: 5a1ea9cc41b1c35c2f72e7cdd2e467b6e3adb9cce912e118f214f4bbf8592bbea623a711440566bcee4a786418476df19f6513473074f0d2f3b536f9a2c78211
7
+ data.tar.gz: 6bb2ccd7d1d27195ed3782074a2f42872777776e26c50d2423362741626740c27f42c5d9fe298b159166c18f542f95ba122385533cd45cf9401026e86f82986b
data/README.md CHANGED
@@ -139,6 +139,23 @@ end
139
139
  SlowModule.perform # Throws OutOfTime error
140
140
  ```
141
141
 
142
+ ### Method interruption
143
+ Ruby's Timeout module is a bit heavy handed and could terminate operations at any point in your code flow. This gem
144
+ is an attempt at marking interruption points at the start and end of slow methods. It should always be preferred to
145
+ use a more graceful method, but sometimes a hammer is called for.
146
+
147
+ If you declare a deadline with `interruptible: true`. Ruby's Timeout module will be wrapped around your method set to
148
+ timeout at the remaining deadline time. The error will be replaced with the value of `raises` or the built in classes.
149
+
150
+ #### Example
151
+ ```
152
+ deadline runs_for: 0.6, interruptible: true
153
+ def self.sub_method_1
154
+ sleep 0.7
155
+ end
156
+ ```
157
+
158
+
142
159
  ### Raised Errors
143
160
  `ExecutionDeadline::OutOfTime` - Raised when a deadlined method is called but
144
161
  there is less time left then the expected runtime.
@@ -1,3 +1,5 @@
1
+ require 'timeout'
2
+
1
3
  module ExecutionDeadline
2
4
  module Deadliner
3
5
  WRAPPED_METHOD = Proc.new do |options|
@@ -7,10 +9,21 @@ module ExecutionDeadline
7
9
  raises: options[:raises]
8
10
  )
9
11
 
10
- ExecutionDeadline.current_deadline&.require_seconds_left!(options[:runs_for]) if options[:runs_for]
11
- super(*args, **kwargs, &blk).tap do
12
- ExecutionDeadline.current_deadline&.check_deadline_expiration!
12
+ current_deadline = ExecutionDeadline.current_deadline
13
+ current_deadline&.require_seconds_left!(options[:runs_for]) if options[:runs_for]
14
+
15
+ result = if !options[:interruptible]
16
+ super(*args, **kwargs, &blk)
17
+ else
18
+ Timeout.timeout(current_deadline&.time_left) do
19
+ super(*args, **kwargs, &blk)
20
+ rescue => Timeout::Error
21
+ end
13
22
  end
23
+
24
+ current_deadline&.check_deadline_expiration!
25
+
26
+ result
14
27
  ensure
15
28
  ExecutionDeadline.clear_deadline! if set_deadline
16
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ExecutionDeadline
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: execution_deadline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Malinconico
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-20 00:00:00.000000000 Z
11
+ date: 2020-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  requirements: []
99
- rubygems_version: 3.0.6
99
+ rubygems_version: 3.1.2
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Manage code deadlines without the hard termination of Timeout