Skip to content

Commit

Permalink
JENKINS-15972 No display available in post build actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoran Regvart committed Dec 9, 2012
1 parent 7beee89 commit 2029cb2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
55 changes: 46 additions & 9 deletions src/main/java/org/jenkinsci/plugins/xvfb/XvfbBuildWrapper.java
Expand Up @@ -38,6 +38,8 @@
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.listeners.RunListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.tools.ToolInstallation;
Expand All @@ -55,6 +57,25 @@

public class XvfbBuildWrapper extends BuildWrapper {

@SuppressWarnings("rawtypes")
@Extension
public static final RunListener<Run> xvfbShutdownListener = new RunListener<Run>() {
@Override
public void onCompleted(Run r, TaskListener listener) {
XvfbEnvironment xvfbEnvironment = r.getAction(XvfbEnvironment.class);

if (xvfbEnvironment != null && xvfbEnvironment.isShutdownWithBuild()) {
try {
shutdownAndCleanup(xvfbEnvironment, listener);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};

@Extension
public static class XvfbBuildWrapperDescriptor extends BuildWrapperDescriptor {

Expand Down Expand Up @@ -149,9 +170,12 @@ private FormValidation validateOptionalPositiveInteger(final String value) {
/** Additional options to be passed to Xvfb */
private final String additionalOptions;

/** Should the Xvfb display be around for post build actions, i.e. should it terminate with the whole build */
private boolean shutdownWithBuild = false;

@DataBoundConstructor
public XvfbBuildWrapper(final String installationName, final Integer displayName, final String screen, final Boolean debug, final int timeout, final int displayNameOffset,
final String additionalOptions) {
final String additionalOptions, Boolean shutdownWithBuild) {
this.installationName = installationName;
this.displayName = displayName;

Expand All @@ -171,6 +195,8 @@ public XvfbBuildWrapper(final String installationName, final Integer displayName
this.displayNameOffset = displayNameOffset;
}
this.additionalOptions = additionalOptions;

this.shutdownWithBuild = shutdownWithBuild;
}

public String getAdditionalOptions() {
Expand Down Expand Up @@ -222,6 +248,10 @@ public boolean isDebug() {
return debug;
}

public boolean isShutdownWithBuild() {
return shutdownWithBuild;
}

private XvfbEnvironment launchXvfb(@SuppressWarnings("rawtypes") final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {
int displayNameUsed;

Expand Down Expand Up @@ -278,7 +308,9 @@ private XvfbEnvironment launchXvfb(@SuppressWarnings("rawtypes") final AbstractB

Thread.sleep(timeout * MILLIS_IN_SECOND);

return new XvfbEnvironment(frameBufferDir, displayNameUsed, process);
final XvfbEnvironment xvfbEnvironment = new XvfbEnvironment(frameBufferDir, displayNameUsed, process, shutdownWithBuild);

return xvfbEnvironment;
}

@Override
Expand All @@ -292,6 +324,15 @@ public void makeBuildVariables(@SuppressWarnings("rawtypes") final AbstractBuild
}
}

private static void shutdownAndCleanup(XvfbEnvironment environment, TaskListener listener) throws IOException, InterruptedException {
final FilePath frameBufferDir = environment.getFrameBufferDir();
final Proc process = environment.getProcess();

listener.getLogger().println(Messages.XvfbBuildWrapper_Stopping());
process.kill();
frameBufferDir.deleteRecursive();
}

@Override
public Environment setUp(@SuppressWarnings("rawtypes") final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {
if (!launcher.isUnix()) {
Expand All @@ -308,10 +349,6 @@ public Environment setUp(@SuppressWarnings("rawtypes") final AbstractBuild build

final int displayNameUsed = xvfbEnvironment.getDisplayNameUsed();

final FilePath frameBufferDir = xvfbEnvironment.getFrameBufferDir();

final Proc process = xvfbEnvironment.getProcess();

return new Environment() {
@Override
public void buildEnvVars(final Map<String, String> env) {
Expand All @@ -320,9 +357,9 @@ public void buildEnvVars(final Map<String, String> env) {

@Override
public boolean tearDown(@SuppressWarnings("rawtypes") final AbstractBuild build, final BuildListener listener) throws IOException, InterruptedException {
listener.getLogger().println(Messages.XvfbBuildWrapper_Stopping());
process.kill();
frameBufferDir.deleteRecursive();
if (!shutdownWithBuild) {
shutdownAndCleanup(xvfbEnvironment, listener);
}

return true;
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/jenkinsci/plugins/xvfb/XvfbEnvironment.java
Expand Up @@ -14,10 +14,14 @@ public class XvfbEnvironment extends InvisibleAction {
/** Handle to the Xvfb process. */
private final transient Proc process;

public XvfbEnvironment(final FilePath frameBufferDir, final int displayNameUsed, final Proc process) {
/** The shutdownWithBuild indicator from the job configuration. */
private final transient boolean shutdownWithBuild;

public XvfbEnvironment(final FilePath frameBufferDir, final int displayNameUsed, final Proc process, boolean shutdownWithBuild) {
this.frameBufferDir = frameBufferDir;
this.displayNameUsed = displayNameUsed;
this.process = process;
this.shutdownWithBuild = shutdownWithBuild;
}

public int getDisplayNameUsed() {
Expand All @@ -31,4 +35,8 @@ public FilePath getFrameBufferDir() {
public Proc getProcess() {
return process;
}

public boolean isShutdownWithBuild() {
return shutdownWithBuild;
}
}
Expand Up @@ -64,6 +64,12 @@ SUCH DAMAGE.
<f:entry title="${%Log Xvfb output}" field="debug">
<f:checkbox value="${instance.debug}" />
</f:entry>

<f:entry
title="${%Shoutdown Xvfb with whole job, not just with the main build action}"
field="shutdownWithBuild">
<f:checkbox value="${instance.shutdownWithBuild}" />
</f:entry>
</f:advanced>

</j:jelly>
@@ -0,0 +1,26 @@
<!--
Copyright (c) 2012 Zoran Regvart All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
-->
<div>Should the display be kept until the whole job ends
(including the post build steps).</div>

0 comments on commit 2029cb2

Please sign in to comment.