Search in sources :

Example 1 with AsynchronousExecution

use of jenkins.model.queue.AsynchronousExecution 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;
}
Also used : AsynchronousExecution(jenkins.model.queue.AsynchronousExecution) NullStream(hudson.util.NullStream) WithThreadName(org.jenkinsci.plugins.workflow.support.concurrent.WithThreadName) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamBuildListener(hudson.model.StreamBuildListener) AbortException(hudson.AbortException) IOException(java.io.IOException) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) ScheduledFuture(java.util.concurrent.ScheduledFuture) Jenkins(jenkins.model.Jenkins) Executor(hudson.model.Executor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Collection(java.util.Collection)

Aggregations

AbortException (hudson.AbortException)1 Executor (hudson.model.Executor)1 StreamBuildListener (hudson.model.StreamBuildListener)1 NullStream (hudson.util.NullStream)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Jenkins (jenkins.model.Jenkins)1 AsynchronousExecution (jenkins.model.queue.AsynchronousExecution)1 FlowInterruptedException (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException)1 WithThreadName (org.jenkinsci.plugins.workflow.support.concurrent.WithThreadName)1