pwork 1.1.0 → 1.2.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 +4 -4
- data/lib/pwork/async.rb +18 -9
- data/lib/pwork/async/task.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5bddce5bdc79b8eb693f8a9ce38dfb2e0e81dbb4fe60b3f0b3a8d92cd72138b9
|
4
|
+
data.tar.gz: c3de3dcc22eb731e4686ae3ee402747653e8c4f6e6e1c4a758abae4e1f705464
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be9d97bb3d642aac3f4cc8991f319b68056b21b61ffd81f761f1808af1696b35c9e91b13d8fe17d2d5d04739e25370dfd235ba776f23e79b629ba5f3143960fc
|
7
|
+
data.tar.gz: b51e7f88bd67d1c3827e084c4377a58be95579c89c6320ade0b0f0f7f545aadad8e2256b31524861c6612aa9d0d93b871e845f3abafb1b8ddc6890b94fe4801d
|
data/lib/pwork/async.rb
CHANGED
@@ -4,14 +4,12 @@ require_relative 'async/manager'
|
|
4
4
|
|
5
5
|
module PWork
|
6
6
|
module Async
|
7
|
-
def async(options =
|
7
|
+
def async(options = {}, &block)
|
8
8
|
if block_given?
|
9
|
+
options[:caller] = self
|
9
10
|
PWork::Async.add_task(options, &block)
|
10
11
|
else
|
11
|
-
|
12
|
-
'Unknown async option.'
|
13
|
-
) unless options == :wait
|
14
|
-
PWork::Async.wait_for_tasks
|
12
|
+
PWork::Async.wait_for_tasks({ caller: self, command: options })
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
@@ -22,9 +20,9 @@ module PWork
|
|
22
20
|
def self.add_task(options, &block)
|
23
21
|
manager.start unless manager.running
|
24
22
|
|
25
|
-
options = {} if options == nil
|
26
23
|
task = PWork::Async::Task.new.tap do |e|
|
27
24
|
e.block = block
|
25
|
+
e.caller = options[:caller]
|
28
26
|
end
|
29
27
|
|
30
28
|
unless options[:wait] == false
|
@@ -40,11 +38,20 @@ module PWork
|
|
40
38
|
Thread.current[:pwork_async_tasks] ||= []
|
41
39
|
end
|
42
40
|
|
43
|
-
def self.wait_for_tasks
|
44
|
-
|
41
|
+
def self.wait_for_tasks(options)
|
42
|
+
case options[:command]
|
43
|
+
when :wait
|
44
|
+
task_list = tasks
|
45
|
+
when :wait_local
|
46
|
+
task_list = tasks.select { |t| t.caller == options[:caller] }
|
47
|
+
end
|
48
|
+
|
49
|
+
until task_list.detect { |t| t.state == :pending || t.state == :active }.nil?
|
45
50
|
sleep(async_wait_sleep_iteration)
|
46
51
|
end
|
52
|
+
|
47
53
|
handle_errors
|
54
|
+
|
48
55
|
ensure
|
49
56
|
Thread.current[:pwork_async_tasks] = []
|
50
57
|
end
|
@@ -54,7 +61,9 @@ module PWork
|
|
54
61
|
tasks.select { |t| t.state == :error }.each do |t|
|
55
62
|
error_messages << "Error: #{t.error.message}, #{t.error.backtrace}"
|
56
63
|
end
|
57
|
-
raise PWork::Async::Exceptions::TaskError.new(
|
64
|
+
raise PWork::Async::Exceptions::TaskError.new(
|
65
|
+
"1 or more async errors occurred. #{error_messages.join(' | ')}"
|
66
|
+
) if error_messages.length > 0
|
58
67
|
true
|
59
68
|
end
|
60
69
|
|
data/lib/pwork/async/task.rb
CHANGED