async 2.9.0 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e93ed3ea9094d074f430f9518bf0087f57abf732bf196bed5cf8249315aac33d
4
- data.tar.gz: 233dffa50c8bb88cd514545280e3bef088b998a1ddb58e6db68d1a7c8a268c32
3
+ metadata.gz: 707788679dfee0d00262df41dca98842740769f25f38b4ec72f3a0fe0af466fb
4
+ data.tar.gz: 543acd4f573f1665cda010fd6b15d7d24ec14eb5de39cc873ef8b4d1b1cc7f8e
5
5
  SHA512:
6
- metadata.gz: bede7ace58ac0e5f6bbefd05a89855a3f80ddf3d02a441aaa73b0a64adeb82b83d02d064121ae267b719519a6fc03f86b7b586f8af3901ae12fcfc6db2d5612c
7
- data.tar.gz: a093ddf356f924089fc8500bee1570cac0e7a4a2e131127eeae0b65813905ae853f44e448f18ecda3d55e39d33f2c7e96c41a34f53cc02259ac8b5bef02e857a
6
+ metadata.gz: a6be161ee5c42ebcdc0b26ed054b749900060fab3a0601255d7fb038feb063282cd13e8917f79700fc8379996903ea7cb9796c083f348b70f85f6ae363e348ca
7
+ data.tar.gz: ded0df5f2a950f143819bfe94537b188c0e66426344f0d21e0f5e9ed240651b2cafed41d6d5da36a123793cc6406b0ffbc5b8adb1e834faddd168f96855b02c4
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/async/task.rb CHANGED
@@ -67,6 +67,8 @@ module Async
67
67
  @status = :initialized
68
68
  @result = nil
69
69
  @finished = finished
70
+
71
+ @defer_stop = nil
70
72
  end
71
73
 
72
74
  def reactor
@@ -212,6 +214,13 @@ module Async
212
214
  return stopped!
213
215
  end
214
216
 
217
+ # If we are deferring stop...
218
+ if @defer_stop == false
219
+ # Don't stop now... but update the state so we know we need to stop later.
220
+ @defer_stop = true
221
+ return false
222
+ end
223
+
215
224
  # If the fiber is alive, we need to stop it:
216
225
  if @fiber&.alive?
217
226
  if self.current?
@@ -239,6 +248,42 @@ module Async
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
+ # @public Since `stable-v1`.
259
+ def defer_stop
260
+ # Tri-state variable for controlling stop:
261
+ # - nil: defer_stop has not been called.
262
+ # - false: defer_stop has been called and we are not stopping.
263
+ # - true: defer_stop has been called and we will stop when exiting the block.
264
+ if @defer_stop.nil?
265
+ # If we are not deferring stop already, we can defer it now:
266
+ @defer_stop = false
267
+
268
+ begin
269
+ yield
270
+ rescue Stop
271
+ # If we are exiting due to a stop, we shouldn't try to invoke stop again:
272
+ @defer_stop = nil
273
+ raise
274
+ ensure
275
+ # If we were asked to stop, we should do so now:
276
+ if @defer_stop
277
+ @defer_stop = nil
278
+ self.stop
279
+ end
280
+ end
281
+ else
282
+ # If we are deferring stop already, entering it again is a no-op.
283
+ yield
284
+ end
285
+ end
286
+
242
287
  # Lookup the {Task} for the current fiber. Raise `RuntimeError` if none is available.
243
288
  # @returns [Task]
244
289
  # @raises[RuntimeError] If task was not {set!} for the current fiber.
data/lib/async/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2017-2024, by Samuel Williams.
5
5
 
6
6
  module Async
7
- VERSION = "2.9.0"
7
+ VERSION = "2.10.0"
8
8
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -61,7 +61,7 @@ cert_chain:
61
61
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
62
62
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
63
63
  -----END CERTIFICATE-----
64
- date: 2024-03-05 00:00:00.000000000 Z
64
+ date: 2024-03-27 00:00:00.000000000 Z
65
65
  dependencies:
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: console
metadata.gz.sig CHANGED
Binary file