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.
220
+
@defer_stop=true
221
+
returnfalse
222
+
end
223
+
215
224
# If the fiber is alive, we need to stop it:
216
225
if@fiber&.alive?
217
226
ifself.current?
@@ -239,6 +248,41 @@ def stop(later = false)
239
248
end
240
249
end
241
250
251
+
# 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.
252
+
#
253
+
# You can nest calls to defer_stop, but the stop will only be deferred until the outermost block exits.
254
+
#
255
+
# If stop is invoked a second time, it will be immediately executed.
256
+
#
257
+
# @yields {} The block of code to execute.
258
+
defdefer_stop
259
+
# Tri-state variable for controlling stop:
260
+
# - nil: defer_stop has not been called.
261
+
# - false: defer_stop has been called and we are not stopping.
262
+
# - true: defer_stop has been called and we will stop when exiting the block.
263
+
if@defer_stop.nil?
264
+
# If we are not deferring stop already, we can defer it now:
265
+
@defer_stop=false
266
+
267
+
begin
268
+
yield
269
+
rescueStop
270
+
# If we are exiting due to a stop, we shouldn't try to invoke stop again:
271
+
@defer_stop=nil
272
+
raise
273
+
ensure
274
+
# If we were asked to stop, we should do so now:
275
+
if@defer_stop
276
+
@defer_stop=nil
277
+
self.stop
278
+
end
279
+
end
280
+
else
281
+
# If we are deferring stop already, entering it again is a no-op.
282
+
yield
283
+
end
284
+
end
285
+
242
286
# Lookup the {Task} for the current fiber. Raise `RuntimeError` if none is available.
243
287
# @returns [Task]
244
288
# @raises[RuntimeError] If task was not {set!} for the current fiber.
0 commit comments