Skip to content

Commit

Permalink
Merge pull request #669 from dwnusbaum/flush-log-on-shutdown
Browse files Browse the repository at this point in the history
Flush overall Pipeline log stream when shutting down Jenkins
  • Loading branch information
dwnusbaum authored Mar 7, 2023
2 parents 8177e69 + 6888d82 commit edb8602
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ public void pause(final boolean v) throws IOException {
LOGGER.log(Level.WARNING, null, t);
}
});
cpsExec.getOwner().getListener().getLogger().close();
}
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Error persisting Pipeline execution at shutdown: "+((CpsFlowExecution) execution).owner, ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.jenkinsci.plugins.workflow.cps;

import hudson.Functions;
import java.util.logging.Level;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -21,4 +25,28 @@ private static void doesItSmoke(JenkinsRule r) throws Exception {
p.setDefinition(new CpsFlowDefinition("print Jenkins.get().getRootDir().toString()", false));
r.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

@Test
public void flushLogsOnShutdown() throws Throwable {
Assume.assumeFalse("RealJenkinsRule does not shut down Jenkins cleanly on Windows, see https://github.com/jenkinsci/jenkins-test-harness/pull/559", Functions.isWindows());
rjr.withLogger(CpsFlowExecution.class, Level.FINER);
rjr.then(CpsFlowDefinitionRJRTest::flushLogsOnShutdownPreRestart);
rjr.then(CpsFlowDefinitionRJRTest::flushLogsOnShutdownPostRestart);
}

private static void flushLogsOnShutdownPreRestart(JenkinsRule r) throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("sleep 10\n", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
r.waitForMessage("Sleeping for 10 sec", b);
r.jenkins.doQuietDown();
}

private static void flushLogsOnShutdownPostRestart(JenkinsRule r) throws Exception {
WorkflowJob p = r.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Resuming build at ", b);
r.assertLogContains("Pausing (Preparing for shutdown)", b);
}
}

0 comments on commit edb8602

Please sign in to comment.