execution_deadline 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab73aac3e9d983dcbeae9b4ecfee3260588c84a73bc5371168f49702e54f9f01
4
- data.tar.gz: f9b60401c580d89763e9e56be22494169d8706056b4b0a8300fb482b3c9f4390
3
+ metadata.gz: 982f5c8deaa2d7dd9cfec996423887315caeeda3e3933ef0ec0eb8bfdce7676f
4
+ data.tar.gz: c2b2e6b4d63b8ad17c6ddf06fec10c791db4f883f7a4befef14ea738e6dacc74
5
5
  SHA512:
6
- metadata.gz: 877beba2aab5d7e4098dd574cd5fd1fa8a666e01263657c20bf90ef2a5c6a6505d1404ff82404e3e614296e91283a7b8afdd4e94d9aff4aa6630d00b1015f5ff
7
- data.tar.gz: 40a4e52623b308287683dbe5a58901bd252ba2ed13ac0e5c11259abf91353231e1d32a761e9cb783e1a0ca5bae0a07bc9cdcf5bcc3a0e1942aeafc44b66cc0af
6
+ metadata.gz: 541f8a0246f5ede73be6ffb85b20c3787e983a5e22f5b96eafd91b939184bd00e032f2150d37208c281e8cc090c2de2b1ba43d8503c71f75387b1b99915816b8
7
+ data.tar.gz: 3a73e2da652259b95eae09ceaa393dfa3167f1123cd86a23d1cd757b6dc613d64a115ca92facbfc7b2edff73a637d4fd60ebd13fa9d2ae5d0977d4a80d3d6640
@@ -20,4 +20,12 @@ module ExecutionDeadline
20
20
  !current_deadline &&
21
21
  Thread.current[:deadline] = Deadline.new(expires_at: expires_at, raises: raises)
22
22
  end
23
+
24
+ def self.extended(other)
25
+ other.extend(Helpers)
26
+ end
27
+
28
+ def self.included(other)
29
+ other.extend(Helpers)
30
+ end
23
31
  end
@@ -0,0 +1,35 @@
1
+ module ExecutionDeadline
2
+ module Deadliner
3
+ WRAPPED_METHOD = Proc.new do |options|
4
+ Proc.new do |*args, **kwargs, &blk|
5
+ set_deadline = options[:in] && ExecutionDeadline.set_deadline(
6
+ expires_at: Time.now + options[:in],
7
+ raises: options[:raises]
8
+ )
9
+
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!
13
+ end
14
+ ensure
15
+ ExecutionDeadline.clear_deadline! if set_deadline
16
+ end
17
+ end
18
+
19
+ def inspect
20
+ "ExecutionDeadline::#{@_execution_deadline_built_for}Proxy"
21
+ end
22
+
23
+ def _execution_deadline_built_for
24
+ @_execution_deadline_built_for
25
+ end
26
+
27
+ def _execution_deadline_built_for=(val)
28
+ @_execution_deadline_built_for = val
29
+ end
30
+
31
+ def wrap_implementation(method_name, config)
32
+ define_method(method_name, &WRAPPED_METHOD.call(config))
33
+ end
34
+ end
35
+ end
@@ -2,26 +2,10 @@
2
2
 
3
3
  require 'execution_deadline/version'
4
4
  require 'execution_deadline/deadline'
5
+ require 'execution_deadline/method_proxy'
5
6
 
6
7
  module ExecutionDeadline
7
8
  module Helpers
8
- UNWRAPPED_METHOD_NAME_SUFFIX = "_without_deadline"
9
- WRAPPED_METHOD = Proc.new do |options|
10
- Proc.new do |*args, &blk|
11
- set_deadline = options[:in] && ExecutionDeadline.set_deadline(
12
- expires_at: Time.now + options[:in],
13
- raises: options[:raises]
14
- )
15
-
16
- ExecutionDeadline.current_deadline&.require_seconds_left!(options[:runs_for]) if options[:runs_for]
17
- send(options[:aliased_method_name], *args, &blk).tap do
18
- ExecutionDeadline.current_deadline&.check_deadline_expiration!
19
- end
20
- ensure
21
- ExecutionDeadline.clear_deadline! if set_deadline
22
- end
23
- end
24
-
25
9
  def deadline(options = {})
