Skip to content

Commit 2d3f5c6

Browse files
authored
Merge pull request jenkinsci#478 from jglick/AgentReconnectionListener
Optimized `AgentReconnectionListener`
2 parents bf7088b + 83386f4 commit 2d3f5c6

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/DurableTaskStep.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import org.jenkinsci.plugins.workflow.FilePathUtils;
7676
import org.jenkinsci.plugins.workflow.actions.LabelAction;
7777
import org.jenkinsci.plugins.workflow.flow.FlowExecutionList;
78+
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
7879
import org.jenkinsci.plugins.workflow.graph.FlowNode;
7980
import org.jenkinsci.plugins.workflow.log.OutputStreamTaskListener;
8081
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
@@ -765,6 +766,9 @@ private static class HandlerImpl extends Handler {
765766
@Extension public static final class AgentReconnectionListener extends ComputerListener {
766767

767768
@Override public void onOffline(Computer c, OfflineCause cause) {
769+
if (!USE_WATCHING) {
770+
return;
771+
}
768772
if (Jenkins.get().isTerminating()) {
769773
LOGGER.fine(() -> "Skipping check on " + c.getName() + " during shutdown");
770774
return;
@@ -773,6 +777,9 @@ private static class HandlerImpl extends Handler {
773777
}
774778

775779
@Override public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {
780+
if (!USE_WATCHING) {
781+
return;
782+
}
776783
if (!FlowExecutionList.get().isResumptionComplete()) {
777784
LOGGER.fine(() -> "Skipping check on " + c.getName() + " before builds are ready");
778785
return;
@@ -782,12 +789,33 @@ private static class HandlerImpl extends Handler {
782789

783790
private void check(Computer c) {
784791
String name = c.getName();
785-
StepExecution.acceptAll(Execution.class, exec -> {
786-
if (exec.watching && exec.node.equals(name)) {
787-
LOGGER.fine(() -> "Online/offline event on " + name + ", checking current status of " + exec.remote + " soon");
788-
threadPool().schedule(exec::check, 15, TimeUnit.SECONDS);
792+
// More efficient than StepExecution.acceptAll(Execution.class, exec -> …):
793+
for (var executor : c.getExecutors()) {
794+
var executable = executor.getCurrentExecutable();
795+
if (executable != null && executable.getParentExecutable() instanceof FlowExecutionOwner.Executable feoe) {
796+
var feo = feoe.asFlowExecutionOwner();
797+
if (feo != null) {
798+
try {
799+
var fe = feo.get();
800+
var executionsFuture = fe.getCurrentExecutions(true);
801+
executionsFuture.addListener(() -> {
802+
try {
803+
for (var execution : executionsFuture.get()) {
804+
if (execution instanceof Execution exec && exec.watching && exec.node.equals(name)) {
805+
LOGGER.fine(() -> "Online/offline event on " + name + ", checking current status of " + exec.remote + " soon");
806+
threadPool().schedule(exec::check, 15, TimeUnit.SECONDS);
807+
}
808+
}
809+
} catch (Exception x) {
810+
LOGGER.log(Level.WARNING, "could not inspect " + fe, x);
811+
}
812+
}, Runnable::run);
813+
} catch (IOException x) {
814+
LOGGER.log(Level.WARNING, "could not inspect " + feo, x);
815+
}
816+
}
789817
}
790-
});
818+
}
791819
}
792820

793821
}

0 commit comments

Comments
 (0)