Created
November 22, 2021 01:26
-
-
Save kazuho/77c7f3ccb01d9ac4aa7fb03c6b50f57f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use File::Temp qw(tempdir); | |
use Getopt::Long; | |
use POSIX qw(:sys_wait_h); | |
my $jobs = 1; | |
GetOptions( | |
"jobs=i" => \$jobs, | |
) or die "error in command line arguments\n"; | |
my @scripts = @ARGV; | |
my %pids; | |
my @failures; | |
my $tempdir = tempdir(CLEANUP => 1); | |
while (@scripts or %pids) { | |
# spawn | |
while (@scripts and keys %pids < $jobs) { | |
my $script = shift @scripts; | |
$pids{spawn_script($script)} = $script; | |
} | |
# wait for tasks to exit | |
my $kid = wait; | |
if ($pids{$kid}) { | |
my $script = $pids{$kid}; | |
delete $pids{$kid}; | |
# add to the failed list, if necessary | |
push @failures, $script | |
unless WIFEXITED($?) and WEXITSTATUS($?) == 0; | |
# print the output of the perl script being run | |
my $logfn = get_logfn($script); | |
open my $logfh, "<", $logfn | |
or die "failed to open script:$logfn:$!"; | |
unlink $logfn; | |
print do { local $/; <$logfh> }; | |
} | |
} | |
if (@failures) { | |
print STDERR join "\n", "failed:", @failures, ""; | |
exit 1; | |
} else { | |
print STDERR "all ok\n"; | |
exit 0; | |
} | |
sub spawn_script { | |
my $script = shift; | |
my $logfn = get_logfn($script); | |
open my $logfh, ">", $logfn | |
or die "failed to create log file:$logfn:$!"; | |
my $pid = fork; | |
die "fork failed:$!" | |
unless defined $pid; | |
# return the spawned PID if in parent process | |
return $pid if $pid != 0; | |
# child process, redirect STDOUT, STDERR to tempfile and exec | |
open STDOUT, ">&", $logfh | |
or die "failed to redirect STDOUT:$!"; | |
open STDERR, ">&", $logfh | |
or die "failed to redirect STDERR:$!"; | |
exec $^X, $script; | |
die "failed to exec $^X $script:$!"; | |
} | |
sub get_logfn { | |
my $script = shift; | |
$script =~ s{/}{__}g; | |
"$tempdir/$script.out"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment