| 1 | package Event::ExecFlow::Job::Command; | ||
| 2 | |||
| 3 | use base qw( Event::ExecFlow::Job ); | ||
| 4 | |||
| 5 | use Locale::TextDomain $Event::ExecFlow::locale_textdomain; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use AnyEvent; | ||
| 9 | |||
| 10 | # prevent warnings from AnyEvent | ||
| 11 | { package AnyEvent::Impl::Event::CondVar; | ||
| 12 | package AnyEvent::Impl::Event::Glib; } | ||
| 13 | |||
| 14 | sub get_type { "command" } | ||
| 15 | sub get_exec_type { "async" } | ||
| 16 | |||
| 17 | #------------------------------------------------------------------------ | ||
| 18 | |||
| 19 | sub get_command { shift->{command} } | ||
| 20 | sub get_fetch_output { shift->{fetch_output} } | ||
| 21 | sub get_node { shift->{node} } | ||
| 22 | sub get_output { shift->{output} } | ||
| 23 | sub get_progress_parser { shift->{progress_parser} } | ||
| 24 | sub get_got_exec_ok { shift->{got_exec_ok} } | ||
| 25 | sub get_configure_callback { shift->{configure_callback} } | ||
| 26 | |||
| 27 | sub set_command { shift->{command} = $_[1] } | ||
| 28 | sub set_fetch_output { shift->{fetch_output} = $_[1] } | ||
| 29 | sub set_node { shift->{node} = $_[1] } | ||
| 30 | sub set_output { shift->{output} = $_[1] } | ||
| 31 | sub set_progress_parser { shift->{progress_parser} = $_[1] } | ||
| 32 | sub set_got_exec_ok { shift->{got_exec_ok} = $_[1] } | ||
| 33 | sub set_configure_callback { shift->{configure_callback} = $_[1] } | ||
| 34 | |||
| 35 | #------------------------------------------------------------------------ | ||
| 36 | |||
| 37 | sub get_pids { shift->{pids} } | ||
| 38 | sub get_fh { shift->{fh} } | ||
| 39 | sub get_watcher { shift->{watcher} } | ||
| 40 | sub get_executed_command { shift->{executed_command} } | ||
| 41 | |||
| 42 | sub set_pids { shift->{pids} = $_[1] } | ||
| 43 | sub set_fh { shift->{fh} = $_[1] } | ||
| 44 | sub set_watcher { shift->{watcher} = $_[1] } | ||
| 45 | sub set_executed_command { shift->{executed_command} = $_[1] } | ||
| 46 | |||
| 47 | #------------------------------------------------------------------------ | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my $class = shift; | ||
| 51 | my %par = @_; | ||
| 52 | my ($command, $fetch_output, $node, $progress_parser) = | ||
| 53 | @par{'command','fetch_output','node','progress_parser'}; | ||
| 54 | my ($configure_callback) = | ||
| 55 | $par{'configure_callback'}; | ||
| 56 | |||
| 57 | my $self = $class->SUPER::new(@_); | ||
| 58 | |||
| 59 | $self->set_command($command); | ||
| 60 | $self->set_fetch_output($fetch_output); | ||
| 61 | $self->set_node($node); | ||
| 62 | $self->set_progress_parser($progress_parser); | ||
| 63 | $self->set_configure_callback($configure_callback); | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub init { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | $self->SUPER::init(); | ||
| 72 | |||
| 73 | $self->set_pids([]); | ||
| 74 | $self->set_fh(); | ||
| 75 | $self->set_watcher(); | ||
| 76 | $self->set_output(""); | ||
| 77 | |||
| 78 | 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub execute { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | $self->open_pipe; | ||
| 85 | |||
| 86 | 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub open_pipe { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | my $command = $self->get_command; | ||
| 93 | |||
| 94 | if ( ref $command eq 'CODE' ) { | ||
| 95 | $Event::ExecFlow::JOB = $self; | ||
| 96 | $command = $command->($self); | ||
| 97 | $Event::ExecFlow::JOB = undef; | ||
| 98 | } | ||
| 99 | |||
| 100 | if ( $self->get_configure_callback ) { | ||
| 101 | my $cb = $self->get_configure_callback; | ||
| 102 | $command = &$cb($command); | ||
| 103 | } | ||
| 104 | |||
| 105 | if ( $self->get_node ) { | ||
| 106 | $command = $self->get_node->prepare_command($command, $self); | ||
| 107 | } | ||
| 108 | |||
| 109 | $command =~ s/\s+$//; | ||
| 110 | |||
| 111 | my $execflow = $command =~ /execflow/ ? "" : "execflow "; | ||
| 112 | $command = $execflow.$command; | ||
| 113 | $command .= " && echo EXECFLOW_OK" if $command !~ /EXECFLOW_OK/; | ||
| 114 | |||
| 115 | $self->log (__x("Executing command: {command}", command => $command)); | ||
| 116 | $Event::ExecFlow::DEBUG && print "Command(".$self->get_info."): command=$command\n"; | ||
| 117 | |||
| 118 | $self->set_executed_command($command); | ||
| 119 | |||
| 120 | local $ENV{LC_ALL} = "C"; | ||
| 121 | local $ENV{LANG} = "C"; | ||
| 122 | |||
| 123 | my $pid = open (my $fh, "( $command ) 2>&1 |") | ||
| 124 | or die "can't fork '$command'"; | ||
| 125 | |||
| 126 | my $watcher = AnyEvent->io ( fh => $fh, poll => 'r', cb => sub { | ||
| 127 | $self->command_progress; | ||
| 128 | }); | ||
| 129 | |||
| 130 | push @{$self->get_pids}, $pid; | ||
| 131 | $self->set_fh($fh); | ||
| 132 | $self->set_watcher($watcher); | ||
| 133 | |||
| 134 | return $fh; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub close_pipe { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | $self->set_watcher(undef); | ||
| 141 | |||
| 142 | close($self->get_fh); | ||
| 143 | $self->set_fh(undef); | ||
| 144 | $self->set_pids([]); | ||
| 145 | |||
| 146 | if ( !$self->get_error_message && !$self->get_got_exec_ok ) { | ||
| 147 | $self->set_error_message( | ||
| 148 | "Command exits with failure code:\n". | ||
| 149 | "Command: ".$self->get_executed_command."\n\n". | ||
| 150 | "Output: ".$self->get_output | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub command_progress { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | my $fh = $self->get_fh; | ||
| 161 | |||
| 162 | #-- read and check for eof | ||
| 163 | my $buffer; | ||
| 164 | if ( !sysread($fh, $buffer, 4096) ) { | ||
| 165 | $self->close_pipe; | ||
| 166 | $self->execution_finished; | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | |||
| 170 | #-- get job's PID | ||
| 171 | my ($pid) = ( $buffer =~ /EXEC_FLOW_JOB_PID=(\d+)/ ); | ||
| 172 | if ( defined $pid ) { | ||
| 173 | push @{$self->get_pids}, $pid; | ||
| 174 | $buffer =~ s/EXEC_FLOW_JOB_PID=(\d+)\n//; | ||
| 175 | } | ||
| 176 | |||
| 177 | #-- succesfully executed? | ||
| 178 | if ( $buffer =~ s/EXECFLOW_OK\n// ) { | ||
| 179 | $self->set_got_exec_ok(1); | ||
| 180 | } | ||
| 181 | |||
| 182 | #-- store output | ||
| 183 | if ( $self->get_fetch_output ) { | ||
| 184 | $self->{output} .= $buffer; | ||
| 185 | } else { | ||
| 186 | $self->{output} = substr($self->{output}.$buffer,-16384); | ||
| 187 | } | ||
| 188 | |||
| 189 | #-- parse output & report progress | ||
| 190 | my $progress_parser = $self->get_progress_parser; | ||
| 191 | if ( ref $progress_parser eq 'CODE' ) { | ||
| 192 | $progress_parser->($self, $buffer); | ||
| 193 | } | ||
| 194 | elsif ( ref $progress_parser eq 'Regexp' ) { | ||
| 195 | if ( $buffer =~ $progress_parser ) { | ||
| 196 | $self->set_progress_cnt($1); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | $self->get_frontend->report_job_progress($self) | ||
| 201 | if $self->progress_has_changed; | ||
| 202 | |||
| 203 | 1; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub cancel { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | $self->set_cancelled(1); | ||
| 210 | |||
| 211 | my $pids = $self->get_pids; | ||
| 212 | return unless @{$pids}; | ||
| 213 | |||
| 214 | kill 9, @{$pids}; | ||
| 215 | |||
| 216 | $self->log(__x("Sending signal 9 to PID(s)")." ".join(", ", @{$pids})); | ||
| 217 | |||
| 218 | 1; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub pause_job { | ||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | my $signal; | ||
| 225 | if ( $self->get_paused ) { | ||
| 226 | $signal = "STOP"; | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | $signal = "CONT"; | ||
| 230 | } | ||
| 231 | |||
| 232 | my $pids = $self->get_pids; | ||
| 233 | kill $signal, @{$pids} if @{$pids}; | ||
| 234 | |||
| 235 | 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub backup_state { | ||
| 239 | my $self = shift; | ||
| 240 | |||
| 241 | my $data_href = $self->SUPER::backup_state(); | ||
| 242 | |||
| 243 | delete $data_href->{configure_callback}; | ||
| 244 | delete $data_href->{progress_parser}; | ||
| 245 | delete $data_href->{node}; | ||
| 246 | delete $data_href->{watcher}; | ||
| 247 | delete $data_href->{fh}; | ||
| 248 | delete $data_href->{command} | ||
| 249 | if ref $data_href->{command} eq 'CODE'; | ||
| 250 | |||
| 251 | return $data_href; | ||
| 252 | } | ||
| 253 | |||
| 254 | 1; | ||
| 255 | |||
| 256 | __END__ |