| 1 | package System::Wrapper::Parallel; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use constant MAX_RAND => System::Wrapper::MAX_RAND; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use File::Spec; | ||
| 9 | use Moose; | ||
| 10 | use System::Wrapper; | ||
| 11 | |||
| 12 | our $VERSION = '0.0.2'; | ||
| 13 | |||
| 14 | has 'commands' => ( is => 'rw', isa => 'ArrayRef[System::Wrapper]', required => 1 ); | ||
| 15 | has 'pipeline' => ( is => 'rw', isa => 'Bool' ); | ||
| 16 | |||
| 17 | sub run { | ||
| 18 | my ( $self ) = @_; | ||
| 19 | |||
| 20 | my ($previous, @commands) = @{$self->commands}; | ||
| 21 | |||
| 22 | System::Wrapper::_err( "parallel execution requires more than one command" ) | ||
| 23 | unless @commands; | ||
| 24 | |||
| 25 | System::Wrapper::_err( "pipeline execution requires POSIX::mkfifo() to be implemented" ) | ||
| 26 | if $self->pipeline and not _mkfifo_works(); | ||
| 27 | |||
| 28 | my @results = (); | ||
| 29 | COMMAND: | ||
| 30 | for my $command ( @commands, 'DUMMY' ) { | ||
| 31 | |||
| 32 | if ( $self->pipeline and ref $command ) { | ||
| 33 | $previous->_fifo( _connect( $previous, $command ) ); | ||
| 34 | } | ||
| 35 | |||
| 36 | if ( ref $command ) { | ||
| 37 | unless (my $pid = fork) { | ||
| 38 | push @results, $previous->run; | ||
| 39 | exit if ref $command; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | push @results, $previous->run; | ||
| 44 | } | ||
| 45 | $previous = $command; | ||
| 46 | } | ||
| 47 | return @results; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _connect { | ||
| 51 | my ( $upstream, $downstream ) = @_; | ||
| 52 | my $class = ref $downstream; | ||
| 53 | |||
| 54 | my $named_pipe = File::Spec->catfile( File::Spec->tmpdir(), | ||
| 55 | qq{$class:} . int rand MAX_RAND ); | ||
| 56 | |||
| 57 | POSIX::mkfifo( $named_pipe, 0777 ) | ||
| 58 | or _err( "couldn't create named pipe %s: %s", $named_pipe, $! ); | ||
| 59 | |||
| 60 | my %output_spec = $upstream->output; | ||
| 61 | |||
| 62 | _err( | ||
| 63 | "can't install fifo %s as output because there are multiple output specifications (%s):\n%s", | ||
| 64 | $named_pipe, | ||
| 65 | join( q{, }, map {qq{'$_' }} sort keys %output_spec ), | ||
| 66 | $upstream->description || "$upstream" | ||
| 67 | ) if keys %output_spec > 1; | ||
| 68 | |||
| 69 | $upstream->output( { scalar each %output_spec || q{>} => $named_pipe } ); | ||
| 70 | $downstream->input($named_pipe); | ||
| 71 | |||
| 72 | return $named_pipe; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _mkfifo_works { | ||
| 76 | eval { | ||
| 77 | require POSIX; | ||
| 78 | POSIX->import(); | ||
| 79 | my $test_named_pipe = File::Spec->catfile( File::Spec->tmpdir(), | ||
| 80 | 'test.' . int rand MAX_RAND ); | ||
| 81 | |||
| 82 | POSIX::mkfifo( $test_named_pipe, 0777 ) | ||
| 83 | or _err( "couldn't create test named pipe %s: %s", | ||
| 84 | $test_named_pipe, $! ); | ||
| 85 | |||
| 86 | unlink $test_named_pipe; | ||
| 87 | }; | ||
| 88 | _err( "requires POSIX::mkfifo:\n%s", $@ ) | ||
| 89 | if $@; | ||
| 90 | |||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; |