captureStdoutStderr*: move to Hydra::Helper::Exec which helps avoid some environment variable fixation problems

This commit is contained in:
Graham Christensen
2022-02-09 13:40:51 -05:00
parent 1abe7f4d80
commit 845e6d4760
16 changed files with 54 additions and 50 deletions

View File

@ -0,0 +1,35 @@
use warnings;
use strict;
use IPC::Run;
package Hydra::Helper::Exec;
our @ISA = qw(Exporter);
our @EXPORT = qw(
captureStdoutStderr
captureStdoutStderrWithStdin
);
sub captureStdoutStderr {
my ($timeout, @cmd) = @_;
return captureStdoutStderrWithStdin($timeout, \@cmd, "");
}
sub captureStdoutStderrWithStdin {
my ($timeout, $cmd, $stdin) = @_;
my $stdout;
my $stderr;
eval {
local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required
alarm $timeout;
IPC::Run::run($cmd, \$stdin, \$stdout, \$stderr);
alarm 0;
1;
} or do {
die unless $@ eq "timeout\n"; # propagate unexpected errors
return (-1, $stdout, ($stderr // "") . "timeout\n");
};
return ($?, $stdout, $stderr);
}

View File

@ -18,8 +18,6 @@ use UUID4::Tiny qw(is_uuid4_string);
our @ISA = qw(Exporter);
our @EXPORT = qw(
cancelBuilds
captureStdoutStderr
captureStdoutStderrWithStdin
constructRunCommandLogPath
findLog
gcRootFor
@ -429,30 +427,7 @@ sub pathIsInsidePrefix {
}
sub captureStdoutStderr {
my ($timeout, @cmd) = @_;
return captureStdoutStderrWithStdin($timeout, \@cmd, "");
}
sub captureStdoutStderrWithStdin {
my ($timeout, $cmd, $stdin) = @_;
my $stdout;
my $stderr;
eval {
local $SIG{ALRM} = sub { die "timeout\n" }; # NB: \n required
alarm $timeout;
IPC::Run::run($cmd, \$stdin, \$stdout, \$stderr);
alarm 0;
1;
} or do {
die unless $@ eq "timeout\n"; # propagate unexpected errors
return (-1, $stdout, ($stderr // "") . "timeout\n");
};
return ($?, $stdout, $stderr);
}
sub run {

View File

@ -5,6 +5,7 @@ use warnings;
use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex);
use File::Path;
use Hydra::Helper::Exec;
use Hydra::Helper::Nix;
use Nix::Store;

View File

@ -5,6 +5,7 @@ use warnings;
use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex);
use File::Path;
use Hydra::Helper::Exec;
use Hydra::Helper::Nix;
use Nix::Store;

View File

@ -6,6 +6,7 @@ use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex);
use File::Path;
use Hydra::Helper::Nix;
use Hydra::Helper::Exec;
use Nix::Store;
use Fcntl qw(:flock);

View File

@ -4,6 +4,7 @@ use strict;
use warnings;
use parent 'Hydra::Plugin';
use Digest::SHA qw(sha256_hex);
use Hydra::Helper::Exec;
use Hydra::Helper::Nix;
use IPC::Run;
use Nix::Store;

View File

@ -11,6 +11,7 @@ use File::Slurper qw(read_text);
use Hydra::Helper::AddBuilds;
use Hydra::Helper::CatalystUtils;
use Hydra::Helper::Email;
use Hydra::Helper::Exec;
use Hydra::Helper::Nix;
use Hydra::Model::DB;
use Hydra::Plugin;