Skip to content

Commit 42d7646

Browse files
committed
Added 'nuclear option' Concurrent.disable_auto_termination_of_all_executors! method.
1 parent 6d2f821 commit 42d7646

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

lib/concurrent/configuration.rb

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,115 @@ module Concurrent
1010

1111
# initialize the global executors
1212
class << self
13+
14+
# @!visibility private
1315
@@auto_terminate_global_executors = Concurrent::AtomicBoolean.new(true)
1416

17+
# @!visibility private
18+
@@auto_terminate_all_executors = Concurrent::AtomicBoolean.new(true)
19+
20+
# @!visibility private
1521
@@global_fast_executor = LazyReference.new do
1622
Concurrent.new_fast_executor(
1723
stop_on_exit: @@auto_terminate_global_executors.value)
1824
end
1925

26+
# @!visibility private
2027
@@global_io_executor = LazyReference.new do
2128
Concurrent.new_io_executor(
2229
stop_on_exit: @@auto_terminate_global_executors.value)
2330
end
2431

32+
# @!visibility private
2533
@@global_timer_set = LazyReference.new do
2634
Concurrent::TimerSet.new(
2735
stop_on_exit: @@auto_terminate_global_executors.value)
2836
end
2937
end
3038

31-
# defines if executors should be auto-terminated with `at_exit` callback
39+
# Defines if global executors should be auto-terminated with an
40+
# `at_exit` callback. When set to `false` it will be the application
41+
# programmer's responsibility to ensure that the global thread pools
42+
# are shutdown properly prior to application exit.
43+
#
44+
# @note Only change this option if you know what you are doing!
45+
# When this is set to true (the default) then `at_exit` handlers
46+
# will be registered automatically for the *global* thread pools
47+
# to ensure that they are shutdown when the application ends. When
48+
# changed to false, the `at_exit` handlers will be circumvented
49+
# for all *global* thread pools. This method should *never* be called
50+
# from within a gem. It should *only* be used from within the main
51+
# application and even then it should be used only when necessary.
52+
#
3253
def self.disable_auto_termination_of_global_executors!
3354
@@auto_terminate_global_executors.make_false
3455
end
3556

36-
# defines if executors should be auto-terminated with `at_exit` callback
57+
# Defines if global executors should be auto-terminated with an
58+
# `at_exit` callback. When set to `false` it will be the application
59+
# programmer's responsibility to ensure that the global thread pools
60+
# are shutdown properly prior to application exit.
61+
#
62+
# @note Only change this option if you know what you are doing!
63+
# When this is set to true (the default) then `at_exit` handlers
64+
# will be registered automatically for the *global* thread pools
65+
# to ensure that they are shutdown when the application ends. When
66+
# changed to false, the `at_exit` handlers will be circumvented
67+
# for all *global* thread pools. This method should *never* be called
68+
# from within a gem. It should *only* be used from within the main
69+
# application and even then it should be used only when necessary.
70+
#
71+
# @return [Boolean] true when global thread pools will auto-terminate on
72+
# application exit using an `at_exit` handler; false when no auto-termination
73+
# will occur.
3774
def self.auto_terminate_global_executors?
3875
@@auto_terminate_global_executors.value
3976
end
4077

78+
# Defines if *ALL* executors should be auto-terminated with an
79+
# `at_exit` callback. When set to `false` it will be the application
80+
# programmer's responsibility to ensure that *all* thread pools,
81+
# including the global thread pools, are shutdown properly prior to
82+
# application exit.
83+
#
84+
# @note Only change this option if you know what you are doing!
85+
# When this is set to true (the default) then `at_exit` handlers
86+
# will be registered automatically for *all* thread pools to
87+
# ensure that they are shutdown when the application ends. When
88+
# changed to false, the `at_exit` handlers will be circumvented
89+
# for *all* Concurrent Ruby thread pools running within the
90+
# application. Even those created within other gems used by the
91+
# application. This method should *never* be called from within a
92+
# gem. It should *only* be used from within the main application.
93+
# And even then it should be used only when necessary.
94+
def self.disable_auto_termination_of_all_executors!
95+
@@auto_terminate_all_executors.make_false
96+
end
97+
98+
# Defines if *ALL* executors should be auto-terminated with an
99+
# `at_exit` callback. When set to `false` it will be the application
100+
# programmer's responsibility to ensure that *all* thread pools,
101+
# including the global thread pools, are shutdown properly prior to
102+
# application exit.
103+
#
104+
# @note Only change this option if you know what you are doing!
105+
# When this is set to true (the default) then `at_exit` handlers
106+
# will be registered automatically for *all* thread pools to
107+
# ensure that they are shutdown when the application ends. When
108+
# changed to false, the `at_exit` handlers will be circumvented
109+
# for *all* Concurrent Ruby thread pools running within the
110+
# application. Even those created within other gems used by the
111+
# application. This method should *never* be called from within a
112+
# gem. It should *only* be used from within the main application.
113+
# And even then it should be used only when necessary.
114+
#
115+
# @return [Boolean] true when *all* thread pools will auto-terminate on
116+
# application exit using an `at_exit` handler; false when no auto-termination
117+
# will occur.
118+
def self.auto_terminate_all_executors?
119+
@@auto_terminate_all_executors.value
120+
end
121+
41122
# Global thread pool optimized for short, fast *operations*.
42123
#
43124
# @return [ThreadPoolExecutor] the thread pool

lib/concurrent/executor/executor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def enable_at_exit_handler!(opts = {})
7171
# without this the application may fail to exit
7272
@auto_terminate = true
7373
this = self
74-
at_exit { this.kill }
74+
at_exit { this.kill if Concurrent.auto_terminate_all_executors? }
7575
end
7676
end
7777
end

0 commit comments

Comments
 (0)