team_effort 0.0.9 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/team_effort/version.rb +1 -1
- data/lib/team_effort.rb +27 -8
- data/test/{test_team_effort.rb → team_effort_test.rb} +20 -4
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d7779d24f1c2aa78460778a38d505c106677eab6afe2c829ebd551b169ce551
|
4
|
+
data.tar.gz: 2f96281c9c5558d23d4bb49153d450bd8a4617b0ec551d93fcb6ebab5de13246
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7090174299461bd8e9d50190c3b7d10cede4fcb918985d741c72e629860ee2b98f866daf0c512f3b5fe5c452ca07972c70e09bd39d8f858d671e74ffa9bd5e66
|
7
|
+
data.tar.gz: 24a64b1dbaf6b2b6f4d6718bd49cf208e9cb92f6c3d86ef0b5d3c28afc71eba4a65b0e72eb5707eecf09bfda6318de90be6dee044df69fc86e936444ddc7f629
|
data/CHANGELOG.md
ADDED
data/lib/team_effort/version.rb
CHANGED
data/lib/team_effort.rb
CHANGED
@@ -3,25 +3,44 @@ require_relative "team_effort/version"
|
|
3
3
|
module TeamEffort
|
4
4
|
def self.work(enumerable, max_process_count = 4, progress_proc: nil)
|
5
5
|
pids = []
|
6
|
+
arg_sets = []
|
7
|
+
|
6
8
|
max_count = enumerable.count
|
7
9
|
completed_count = 0
|
8
10
|
|
9
|
-
enumerable.each do |
|
11
|
+
enumerable.each do |arg_set|
|
10
12
|
while pids.size == max_process_count
|
11
|
-
|
12
|
-
pids.delete finished_pid
|
13
|
-
progress_proc.call(completed_count += 1, max_count) if progress_proc
|
13
|
+
pids, arg_sets, completed_count = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
|
14
14
|
end
|
15
15
|
|
16
16
|
pids << fork do
|
17
|
-
|
17
|
+
begin
|
18
|
+
yield arg_set
|
19
|
+
exit! 0
|
20
|
+
rescue => e
|
21
|
+
$stderr.puts "TeamEffort child process error"
|
22
|
+
$stderr.puts e
|
23
|
+
$stderr.puts caller
|
24
|
+
exit! 1
|
25
|
+
end
|
18
26
|
end
|
27
|
+
arg_sets << arg_set
|
19
28
|
end
|
20
29
|
|
21
30
|
while !pids.empty?
|
22
|
-
|
23
|
-
|
24
|
-
|
31
|
+
pids, arg_sets, completed_count = wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.wait_for_completion(pids, arg_sets, completed_count, max_count, progress_proc)
|
36
|
+
pid, status = Process.wait2
|
37
|
+
pids_index = pids.index(pid)
|
38
|
+
if pids_index
|
39
|
+
pids.delete_at pids_index
|
40
|
+
arg_set = arg_sets.delete_at pids_index
|
41
|
+
raise "TeamEffort child process failed when processing > #{arg_set} <" if !status.success?
|
25
42
|
end
|
43
|
+
progress_proc.call(completed_count += 1, max_count) if progress_proc
|
44
|
+
[pids, arg_sets, completed_count]
|
26
45
|
end
|
27
46
|
end
|
@@ -26,16 +26,32 @@ describe TeamEffort do
|
|
26
26
|
lines = output.split(/\n/)
|
27
27
|
lines.size.must_equal 3
|
28
28
|
lines.each do |line|
|
29
|
-
line.must_match
|
29
|
+
line.must_match(/^\d+$/)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
it 'exits when a child process fails' do
|
34
|
+
data = [8, 4, 2, 0, 4, 8]
|
35
|
+
|
36
|
+
err = -> {
|
37
|
+
$stderr = File.open('/dev/null', 'w')
|
38
|
+
begin
|
39
|
+
TeamEffort.work(data, 1) do |item|
|
40
|
+
16 / item
|
41
|
+
end
|
42
|
+
ensure
|
43
|
+
$stderr.close
|
44
|
+
$stderr = STDERR
|
45
|
+
end
|
46
|
+
}.must_raise RuntimeError
|
47
|
+
err.message.must_match /TeamEffort child process failed when processing > 0 </
|
48
|
+
end
|
49
|
+
|
33
50
|
it 'invokes an optional proc when it completes an item' do
|
34
51
|
data = %w|one two three|
|
35
52
|
proc_data = []
|
36
|
-
|
37
|
-
|
38
|
-
TeamEffort.work(data, 1, progress_proc: progress_proc) {}
|
53
|
+
proc = ->(item_index, max_items) { proc_data << [item_index, max_items] }
|
54
|
+
TeamEffort.work(data, 1, progress_proc: proc) {}
|
39
55
|
|
40
56
|
proc_data.must_equal [
|
41
57
|
[1, 3],
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: team_effort
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Felkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -49,13 +49,14 @@ files:
|
|
49
49
|
- ".gitignore"
|
50
50
|
- ".ruby-gemset"
|
51
51
|
- ".ruby-version"
|
52
|
+
- CHANGELOG.md
|
52
53
|
- LICENSE
|
53
54
|
- README.md
|
54
55
|
- Rakefile
|
55
56
|
- lib/team_effort.rb
|
56
57
|
- lib/team_effort/version.rb
|
57
58
|
- team_effort.gemspec
|
58
|
-
- test/
|
59
|
+
- test/team_effort_test.rb
|
59
60
|
homepage: https://github.com/kellyfelkins/team_effort
|
60
61
|
licenses:
|
61
62
|
- MIT
|
@@ -81,4 +82,4 @@ signing_key:
|
|
81
82
|
specification_version: 4
|
82
83
|
summary: Use child processes to process a collection in parallel
|
83
84
|
test_files:
|
84
|
-
- test/
|
85
|
+
- test/team_effort_test.rb
|