26
10
  options[:in] ||
27
11
  options[:runs_for] ||
@@ -31,28 +15,25 @@ module ExecutionDeadline
31
15
  end
32
16
 
33
17
  def method_added(method_name)
34
- return super unless _has_deadline_config?
18
+ super
35
19
 
36
- options = _fetch_and_reset_deadline_config
37
- options[:aliased_method_name] ||= "_#{method_name}#{UNWRAPPED_METHOD_NAME_SUFFIX}".to_sym
20
+ return unless _has_deadline_config?
38
21
 
39
- alias_method options[:aliased_method_name], method_name
22
+ ExecutionDeadline::MethodProxy
23
+ .for_class(self)
24
+ .wrap_implementation(method_name, _fetch_and_reset_deadline_config)
40
25
 
41
- define_method(method_name, &WRAPPED_METHOD.call(options))
26
+ super
42
27
  end
43
28
 
44
29
  def singleton_method_added(method_name)
45
- return super unless _has_deadline_config?
46
-
47
- options = _fetch_and_reset_deadline_config
30
+ super
48
31
 
49
- options[:aliased_method_name] ||= "_#{method_name}#{UNWRAPPED_METHOD_NAME_SUFFIX}".to_sym
32
+ return unless _has_deadline_config?
50
33
 
51
- singleton_class.class_eval do
52
- alias_method options[:aliased_method_name], method_name
53
- end
54
-
55
- define_singleton_method(method_name, &WRAPPED_METHOD.call(options))
34
+ ExecutionDeadline::MethodProxy
35
+ .for_class(singleton_class)
36
+ .wrap_implementation(method_name, _fetch_and_reset_deadline_config)
56
37
  end
57
38
 
58
39
  private
@@ -64,9 +45,5 @@ module ExecutionDeadline
64
45
  def _fetch_and_reset_deadline_config
65
46
  @last_deadline_config.tap { @last_deadline_config = nil }
66
47
  end
67
-
68
- def _add_deadlined_method(method_name, options)
69
- end
70
-
71
48
  end
72
49
  end
@@ -0,0 +1,28 @@
1
+ require 'execution_deadline/deadliner'
2
+
3
+ module ExecutionDeadline
4
+ module MethodProxy
5
+ def self.for_class(klass)
6
+ find_for_class(klass) || install_on_class(klass)
7
+ end
8
+
9
+ def self.find_for_class(klass)
10
+ klass.ancestors.detect do |a|
11
+ a.is_a?(Deadliner) &&
12
+ a._execution_deadline_built_for == klass
13
+ end
14
+ end
15
+
16
+ def self.install_on_class(klass)
17
+ construct_for_class(klass).tap do |m|
18
+ klass.prepend(m)
19
+ end
20
+ end
21
+
22
+ def self.construct_for_class(klass)
23
+ Module.new do
24
+ extend Deadliner
25
+ end.tap { |m| m._execution_deadline_built_for = klass }
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ExecutionDeadline
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.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.1.1
4
+ version: 0.2.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: 2019-09-30 00:00:00.000000000 Z
11
+ date: 2020-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,8 +72,10 @@ files:
72
72
  - execution_deadline.gemspec
73
73
  - lib/execution_deadline.rb
74
74
  - lib/execution_deadline/deadline.rb
75
+ - lib/execution_deadline/deadliner.rb
75
76
  - lib/execution_deadline/errors.rb
76
77
  - lib/execution_deadline/helpers.rb
78
+ - lib/execution_deadline/method_proxy.rb
77
79
  - lib/execution_deadline/version.rb
78
80
  homepage: https://github.com/arjes/execution_deadline
79
81
  licenses: