| 1 | package MYDan::Util::MIO::CMD; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Util::MIO::CMD - Run multiple commands in parallel. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Util::MIO::CMD; | ||
| 10 | |||
| 11 | my @node = qw( host1 host2 ... ); | ||
| 12 | my @cmd = qw( ssh {} wc ); | ||
| 13 | |||
| 14 | my $cmd = MYDan::Util::MIO::CMD->new( map { $_ => \@cmd } @node ); | ||
| 15 | my $result = $cmd->run( max => 32, log => \*STDERR, timeout => 300 ); | ||
| 16 | |||
| 17 | my $stdout = $result->{stdout}; | ||
| 18 | my $stderr = $result->{stderr}; | ||
| 19 | my $error = $result->{error}; | ||
| 20 | |||
| 21 | =cut | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | |||
| 25 | use Carp; | ||
| 26 | use IPC::Open3; | ||
| 27 | use Time::HiRes qw( time ); | ||
| 28 | use POSIX qw( :sys_wait_h ); | ||
| 29 | use IO::Poll qw( POLLIN POLLHUP POLLOUT ); | ||
| 30 | use Tie::File; | ||
| 31 | use FindBin qw( $Script ); | ||
| 32 | |||
| 33 | use base qw( MYDan::Util::MIO ); | ||
| 34 | |||
| 35 | our %RUN = ( %MYDan::Util::MIO::RUN, interchange => '{}' ); | ||
| 36 | our %MAX = %MYDan::Util::MIO::MAX; | ||
| 37 | |||
| 38 | sub new | ||
| 39 | { | ||
| 40 | my $self = shift; | ||
| 41 | $self->cmd( @_ ); | ||
| 42 | } | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =head3 run( %param ) | ||
| 47 | |||
| 48 | Run commands in parallel. | ||
| 49 | The following parameters may be defined in I<%param>: | ||
| 50 | |||
| 51 | max : ( default 128 ) number of commands in parallel. | ||
| 52 | log : ( default STDERR ) a handle to report progress. | ||
| 53 | timeout : ( default 300 ) number of seconds allotted for each command. | ||
| 54 | input : ( default from STDIN ) input buffer. | ||
| 55 | |||
| 56 | Returns HASH of HASH of nodes. First level is indexed by type | ||
| 57 | ( I<stdout>, I<stderr>, or I<error> ). Second level is indexed by message. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | sub run | ||
| 61 | { | ||
| 62 | local $| = 1; | ||
| 63 | |||
| 64 | my $self = shift; | ||
| 65 | my @node = keys %$self; | ||
| 66 | my ( $run, $ext, %run, %result, %busy ) = ( 1, "$Script.$$", %RUN, @_ ); | ||
| 67 | my ( $max, $timeout, $interchange ) = @run{ qw( max timeout interchange ) }; | ||
| 68 | my $input = defined $run{input} ? $run{input} : -t STDIN ? '' : <STDIN>; | ||
| 69 | |||
| 70 | $SIG{INT} = $SIG{TERM} = sub | ||
| 71 | { | ||
| 72 | print STDERR "killed\n"; | ||
| 73 | $run = 0; | ||
| 74 | }; | ||
| 75 | |||
| 76 | for ( my $time = time; $run && ( @node || %busy ); ) | ||
| 77 | { | ||
| 78 | $run = 0 if time - $time > $timeout; | ||
| 79 | |||
| 80 | while ( @node && keys %busy < $max ) | ||
| 81 | { | ||
| 82 | my $node = shift @node; | ||
| 83 | my $log = "/tmp/$node.$ext"; | ||
| 84 | my $cmd = $self->{$node}; | ||
| 85 | my @cmd = map { my $t = $_; $t =~ s/$interchange/$node/g; $t } @$cmd; | ||
| 86 | |||
| 87 | if ( $run{noop} ) | ||
| 88 | { | ||
| 89 | print join ' ', @cmd, "\n"; | ||
| 90 | next; | ||
| 91 | } | ||
| 92 | |||
| 93 | print "$node started.\n" if $run{verbose}; | ||
| 94 | |||
| 95 | if ( my $pid = fork() ) { $busy{$pid} = [ $log, $node ]; next } | ||
| 96 | |||
| 97 | open STDOUT, ">>$log"; | ||
| 98 | open STDERR, ">>$log"; | ||
| 99 | |||
| 100 | exec sprintf join ' ', @cmd; | ||
| 101 | exit 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | for ( keys %busy ) | ||
| 105 | { | ||
| 106 | my $pid = waitpid( -1, WNOHANG ); | ||
| 107 | next if $pid <= 0; | ||
| 108 | |||
| 109 | my $stat = $? >> 8; | ||
| 110 | |||
| 111 | my ( $log, $node ) = @{ delete $busy{$pid} }; | ||
| 112 | |||
| 113 | print "$node done.\n" if $run{verbose}; | ||
| 114 | |||
| 115 | tie my @log, 'Tie::File', $log,recsep => "\n"; | ||
| 116 | |||
| 117 | my $tmp = join "\n", @log; | ||
| 118 | $tmp =~ s/$node/$interchange/g if $run{xx}; | ||
| 119 | push @{ $result{output}{ join "\n", $tmp, "--- $stat", '' } }, $node; | ||
| 120 | unlink $log; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | kill 9, keys %busy; | ||
| 125 | push @{ $result{output}{killed} }, map{ $busy{$_}[1]}keys %busy; | ||
| 126 | push @{ $result{output}{norun} }, @node; | ||
| 127 | unlink glob "/tmp/*.$ext"; | ||
| 128 | unlink $input if $input && -f $input; | ||
| 129 | |||
| 130 | return wantarray ? %result : \%result; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; |