async 1.19.0 → 1.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/async/task.rb +21 -6
- data/lib/async/version.rb +1 -1
- data/spec/async/task_spec.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db5bf4667851a5b5c7f18895a970d61429aaa4a3d09b67f1b27114f60c1b3be0
|
4
|
+
data.tar.gz: 5e01edfda229d537e214c5addc67460d533eb2096580f2055642f160c2f4c116
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3353bb22a9f9669db6e417d575c953bb7c4384109459b437de5539c6ff0869957f02720bfbb878a6682678015a7d54ea7e67e2caf4c805d59c5fb57e470185be
|
7
|
+
data.tar.gz: bb6f76024fc98843c9ed1d08e834b9bfa0230deca26f4aa07b87eb8faf761922bab6a89f080b5dbae77b389b45ec8929bbcc0695cd4515058752b4250bd9b76c
|
data/lib/async/task.rb
CHANGED
@@ -134,13 +134,20 @@ module Async
|
|
134
134
|
# Stop the task and all of its children.
|
135
135
|
# @return [void]
|
136
136
|
def stop
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
137
|
+
if self.stopping?
|
138
|
+
# If we are already stopping this task... don't try to stop it again.
|
139
|
+
return true
|
140
|
+
elsif self.running?
|
141
|
+
@status = :stopping
|
142
|
+
|
143
|
+
if self.current?
|
144
|
+
raise Stop, "Stopping current fiber!"
|
145
|
+
elsif @fiber.alive?
|
146
|
+
@fiber.resume(Stop.new)
|
147
|
+
end
|
143
148
|
end
|
149
|
+
ensure
|
150
|
+
@children&.each(&:stop)
|
144
151
|
end
|
145
152
|
|
146
153
|
# Lookup the {Task} for the current fiber. Raise `RuntimeError` if none is available.
|
@@ -176,10 +183,18 @@ module Async
|
|
176
183
|
@status == :failed
|
177
184
|
end
|
178
185
|
|
186
|
+
def stopping?
|
187
|
+
@status == :stopping
|
188
|
+
end
|
189
|
+
|
179
190
|
def stopped?
|
180
191
|
@status == :stopped
|
181
192
|
end
|
182
193
|
|
194
|
+
def complete?
|
195
|
+
@status == :complete
|
196
|
+
end
|
197
|
+
|
183
198
|
private
|
184
199
|
|
185
200
|
# This is a very tricky aspect of tasks to get right. I've modelled it after `Thread` but it's slightly different in that the exception can propagate back up through the reactor. If the user writes code which raises an exception, that exception should always be visible, i.e. cause a failure. If it's not visible, such code fails silently and can be very difficult to debug.
|
data/lib/async/version.rb
CHANGED
data/spec/async/task_spec.rb
CHANGED
@@ -148,6 +148,26 @@ RSpec.describe Async::Task do
|
|
148
148
|
expect(task).to be_stopped
|
149
149
|
end
|
150
150
|
|
151
|
+
it "can stop nested tasks with exception handling" do
|
152
|
+
task = reactor.async do |task|
|
153
|
+
child = task.async do |subtask|
|
154
|
+
subtask.sleep(1)
|
155
|
+
end
|
156
|
+
|
157
|
+
begin
|
158
|
+
child.wait
|
159
|
+
ensure
|
160
|
+
child.stop
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
subtask = task.children.first
|
165
|
+
task.stop
|
166
|
+
|
167
|
+
expect(task.status).to be :stopped
|
168
|
+
expect(subtask.status).to be :stopped
|
169
|
+
end
|
170
|
+
|
151
171
|
it "can stop current task" do
|
152
172
|
state = nil
|
153
173
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.20.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|