line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cinnamon::Runner::Concurrent; |
2
|
2
|
|
|
2
|
|
1099
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
60
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
48
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
11
|
use Cinnamon::Logger; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
82
|
|
6
|
2
|
|
|
2
|
|
11
|
use Cinnamon::Config; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
35
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
420
|
use Coro; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Coro::Select; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub start { |
12
|
|
|
|
|
|
|
my ($class, $hosts, $task, @args) = @_; |
13
|
|
|
|
|
|
|
my $all_results = {}; |
14
|
|
|
|
|
|
|
$hosts = [ @$hosts ]; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $task_name = Cinnamon::Config::get('task'); |
17
|
|
|
|
|
|
|
my $concurrency_setting = Cinnamon::Config::get('max_concurrency') || {}; |
18
|
|
|
|
|
|
|
my $concurrency = $concurrency_setting->{$task_name} || scalar @$hosts; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
while (my @target_hosts = splice @$hosts, 0, $concurrency) { |
21
|
|
|
|
|
|
|
my @coros; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
for my $host (@target_hosts) { |
24
|
|
|
|
|
|
|
my $coro = async { |
25
|
|
|
|
|
|
|
my $result = $class->execute($host, $task, @args); |
26
|
|
|
|
|
|
|
$all_results->{$host} = $result; |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
push @coros, $coro; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$_->join for @coros; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
return $all_results; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub execute { |
39
|
|
|
|
|
|
|
my ($class, $host, $task, @args) = @_; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $result = { host => $host, error => 0 }; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
local $@; |
44
|
|
|
|
|
|
|
eval { $task->($host, @args) }; |
45
|
|
|
|
|
|
|
if ($@) { |
46
|
|
|
|
|
|
|
chomp $@; |
47
|
|
|
|
|
|
|
log error => sprintf '[%s] %s', $host, $@; |
48
|
|
|
|
|
|
|
$result->{error} = 1; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return $result; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |