You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Don't stop now... but update the state so we know we need to stop later.
170
+
@defer_stop=true
171
+
returnfalse
172
+
end
173
+
165
174
ifself.running?
166
175
ifself.current?
167
176
iflater
@@ -182,6 +191,41 @@ def stop(later = false)
182
191
end
183
192
end
184
193
194
+
# Defer the handling of stop. During the execution of the given block, if a stop is requested, it will be deferred until the block exits. This is useful for ensuring graceful shutdown of servers and other long-running tasks. You should wrap the response handling code in a defer_stop block to ensure that the task is stopped when the response is complete but not before.
195
+
#
196
+
# You can nest calls to defer_stop, but the stop will only be deferred until the outermost block exits.
197
+
#
198
+
# If stop is invoked a second time, it will be immediately executed.
199
+
#
200
+
# @yields {} The block of code to execute.
201
+
defdefer_stop
202
+
# Tri-state variable for controlling stop:
203
+
# - nil: defer_stop has not been called.
204
+
# - false: defer_stop has been called and we are not stopping.
205
+
# - true: defer_stop has been called and we will stop when exiting the block.
206
+
if@defer_stop.nil?
207
+
# If we are not deferring stop already, we can defer it now:
208
+
@defer_stop=false
209
+
210
+
begin
211
+
yield
212
+
rescueStop
213
+
# If we are exiting due to a stop, we shouldn't try to invoke stop again:
214
+
@defer_stop=nil
215
+
raise
216
+
ensure
217
+
# If we were asked to stop, we should do so now:
218
+
if@defer_stop
219
+
@defer_stop=nil
220
+
self.stop
221
+
end
222
+
end
223
+
else
224
+
# If we are deferring stop already, entering it again is a no-op.
225
+
yield
226
+
end
227
+
end
228
+
185
229
# Lookup the {Task} for the current fiber. Raise `RuntimeError` if none is available.
186
230
# @return [Async::Task]
187
231
# @raise [RuntimeError] if task was not {set!} for the current fiber.
0 commit comments