From f06bd7813de6177e385fa6529696c6fe99126385 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 14 Aug 2020 18:20:29 -0700 Subject: [PATCH] Replace anonymous Runnable with lambda --- .../steps/durable_task/DurableTaskStep.java | 26 +++---- .../support/steps/ExecutorStepExecution.java | 77 ++++++++----------- 2 files changed, 45 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/DurableTaskStep.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/DurableTaskStep.java index c073ee26..7d9c3214 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/DurableTaskStep.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/DurableTaskStep.java @@ -494,21 +494,19 @@ private static final class NewlineSafeTaskListener implements TaskListener { if (workspace != null) { listener().getLogger().println("Sending interrupt signal to process"); LOGGER.log(Level.FINE, "stopping process", cause); - stopTask = Timer.get().schedule(new Runnable() { - @Override public void run() { - stopTask = null; - if (recurrencePeriod > 0) { - recurrencePeriod = 0; - listener().getLogger().println("After " + REMOTE_TIMEOUT + "s process did not stop"); - getContext().onFailure(cause); - try { - FilePath workspace = getWorkspace(); - if (workspace != null) { - controller.cleanup(workspace); - } - } catch (IOException | InterruptedException x) { - Functions.printStackTrace(x, listener().getLogger()); + stopTask = Timer.get().schedule(() -> { + stopTask = null; + if (recurrencePeriod > 0) { + recurrencePeriod = 0; + listener().getLogger().println("After " + REMOTE_TIMEOUT + "s process did not stop"); + getContext().onFailure(cause); + try { + FilePath taskWorkspace = getWorkspace(); + if (taskWorkspace != null) { + controller.cleanup(taskWorkspace); } + } catch (IOException | InterruptedException x) { + Functions.printStackTrace(x, listener().getLogger()); } } }, REMOTE_TIMEOUT, TimeUnit.SECONDS); diff --git a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java index c5950325..a27f6c69 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStepExecution.java @@ -108,22 +108,20 @@ public boolean start() throws Exception { } getContext().get(FlowNode.class).addAction(new QueueItemActionImpl(waitingItem.getId())); - Timer.get().schedule(new Runnable() { - @Override public void run() { - Queue.Item item = Queue.getInstance().getItem(task); - if (item != null) { - TaskListener listener; - try { - listener = getContext().get(TaskListener.class); - } catch (Exception x) { // IOException, InterruptedException - LOGGER.log(FINE, "could not print message to build about " + item + "; perhaps it is already completed", x); - return; - } - listener.getLogger().println("Still waiting to schedule task"); - CauseOfBlockage cob = item.getCauseOfBlockage(); - if (cob != null) { - cob.print(listener); - } + Timer.get().schedule(() -> { + Queue.Item item = Queue.getInstance().getItem(task); + if (item != null) { + TaskListener listener; + try { + listener = getContext().get(TaskListener.class); + } catch (Exception x) { // IOException, InterruptedException + LOGGER.log(FINE, "could not print message to build about " + item + "; perhaps it is already completed", x); + return; + } + listener.getLogger().println("Still waiting to schedule task"); + CauseOfBlockage cob = item.getCauseOfBlockage(); + if (cob != null) { + cob.print(listener); } } }, 15, TimeUnit.SECONDS); @@ -732,23 +730,16 @@ private static void finish(@CheckForNull final String cookie) { return; } assert runningTask.launcher != null; - Timer.get().submit(new Runnable() { // JENKINS-31614 - @Override public void run() { - execution.completed(null); - } - }); - Computer.threadPoolForRemoting.submit(new Runnable() { // JENKINS-34542, JENKINS-45553 - @Override - public void run() { - try { - runningTask.launcher.kill(Collections.singletonMap(COOKIE_VAR, cookie)); - } catch (ChannelClosedException x) { - // fine, Jenkins was shutting down - } catch (RequestAbortedException x) { - // slave was exiting; too late to kill subprocesses - } catch (Exception x) { - LOGGER.log(Level.WARNING, "failed to shut down " + cookie, x); - } + Timer.get().submit(() -> execution.completed(null)); // JENKINS-31614 + Computer.threadPoolForRemoting.submit(() -> { // JENKINS-34542, JENKINS-45553 + try { + runningTask.launcher.kill(Collections.singletonMap(COOKIE_VAR, cookie)); + } catch (ChannelClosedException x) { + // fine, Jenkins was shutting down + } catch (RequestAbortedException x) { + // slave was exiting; too late to kill subprocesses + } catch (Exception x) { + LOGGER.log(Level.WARNING, "failed to shut down " + cookie, x); } }); } @@ -889,18 +880,16 @@ private final class PlaceholderExecutable implements ContinuableExecutable, Acce } LOGGER.log(FINE, "interrupted {0}", cookie); // TODO save the BodyExecution somehow and call .cancel() here; currently we just interrupt the build as a whole: - Timer.get().submit(new Runnable() { // JENKINS-46738 - @Override public void run() { - Executor masterExecutor = r.getExecutor(); - if (masterExecutor != null) { - masterExecutor.interrupt(); - } else { // anomalous state; perhaps build already aborted but this was left behind; let user manually cancel executor slot - Executor thisExecutor = /* AsynchronousExecution. */getExecutor(); - if (thisExecutor != null) { - thisExecutor.recordCauseOfInterruption(r, _listener); - } - completed(null); + Timer.get().submit(() -> { // JENKINS-46738 + Executor masterExecutor = r.getExecutor(); + if (masterExecutor != null) { + masterExecutor.interrupt(); + } else { // anomalous state; perhaps build already aborted but this was left behind; let user manually cancel executor slot + Executor thisExecutor = /* AsynchronousExecution. */getExecutor(); + if (thisExecutor != null) { + thisExecutor.recordCauseOfInterruption(r, _listener); } + completed(null); } }); }