3
3
4
4
module Concurrent
5
5
6
- # Provides ability to add and remove hooks to be run at `Kernel#at_exit`, order is undefined.
7
- # Each hook is executed at most once.
6
+ # Provides ability to add and remove handlers to be run at `Kernel#at_exit`, order is undefined.
7
+ # Each handler is executed at most once.
8
8
class AtExitImplementation < Synchronization ::Object
9
9
include Logging
10
10
11
11
def initialize ( enabled = true )
12
12
super ( )
13
13
synchronize do
14
- @hooks = { }
15
- @enabled = enabled
14
+ @handlers = { }
15
+ @enabled = enabled
16
16
end
17
17
end
18
18
19
- # Add a hook to be run at `Kernel#at_exit`
20
- # @param [Object] hook_id optionally provide an id, if allready present, hook is replaced
21
- # @yield the hook
22
- # @return id of the hook
23
- def add ( hook_id = nil , &hook )
24
- id = hook_id || hook . object_id
25
- synchronize { @hooks [ id ] = hook }
19
+ # Add a handler to be run at `Kernel#at_exit`
20
+ # @param [Object] handler_id optionally provide an id, if allready present, handler is replaced
21
+ # @yield the handler
22
+ # @return id of the handler
23
+ def add ( handler_id = nil , &handler )
24
+ id = handler_id || handler . object_id
25
+ synchronize { @handlers [ id ] = handler }
26
26
id
27
27
end
28
28
29
- # Delete a hook by hook_id
29
+ # Delete a handler by handler_id
30
30
# @return [true, false]
31
- def delete ( hook_id )
32
- !!synchronize { @hooks . delete hook_id }
31
+ def delete ( handler_id )
32
+ !!synchronize { @handlers . delete handler_id }
33
33
end
34
34
35
- # Is hook with hook_id rpesent?
35
+ # Is handler with handler_id rpesent?
36
36
# @return [true, false]
37
- def hook? ( hook_id )
38
- synchronize { @hooks . key? hook_id }
37
+ def handler? ( handler_id )
38
+ synchronize { @handlers . key? handler_id }
39
39
end
40
40
41
- # @return copy of the hooks
42
- def hooks
43
- synchronize { @hooks } . clone
41
+ # @return copy of the handlers
42
+ def handlers
43
+ synchronize { @handlers } . clone
44
44
end
45
45
46
- # install `Kernel#at_exit` callback to execute added hooks
46
+ # install `Kernel#at_exit` callback to execute added handlers
47
47
def install
48
48
synchronize do
49
49
@installed ||= begin
@@ -64,18 +64,18 @@ def enabled=(value)
64
64
synchronize { @enabled = value }
65
65
end
66
66
67
- # run the hooks manually
68
- # @return ids of the hooks
67
+ # run the handlers manually
68
+ # @return ids of the handlers
69
69
def run
70
- hooks , _ = synchronize { hooks , @hooks = @hooks , { } }
71
- hooks . each do |_ , hook |
70
+ handlers , _ = synchronize { handlers , @handlers = @handlers , { } }
71
+ handlers . each do |_ , handler |
72
72
begin
73
- hook . call
73
+ handler . call
74
74
rescue => error
75
75
log ERROR , error
76
76
end
77
77
end
78
- hooks . keys
78
+ handlers . keys
79
79
end
80
80
81
81
private
0 commit comments