Skip to content

Commit

Permalink
Merge pull request #3226 from M0ses/timeout_for_services
Browse files Browse the repository at this point in the history
[backend] timeout for services
  • Loading branch information
M0ses committed Jun 29, 2017
2 parents 6660282 + 65bb858 commit 4e5ee4e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/backend/BSConfiguration.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ $BSConfig::rundir = $BSConfig::rundir || "$B
$BSConfig::servicetempdir = $BSConfig::servicetempdir || "$BSConfig::bsdir/service";
$BSConfig::scm_cache_high_watermark = $BSConfig::scm_cache_high_watermark || 80;
$BSConfig::scm_cache_low_watermark = $BSConfig::scm_cache_low_watermark || 70;
$BSConfig::service_timeout = $BSConfig::service_timeout || 3600;

1;
18 changes: 17 additions & 1 deletion src/backend/bs_service
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,24 @@ sub run_source_update {

BSUtil::printlog("Running command '@run'");
# call the service
if (! open(SERVICE, '-|')) {
my $child_pid = open(SERVICE, '-|');
die "500 Unable to open pipe: $!\n" unless defined($child_pid);
if (! $child_pid) {
open(STDERR, ">&STDOUT");
exec(@run);
die("$run[0]: $!\n");
}

local $SIG{ALRM} = sub {
kill 'TERM', $child_pid;
die "500 timeout while execution of $name\n";
};

# Wait $BSConfig::service_timeout or per default 7200 sec (2 hours) for
# service to finish
BSUtil::printlog("Waiting $BSConfig::service_timeout for service($child_pid) to finish\n") if $verbose;
alarm($BSConfig::service_timeout);

# collect output
my $output = '';
while (<SERVICE>) {
Expand All @@ -192,6 +204,7 @@ sub run_source_update {

if (close SERVICE) {
# SUCCESS, move files inside and add prefix
BSUtil::printlog('Service succeed') if $verbose;
for my $file (grep {!/^[:\.]/} ls("$myworkdir/out")) {
next if -l "$myworkdir/out/$file" || ! -f _; # only plain files for now
my $tfile = $file;
Expand All @@ -201,13 +214,16 @@ sub run_source_update {
}
} else {
# FAILURE, Create error file
BSUtil::printlog("Service failed: $!") if $verbose;
$output =~ s/[\r\n\s]+$//s;
BSUtil::cleandir('.');
die("500 remote execution error in $name detected\n") if $? >> 8 == 3;
BSUtil::writestr('_service_error', undef, "service $name failed:\n$output\n");
$error = 1;
}

alarm(0);

# delete no longer needed outdir
rm_rf("$myworkdir/out");

Expand Down
2 changes: 1 addition & 1 deletion src/backend/bs_servicedispatch
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ sub runservice {
'data' => \&BSHTTP::cpio_sender,
'cpiofiles' => \@send,
'directory' => $odir,
'timeout' => 3600,
'timeout' => $BSConfig::service_timeout,
'withmd5' => 1,
'receiver' => \&BSHTTP::cpio_receiver,
}, undef);
Expand Down

0 comments on commit 4e5ee4e

Please sign in to comment.