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

Commit

Permalink
[JENKINS-34021] If DurableTaskStep.Execution.listener is sometimes null
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Aug 3, 2016
1 parent b83464e commit 344aa77
Showing 1 changed file with 28 additions and 4 deletions.
Expand Up @@ -31,23 +31,27 @@
import hudson.Launcher;
import hudson.model.Computer;
import hudson.model.TaskListener;
import hudson.util.LogTaskListener;
import jenkins.model.Jenkins;
import jenkins.util.Timer;
import org.jenkinsci.plugins.durabletask.Controller;
import org.jenkinsci.plugins.durabletask.DurableTask;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

/**
* Runs an durable task on a slave, such as a shell script.
Expand Down Expand Up @@ -134,13 +138,33 @@ public static final class Execution extends AbstractStepExecutionImpl implements
return ws;
}

private @Nonnull PrintStream logger() {
TaskListener l = listener;
if (l == null) {
StepContext context = getContext();
try {
l = context.get(TaskListener.class);
if (l != null) {
LOGGER.log(Level.WARNING, "JENKINS-34021: DurableTaskStep.Execution.listener not restored in {0}", context);
} else {
LOGGER.log(Level.WARNING, "JENKINS-34021: TaskListener not even available upon request in {0}", context);
l = new LogTaskListener(LOGGER, Level.FINE);
}
} catch (Exception x) {
LOGGER.log(Level.WARNING, "JENKINS-34021: could not get TaskListener in " + context, x);
l = new LogTaskListener(LOGGER, Level.FINE);
}
}
return l.getLogger();
}

@Override public void stop(Throwable cause) throws Exception {
FilePath workspace = getWorkspace();
if (workspace != null) {
listener.getLogger().println("Sending interrupt signal to process");
logger().println("Sending interrupt signal to process");
controller.stop(workspace, getContext().get(Launcher.class));
} else {
listener.getLogger().println("Could not connect to " + node + " to send interrupt signal to process");
logger().println("Could not connect to " + node + " to send interrupt signal to process");
}
}

Expand Down Expand Up @@ -178,7 +202,7 @@ private void check() {
}
}, 10, TimeUnit.SECONDS);
try {
if (controller.writeLog(workspace, listener.getLogger())) {
if (controller.writeLog(workspace, logger())) {
getContext().saveState();
recurrencePeriod = MIN_RECURRENCE_PERIOD; // got output, maybe we will get more soon
} else {
Expand All @@ -188,7 +212,7 @@ private void check() {
if (exitCode == null) {
LOGGER.log(Level.FINE, "still running in {0} on {1}", new Object[] {remote, node});
} else {
if (controller.writeLog(workspace, listener.getLogger())) {
if (controller.writeLog(workspace, logger())) {
LOGGER.log(Level.FINE, "last-minute output in {0} on {1}", new Object[] {remote, node});
}
t.set(null); // do not interrupt cleanup
Expand Down

0 comments on commit 344aa77

Please sign in to comment.