| 1 | package App::MultiModule::Tasks::Runner; | ||
| 2 | $App::MultiModule::Tasks::Runner::VERSION = '1.161390'; | ||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings FATAL => 'all'; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Storable; | ||
| 8 | use POE qw( Wheel::Run ); | ||
| 9 | use JSON; | ||
| 10 | |||
| 11 | use parent 'App::MultiModule::Task'; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | App::MultiModule::Tasks::Runner - Run external programs under App::MultiModule | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | { | ||
| 20 | my $ps_cache; | ||
| 21 | my $cache_ts; | ||
| 22 | sub _is_running { | ||
| 23 | my $regex = shift or die 'is_running: regex required'; | ||
| 24 | $cache_ts = 0 unless defined $cache_ts; | ||
| 25 | my $time = time; | ||
| 26 | if($time - 5 > $cache_ts) { | ||
| 27 | $cache_ts = $time; | ||
| 28 | undef $ps_cache; | ||
| 29 | } | ||
| 30 | if(not $ps_cache) { | ||
| 31 | $ps_cache = `ps -eo cmd`; | ||
| 32 | } | ||
| 33 | return $ps_cache =~ /$regex/; | ||
| 34 | } | ||
| 35 | |||
| 36 | =head2 message | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub message { | ||
| 41 | my $self = shift; | ||
| 42 | my $message = shift; | ||
| 43 | my %args = @_; | ||
| 44 | $self->debug('message', message => $message) | ||
| 45 | if $self->{debug} > 5; | ||
| 46 | my $state = $self->{state}; | ||
| 47 | $state->{running_progs} = {} unless $state->{running_progs}; | ||
| 48 | my $prog = $message->{runner_program_prog} | ||
| 49 | or die 'run_program: runner_program_prog required'; | ||
| 50 | my $prog_args = Storable::dclone($message->{runner_program_args}); | ||
| 51 | $prog_args = [] unless $prog_args; | ||
| 52 | die 'run_program: runner_program_args must be an ARRAY or HASH reference' | ||
| 53 | if not ref $prog_args or | ||
| 54 | ( ref $prog_args ne 'ARRAY' and | ||
| 55 | ref $prog_args ne 'HASH'); | ||
| 56 | if(ref $prog_args eq 'HASH') { | ||
| 57 | my @args = (); | ||
| 58 | my @sorted_arg_numbers = sort { $a <=> $b } keys %$prog_args; | ||
| 59 | foreach my $arg_number (@sorted_arg_numbers) { | ||
| 60 | push @args, $prog_args->{$arg_number}; | ||
| 61 | } | ||
| 62 | $prog_args = \@args; | ||
| 63 | } | ||
| 64 | my $prog_run_key = "$prog," . join ',',@$prog_args; | ||
| 65 | my $prog_regex = $message->{runner_process_regex} | ||
| 66 | or die 'run_program: runner_process_regex required'; | ||
| 67 | $message->{runner_return_type} = 'gather' | ||
| 68 | unless $message->{runner_return_type}; | ||
| 69 | my $return_type = $message->{runner_return_type}; | ||
| 70 | die 'run_program: runner_return_type must be one of "json", "gather"' | ||
| 71 | if $return_type ne 'json' and $return_type ne 'gather'; | ||
| 72 | if($return_type eq 'gather') { | ||
| 73 | $message->{runner_stdout} = ''; | ||
| 74 | $message->{runner_stderr} = ''; | ||
| 75 | } | ||
| 76 | |||
| 77 | if($state->{running_progs}->{prog_run_key} or _is_running($prog_regex)) { | ||
| 78 | $message->{runner_message_type} = 'already running'; | ||
| 79 | $self->emit($message); | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | undef $ps_cache; | ||
| 83 | my $on_start = sub { | ||
| 84 | my $child = POE::Wheel::Run->new( | ||
| 85 | Program => [ $prog, @$prog_args], | ||
| 86 | StdoutEvent => 'got_child_stdout', | ||
| 87 | StderrEvent => 'got_child_stderr', | ||
| 88 | CloseEvent => 'got_child_close', | ||
| 89 | ); | ||
| 90 | |||
| 91 | $_[KERNEL]->sig_child($child->PID, 'got_child_signal'); | ||
| 92 | $_[HEAP]{children_by_wid}{$child->ID} = $child; | ||
| 93 | $_[HEAP]{children_by_pid}{$child->PID} = $child; | ||
| 94 | $message->{runner_start_time} = time; | ||
| 95 | $message->{runner_pid} = $child->PID; | ||
| 96 | $message->{runner_message_type} = 'start'; | ||
| 97 | $message->{runner_prog_run_key} = $prog_run_key; | ||
| 98 | my $send_message = Storable::dclone($message); | ||
| 99 | $self->emit($send_message); | ||
| 100 | delete $message->{runner_message_type}; | ||
| 101 | $state->{running_progs}->{$prog_run_key} = $message; | ||
| 102 | }; | ||
| 103 | # Wheel event, including the wheel's ID. | ||
| 104 | my $on_child_stdout = sub { | ||
| 105 | my ($stdout_line, $wheel_id) = @_[ARG0, ARG1]; | ||
| 106 | my $child = $_[HEAP]{children_by_wid}{$wheel_id}; | ||
| 107 | if($return_type eq 'gather') { #simply gather all of | ||
| 108 | $message->{runner_stdout} .= "$stdout_line\n"; | ||
| 109 | } elsif($return_type eq 'json') { | ||
| 110 | $message->{runner_stdout} .= "$stdout_line\n"; | ||
| 111 | my $emit = eval { | ||
| 112 | return decode_json $message->{runner_stdout}; | ||
| 113 | }; | ||
| 114 | if($emit) { | ||
| 115 | $message->{runner_stdout} = ''; | ||
| 116 | $self->emit($emit); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | |||
| 121 | # Wheel event, including the wheel's ID. | ||
| 122 | my $on_child_stderr = sub { | ||
| 123 | my ($stderr_line, $wheel_id) = @_[ARG0, ARG1]; | ||
| 124 | my $child = $_[HEAP]{children_by_wid}{$wheel_id}; | ||
| 125 | if($return_type eq 'gather') { #simply gather all of | ||
| 126 | $message->{runner_stderr} .= "$stderr_line\n"; | ||
| 127 | } | ||
| 128 | }; | ||
| 129 | |||
| 130 | # Wheel event, including the wheel's ID. | ||
| 131 | my $on_child_close = sub { | ||
| 132 | my $wheel_id = $_[ARG0]; | ||
| 133 | my $child = delete $_[HEAP]{children_by_wid}{$wheel_id}; | ||
| 134 | |||
| 135 | undef $ps_cache; | ||
| 136 | unless (defined $child) { | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | delete $_[HEAP]{children_by_pid}{$child->PID}; | ||
| 141 | }; | ||
| 142 | |||
| 143 | #This is where we're claiming the child is gone | ||
| 144 | my $on_child_signal = sub { | ||
| 145 | my $child = delete $_[HEAP]{children_by_pid}{$_[ARG1]}; | ||
| 146 | $message->{runner_message_type} = 'finish'; | ||
| 147 | $message->{runner_exit_code} = $_[ARG2] >> 8; | ||
| 148 | $message->{runner_run_time} = time - $message->{runner_start_time}; | ||
| 149 | |||
| 150 | if($return_type eq 'gather') { | ||
| 151 | #noop, because we've already gathered the STDOUT/ERR | ||
| 152 | } elsif($return_type eq 'json') { | ||
| 153 | #noop, because we've already sent any and all messages | ||
| 154 | } | ||
| 155 | if($message->{runner_stderr_to_stdout}) { | ||
| 156 | $message->{runner_stdout} .= $message->{runner_stderr}; | ||
| 157 | $message->{runner_stderr} = ''; | ||
| 158 | } | ||
| 159 | $self->emit($message); | ||
| 160 | # May have been reaped by on_child_close(). | ||
| 161 | return unless defined $child; | ||
| 162 | |||
| 163 | delete $_[HEAP]{children_by_wid}{$child->ID}; | ||
| 164 | }; | ||
| 165 | POE::Session->create( | ||
| 166 | inline_states => { | ||
| 167 | _start => $on_start, | ||
| 168 | got_child_stdout => $on_child_stdout, | ||
| 169 | got_child_stderr => $on_child_stderr, | ||
| 170 | got_child_close => $on_child_close, | ||
| 171 | got_child_signal => $on_child_signal, | ||
| 172 | } | ||
| 173 | ); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | =head2 set_config | ||
| 178 | |||
| 179 | =cut | ||
| 180 | sub set_config { | ||
| 181 | my $self = shift; | ||
| 182 | my $config = shift; | ||
| 183 | $self->{config} = $config; | ||
| 184 | my $state = $self->{state}; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | =head1 AUTHOR | ||
| 189 | |||
| 190 | Dana M. Diederich, C<< <dana@realms.org> >> | ||
| 191 | |||
| 192 | =head1 BUGS | ||
| 193 | |||
| 194 | Please report any bugs or feature requests through L<https://github.com/dana/perl-App-MultiModule-Tasks-Runner/issues>. I will be notified, and then you'll | ||
| 195 | automatically be notified of progress on your bug as I make changes. | ||
| 196 | |||
| 197 | =head1 SUPPORT | ||
| 198 | |||
| 199 | You can find documentation for this module with the perldoc command. | ||
| 200 | |||
| 201 | perldoc App::MultiModule::Tasks::Runner | ||
| 202 | |||
| 203 | |||
| 204 | You can also look for information at: | ||
| 205 | |||
| 206 | =over 4 | ||
| 207 | |||
| 208 | =item * Report bugs here: | ||
| 209 | |||
| 210 | L<https://github.com/dana/perl-App-MultiModule-Tasks-Runner/issues> | ||
| 211 | |||
| 212 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 213 | |||
| 214 | L<http://annocpan.org/dist/App-MultiModule-Tasks-Runner> | ||
| 215 | |||
| 216 | =item * CPAN Ratings | ||
| 217 | |||
| 218 | L<http://cpanratings.perl.org/d/App-MultiModule-Tasks-Runner> | ||
| 219 | |||
| 220 | =item * Search CPAN | ||
| 221 | |||
| 222 | L<https://metacpan.org/module/App::MultiModule::Tasks::Runner> | ||
| 223 | |||
| 224 | =back | ||
| 225 | |||
| 226 | =head1 ACKNOWLEDGEMENTS | ||
| 227 | |||
| 228 | =head1 LICENSE AND COPYRIGHT | ||
| 229 | |||
| 230 | Copyright 2016 Dana M. Diederich. | ||
| 231 | |||
| 232 | This program is free software; you can redistribute it and/or modify it | ||
| 233 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 234 | copy of the full license at: | ||
| 235 | |||
| 236 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 237 | |||
| 238 | Any use, modification, and distribution of the Standard or Modified | ||
| 239 | Versions is governed by this Artistic License. By using, modifying or | ||
| 240 | distributing the Package, you accept this license. Do not use, modify, | ||
| 241 | or distribute the Package, if you do not accept this license. | ||
| 242 | |||
| 243 | If your Modified Version has been derived from a Modified Version made | ||
| 244 | by someone other than you, you are nevertheless required to ensure that | ||
| 245 | your Modified Version complies with the requirements of this license. | ||
| 246 | |||
| 247 | This license does not grant you the right to use any trademark, service | ||
| 248 | mark, tradename, or logo of the Copyright Holder. | ||
| 249 | |||
| 250 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 251 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 252 | otherwise transfer the Package with respect to any patent claims | ||
| 253 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 254 | Package. If you institute patent litigation (including a cross-claim or | ||
| 255 | counterclaim) against any party alleging that the Package constitutes | ||
| 256 | direct or contributory patent infringement, then this Artistic License | ||
| 257 | to you shall terminate on the date that such litigation is filed. | ||
| 258 | |||
| 259 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 260 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 261 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 262 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 263 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 264 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 265 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 266 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 267 | |||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | 1; # End of App::MultiModule::Tasks::Runner |