Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
fix uploading of build console logs to remote server
Browse files Browse the repository at this point in the history
Fixes issue JENKINS-21138

An exception occurs during console log upload causing only partial logs
to be copied to the remote server.  The problem is that the session object
is not thread safe.  To fix this problem we need to move the session
object into the ConsoleRunnable thread.  Now the console upload always
runs in a completely detached thread.

Notes:
1. Needed to update jenkins version to 1.430 because 1.403 would not build
   anymore.  I think 1.430 should be conservative enought to be compatible
   with most of the existing jenkins installs.
2. Updated com.jcraft.jsch depenency to version 1.50
  • Loading branch information
zaro0508 committed Dec 24, 2013
1 parent 365bda2 commit 4939909
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.403</version>
<version>1.430</version>
<relativePath/>
</parent>

Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
<version>0.1.50</version>
</dependency>
</dependencies>

Expand Down
Expand Up @@ -182,30 +182,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
folderPath = folderPath.trim();

if (e.copyConsoleLog) {
final OutputStream out;
final BufferedWriter writer;
final Thread consoleWriterThread;
final Session consoleSession;
final ChannelSftp consoleChannel;
if (entries.size() > 1) {
// If we are copying more than the console log
// we need a separate connection for the console
// log due to threading/lock issues.
consoleSession = scpsite.createSession(null);
consoleChannel = scpsite.createChannel(null, consoleSession);
}
else {
// Otherwise use the existing connection.
consoleSession = session;
consoleChannel = channel;
delaySessionClose = true;
}

out = scpsite.createOutStream(folderPath,
"console.html", logger, consoleChannel);
writer = new BufferedWriter(new OutputStreamWriter(out));
consoleWriterThread = new Thread(new consoleRunnable(
build, scpsite, consoleSession, consoleChannel, writer));
consoleWriterThread = new Thread(new consoleRunnable(build, scpsite, folderPath));
log(logger, "Copying console log.");
consoleWriterThread.start();

Expand Down Expand Up @@ -287,6 +265,7 @@ protected DescriptorImpl(Class<? extends Publisher> clazz) {

private final CopyOnWriteList<SCPSite> sites = new CopyOnWriteList<SCPSite>();

@Override
public String getDisplayName() {
return Messages.SCPRepositoryPublisher_DisplayName();
}
Expand Down Expand Up @@ -343,27 +322,31 @@ protected void log(final PrintStream logger, final String message) {
private class consoleRunnable implements Runnable {
private final AbstractBuild build;
private final SCPSite scpsite;
private final Session session;
private final ChannelSftp channel;
private final BufferedWriter writer;
private final String path;

consoleRunnable(AbstractBuild build, SCPSite scpsite, Session session,
ChannelSftp channel, BufferedWriter writer) {
consoleRunnable(AbstractBuild build, SCPSite scpsite, String path) {
this.build = build;
this.scpsite = scpsite;
this.session = session;
this.channel = channel;
this.writer = writer;
this.path = path;
}

public void run () {
AnnotatedLargeText logText;
final StringWriter strWriter;
strWriter = new StringWriter();
long pos = 0;
Session session = null;
ChannelSftp channel = null;
OutputStream out = null;
BufferedWriter writer = null;

try {
strWriter.write("<pre>\n");
session = scpsite.createSession(null);
channel = scpsite.createChannel(null, session);
out = scpsite.createOutStream(path, "console.html", null, channel);
writer = new BufferedWriter(new OutputStreamWriter(out));

do {
logText = build.getLogText();
// Use strWriter as temp storage because
Expand All @@ -386,9 +369,15 @@ public void run () {
writer.flush();
writer.close();
} catch (IOException e) {
//Ignore the error not much we can do about it.
LOGGER.info("Failed to upload console log: " + e.getMessage());
} catch (JSchException e) {
LOGGER.info("Failed to upload console log: " + e.getMessage());
} catch (SftpException e) {
LOGGER.info("Failed to upload console log: " + e.getMessage());
} finally {
scpsite.closeSession(null, session, channel);
if (scpsite != null) {
scpsite.closeSession(null, session, channel);
}
}
}
}
Expand Down

0 comments on commit 4939909

Please sign in to comment.