| 1 | # $Id: Pipe.pm 2187 2006-08-16 19:34:38Z joern $ | ||
| 2 | |||
| 3 | #----------------------------------------------------------------------- | ||
| 4 | # Copyright (C) 2001-2006 Jörn Reder <joern AT zyn.de>. | ||
| 5 | # All Rights Reserved. See file COPYRIGHT for details. | ||
| 6 | # | ||
| 7 | # This program is part of Video::DVDRip, which is free software; you can | ||
| 8 | # redistribute it and/or modify it under the same terms as Perl itself. | ||
| 9 | #----------------------------------------------------------------------- | ||
| 10 | |||
| 11 | package Video::DVDRip::Cluster::Pipe; | ||
| 12 | use Locale::TextDomain qw (video.dvdrip); | ||
| 13 | |||
| 14 | use base Video::DVDRip::Base; | ||
| 15 | |||
| 16 | use Event; | ||
| 17 | use constant NICE => -1; | ||
| 18 | |||
| 19 | use FileHandle; | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | use strict; | ||
| 23 | |||
| 24 | my $LIFO_SIZE = 40; | ||
| 25 | |||
| 26 | sub command { shift->{command} } | ||
| 27 | sub timeout { shift->{timeout} } | ||
| 28 | sub cb_finished { shift->{cb_finished} } | ||
| 29 | sub cb_line_read { shift->{cb_line_read} } | ||
| 30 | |||
| 31 | sub no_log { shift->{no_log} } | ||
| 32 | sub set_no_log { shift->{no_log} = $_[1] } | ||
| 33 | |||
| 34 | sub lifo { shift->{lifo} } | ||
| 35 | sub lifo_idx { shift->{lifo_idx} } | ||
| 36 | |||
| 37 | sub fh { shift->{fh} } | ||
| 38 | sub pid { shift->{pid} } | ||
| 39 | sub line_buffer { shift->{line_buffer} } | ||
| 40 | sub event_waiter { shift->{event_waiter} } | ||
| 41 | sub closed { shift->{closed} } | ||
| 42 | |||
| 43 | sub set_fh { shift->{fh} = $_[1] } | ||
| 44 | sub set_pid { shift->{pid} = $_[1] } | ||
| 45 | sub set_event_waiter { shift->{event_waiter} = $_[1] } | ||
| 46 | sub set_line_buffer { shift->{line_buffer} = $_[1] } | ||
| 47 | sub set_closed { shift->{closed} = $_[1] } | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | my %par = @_; | ||
| 52 | my ( $command, $cb_line_read, $cb_finished, $timeout, $no_log ) | ||
| 53 | = @par{ 'command', 'cb_line_read', 'cb_finished', 'timeout', | ||
| 54 | 'no_log' }; | ||
| 55 | |||
| 56 | $timeout ||= 30; | ||
| 57 | |||
| 58 | my $self = { | ||
| 59 | timeout => $timeout, | ||
| 60 | command => $command, | ||
| 61 | cb_line_read => $cb_line_read, | ||
| 62 | cb_finished => $cb_finished, | ||
| 63 | no_log => $no_log, | ||
| 64 | event_waiter => undef, | ||
| 65 | output_lifo => [ (undef) x $LIFO_SIZE ], | ||
| 66 | lifo_idx => -1, | ||
| 67 | }; | ||
| 68 | |||
| 69 | return bless $self, $class; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub open { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $timeout = $self->timeout; | ||
| 76 | my $command = $self->command; | ||
| 77 | |||
| 78 | my $fh = FileHandle->new; | ||
| 79 | my $pid; | ||
| 80 | |||
| 81 | # we use fork & exec, because we want to have | ||
| 82 | # STDERR on STDOUT in the child. | ||
| 83 | $pid = open( $fh, "-|" ); | ||
| 84 | croak "can't fork child process" if not defined $pid; | ||
| 85 | |||
| 86 | if ( not $pid ) { | ||
| 87 | |||
| 88 | # we are the child. Copy STDERR to STDOUT | ||
| 89 | close STDERR; | ||
| 90 | open( STDERR, ">&STDOUT" ) | ||
| 91 | or croak "can't dup STDOUT to STDERR"; | ||
| 92 | my $command = $self->command; | ||
| 93 | $command = "execflow $command" if $command !~ /execflow/; | ||
| 94 | exec($command) | ||
| 95 | or croak "can't exec program: $!"; | ||
| 96 | } | ||
| 97 | |||
| 98 | # we are the parent and go further, holding the | ||
| 99 | # pid of our child in $pid | ||
| 100 | |||
| 101 | $self->set_fh($fh); | ||
| 102 | $self->set_pid($pid); | ||
| 103 | |||
| 104 | my %timeout_options; | ||
| 105 | %timeout_options = ( | ||
| 106 | timeout => $timeout, | ||
| 107 | timeout_cb => sub { $self->timeout_expired }, | ||
| 108 | ) | ||
| 109 | if $timeout; | ||
| 110 | |||
| 111 | $self->set_event_waiter( | ||
| 112 | Event->io( | ||
| 113 | fd => $fh, | ||
| 114 | poll => 'r', | ||
| 115 | desc => "command execution", | ||
| 116 | nice => NICE, | ||
| 117 | cb => sub { $self->input( $_[1] ) }, | ||
| 118 | %timeout_options, | ||
| 119 | ) | ||
| 120 | ); | ||
| 121 | |||
| 122 | $self->log( | ||
| 123 | 3, | ||
| 124 | __x("execute command: {command} (timeout={timeout})", | ||
| 125 | command => $command, | ||
| 126 | timeout => $timeout | ||
| 127 | ) | ||
| 128 | ) | ||
| 129 | unless $self->no_log; | ||
| 130 | |||
| 131 | return $self; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub add_lifo_line { | ||
| 135 | my $self = shift; | ||
| 136 | |||
| 137 | $self->{lifo} | ||
| 138 | ->[ $self->{lifo_idx} = ( $self->{lifo_idx} + 1 ) % $LIFO_SIZE ] | ||
| 139 | = $_[0]; | ||
| 140 | |||
| 141 | 1; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub output_tail { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | my $tail = ''; | ||
| 148 | my $lifo_idx = $self->{lifo_idx}; | ||
| 149 | my $i = $lifo_idx; | ||
| 150 | |||
| 151 | while () { | ||
| 152 | last if not defined $self->{lifo}->[$i]; | ||
| 153 | $tail .= $self->{lifo}->[ $i++ ]; | ||
| 154 | $i = $i % $LIFO_SIZE; | ||
| 155 | last if $i == $lifo_idx; | ||
| 156 | } | ||
| 157 | |||
| 158 | return $tail; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub timeout_expired { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | $self->log( __ "Command cancelled due to timeout" ) | ||
| 165 | unless $self->no_log; | ||
| 166 | |||
| 167 | kill 15, $self->pid; | ||
| 168 | $self->cancel; | ||
| 169 | |||
| 170 | 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub input { | ||
| 174 | my $self = shift; | ||
| 175 | my ($abort) = @_; | ||
| 176 | |||
| 177 | my $fh = $self->fh; | ||
| 178 | my $line_buffer; | ||
| 179 | |||
| 180 | # eof or abort? | ||
| 181 | if ( $abort or !sysread( $fh, $line_buffer, 4096 ) ) { | ||
| 182 | $self->close; | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | # read next line | ||
| 187 | my ( $rc, $got_empty_line ); | ||
| 188 | |||
| 189 | # get job's PID | ||
| 190 | my ($pid) = ( $line_buffer =~ /DVDRIP_JOB_PID=(\d+)/ ); | ||
| 191 | if ( defined $pid ) { | ||
| 192 | $self->set_pid($pid); | ||
| 193 | $self->log( __x( "Job has PID {pid}", pid => $pid ) ) | ||
| 194 | unless $self->no_log; | ||
| 195 | $line_buffer =~ s/DVDRIP_JOB_PID=(\d+)//; | ||
| 196 | $rc =~ s/DVDRIP_JOB_PID=(\d+)//; | ||
| 197 | } | ||
| 198 | |||
| 199 | # append line to lifo | ||
| 200 | $self->add_lifo_line($line_buffer); | ||
| 201 | |||
| 202 | # call the line_read callback, if we have one | ||
| 203 | my $cb_line_read = $self->cb_line_read; | ||
| 204 | &$cb_line_read($line_buffer) if $cb_line_read; | ||
| 205 | |||
| 206 | 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub close { | ||
| 210 | my $self = shift; | ||
| 211 | |||
| 212 | return if $self->closed; | ||
| 213 | |||
| 214 | my $fh = $self->fh; | ||
| 215 | |||
| 216 | $self->event_waiter->cancel if $self->event_waiter; | ||
| 217 | $self->set_event_waiter(undef); | ||
| 218 | |||
| 219 | close $fh; | ||
| 220 | waitpid $self->pid, 0; | ||
| 221 | |||
| 222 | $self->set_closed(1); | ||
| 223 | |||
| 224 | my $cb_finished = $self->cb_finished; | ||
| 225 | &$cb_finished() if $cb_finished; | ||
| 226 | |||
| 227 | $self->log( 5, "command finished: " . $self->command ) | ||
| 228 | unless $self->no_log; | ||
| 229 | |||
| 230 | 1; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub cancel { | ||
| 234 | my $self = shift; | ||
| 235 | |||
| 236 | my $pid = $self->pid; | ||
| 237 | |||
| 238 | if ($pid) { | ||
| 239 | $self->log( | ||
| 240 | __x("Aborting command. Sending signal 1 to PID {pid}...", | ||
| 241 | pid => $pid | ||
| 242 | ) | ||
| 243 | ) | ||
| 244 | unless $self->no_log; | ||
| 245 | kill 1, $pid; | ||
| 246 | } | ||
| 247 | |||
| 248 | $self->close; | ||
| 249 | |||
| 250 | 1; | ||
| 251 | } | ||
| 252 | |||
| 253 | 1; |