ProcessManager: execute_process() can now take a $timeout value

This commit is contained in:
Pragmatic Software 2020-02-14 21:25:09 -08:00
parent 19aff1d59d
commit 681438c27d
1 changed files with 14 additions and 7 deletions

View File

@ -66,11 +66,9 @@ sub remove_process {
delete $self->{processes}->{$pid}; delete $self->{processes}->{$pid};
} }
sub execute_subroutine {
}
sub execute_process { sub execute_process {
my ($self, $stuff, $subref) = @_; my ($self, $stuff, $subref, $timeout) = @_;
$timeout //= 30;
if (not exists $stuff->{commands}) { if (not exists $stuff->{commands}) {
$stuff->{commands} = [ $stuff->{command} ]; $stuff->{commands} = [ $stuff->{command} ];
@ -88,7 +86,8 @@ sub execute_process {
return; return;
} }
if ($pid == 0) { # start child block if ($pid == 0) {
# child
close $reader; close $reader;
# don't quit the IRC client when the child dies # don't quit the IRC client when the child dies
@ -97,11 +96,19 @@ sub execute_process {
use warnings; use warnings;
# execute the provided subroutine, results are stored in $stuff # execute the provided subroutine, results are stored in $stuff
eval { $subref->($stuff) }; eval {
local $SIG{ALRM} = sub { die "PBot::Process timed-out" };
alarm $timeout;
$subref->($stuff);
die if $@;
};
alarm 0;
# check for errors # check for errors
if ($@) { if ($@) {
$self->{pbot}->{logger}->log("Error executing process: $@\n"); $stuff->{result} = $@;
$stuff->{result} =~ s/ at PBot.*$//ms;
$self->{pbot}->{logger}->log("Error executing process: $stuff->{result}\n");
} }
# print $stuff to pipe # print $stuff to pipe