diff --git a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/PipelineRunOptions.java b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/PipelineRunOptions.java index c277cd14..71fbdee0 100644 --- a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/PipelineRunOptions.java +++ b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/PipelineRunOptions.java @@ -75,4 +75,10 @@ public class PipelineRunOptions extends PipelineOptions { description = "Disable writing build logs to stdout. " + "Plugins that handle build logs will process them as usual") public boolean noBuildLogs = false; + + @CommandLine.Option(names = { "-wlit", "--write-log-init-timeout" }, + description = "Initializing build log forwarding to stdout may fail if the build log has not been created yet. " + + " This option defines the maximum time (in seconds) to wait for build log creation." + + " Defaults to 1 second.") + public int writeLogInitTimeoutSeconds = 1; } diff --git a/payload/src/main/java/io/jenkins/jenkinsfile/runner/Runner.java b/payload/src/main/java/io/jenkins/jenkinsfile/runner/Runner.java index 4883ba9b..90bcf093 100644 --- a/payload/src/main/java/io/jenkins/jenkinsfile/runner/Runner.java +++ b/payload/src/main/java/io/jenkins/jenkinsfile/runner/Runner.java @@ -119,7 +119,7 @@ public int run(PipelineRunOptions runOptions) throws Exception { b = f.getStartCondition().get(); if (!runOptions.noBuildLogs) { - writeLogTo(System.out); + writeLogTo(System.out, runOptions.writeLogInitTimeoutSeconds); } f.get(); // wait for the completion @@ -156,24 +156,23 @@ private CauseAction createCauseAction(String cause) { return new CauseAction(c); } - private void writeLogTo(PrintStream out) throws IOException, InterruptedException { - final int retryCnt = 10; - - // read output in a retry loop, by default try only once + private void writeLogTo(PrintStream out, int timeoutSeconds) throws IOException, InterruptedException { + // read output in a retry loop, // writeWholeLogTo may fail with FileNotFound // exception on a slow/busy machine, if it takes // longish to create the log file int retryInterval = 100; - for (int i=0;i<=retryCnt;) { + long timeoutMillis = timeoutSeconds * 1000; + long startTime = System.currentTimeMillis(); + while (true) { try { b.writeWholeLogTo(out); break; } catch (FileNotFoundException | NoSuchFileException e) { - if ( i == retryCnt ) { + if ( System.currentTimeMillis() - startTime > timeoutMillis ) { throw e; } - i++; Thread.sleep(retryInterval); } }