use of org.jenkinsci.plugins.workflow.support.concurrent.WithThreadName in project workflow-job-plugin by jenkinsci.
the class WorkflowRun method sleep.
private AsynchronousExecution sleep() {
final AsynchronousExecution asynchronousExecution = new AsynchronousExecution() {
@Override
public void interrupt(boolean forShutdown) {
if (forShutdown) {
return;
}
Timer.get().submit(new Runnable() {
@Override
public void run() {
if (execution == null) {
return;
}
Executor executor = getExecutor();
if (executor == null) {
LOGGER.log(Level.WARNING, "Lost executor for {0}", WorkflowRun.this);
return;
}
try {
Collection<CauseOfInterruption> causes = executor.getCausesOfInterruption();
execution.interrupt(executor.abortResult(), causes.toArray(new CauseOfInterruption[causes.size()]));
} catch (Exception x) {
LOGGER.log(Level.WARNING, null, x);
}
executor.recordCauseOfInterruption(WorkflowRun.this, listener);
printLater(StopState.TERM, "Click here to forcibly terminate running steps");
}
});
}
@Override
public boolean blocksRestart() {
return execution != null && execution.blocksRestart();
}
@Override
public boolean displayCell() {
return blocksRestart();
}
};
final AtomicReference<ScheduledFuture<?>> copyLogsTask = new AtomicReference<>();
copyLogsTask.set(copyLogsExecutorService().scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
synchronized (completed) {
if (completed.get()) {
asynchronousExecution.completed(null);
copyLogsTask.get().cancel(false);
return;
}
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null || jenkins.isTerminating()) {
LOGGER.log(Level.FINE, "shutting down, breaking waitForCompletion on {0}", this);
// Stop writing content, in case a new set of objects gets loaded after in-VM restart and starts writing to the same file:
listener.closeQuietly();
listener = new StreamBuildListener(new NullStream());
return;
}
try (WithThreadName naming = new WithThreadName(" (" + WorkflowRun.this + ")")) {
copyLogs();
}
}
}
}, 1, 1, TimeUnit.SECONDS));
return asynchronousExecution;
}
Aggregations