Skip to content

Commit

Permalink
Merge pull request #698 from romanisb/args/retry-log-write
Browse files Browse the repository at this point in the history
Add --write-log-init-timeout option to allow specifying a timeout to retry writing logs to stdout
  • Loading branch information
oleg-nenashev authored Aug 6, 2023
2 parents f6f1061 + c5eec75 commit 58b0a62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" },

Check warning on line 79 in bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/commands/PipelineRunOptions.java

View check run for this annotation

ci.jenkins.io / Static Analysis

URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD

NORMAL: Unread public/protected field: io.jenkins.jenkinsfile.runner.bootstrap.commands.PipelineRunOptions.writeLogInitTimeoutSeconds
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;
}
15 changes: 7 additions & 8 deletions payload/src/main/java/io/jenkins/jenkinsfile/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 58b0a62

Please sign in to comment.