Skip to content

Commit 1c6fb78

Browse files
gbhat618MarkEWaitetimjaVlatombe
authored
Add a method isConnected to IComputer and provide implementation in Computer (#10607)
Co-authored-by: Mark Waite <mark.earl.waite@gmail.com> Co-authored-by: Tim Jacomb <21194782+timja@users.noreply.github.com> Co-authored-by: Vincent Latombe <vincent@latombe.net>
1 parent 00c6707 commit 1c6fb78

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

core/src/main/java/hudson/model/Computer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,19 @@ public final boolean isOnline() {
625625
return !isOffline();
626626
}
627627

628+
/**
629+
* {@inheritDoc}
630+
* <p>
631+
* Uses {@link #getChannel()} to check the connection.
632+
* A connected agent may still be offline for scheduling if marked temporarily offline.
633+
* @return {@code true} if the agent is connected, {@code false} otherwise.
634+
* @see #isOffline()
635+
*/
636+
@Override
637+
public boolean isConnected() {
638+
return getChannel() != null;
639+
}
640+
628641
/**
629642
* This method is called to determine whether manual launching of the agent is allowed at this point in time.
630643
* @return {@code true} if manual launching of the agent is allowed at this point in time.

core/src/main/java/jenkins/model/IComputer.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public interface IComputer extends AccessControlled, IconSpec, ModelObject, Name
5454
List<? extends IDisplayExecutor> getDisplayExecutors();
5555

5656
/**
57-
* @return {@code true} if the node is offline. {@code false} if it is online.
57+
* Returns whether the agent is offline for scheduling new tasks.
58+
* Even if {@code true}, the agent may still be connected to the controller and executing a task,
59+
* but is considered offline for scheduling.
60+
* @return {@code true} if the agent is offline; {@code false} if online.
61+
* @see #isConnected()
5862
*/
5963
boolean isOffline();
6064

@@ -180,10 +184,21 @@ default String getIconClassName() {
180184
int countExecutors();
181185

182186
/**
183-
* @return true if the computer is online.
187+
* Indicates whether the agent can accept a new task when it becomes idle.
188+
* {@code false} does not necessarily mean the agent is disconnected.
189+
* @return {@code true} if the agent is online.
190+
* @see #isConnected()
184191
*/
185192
boolean isOnline();
186193

194+
/**
195+
* Indicates whether the agent is actually connected to the controller.
196+
* @return {@code true} if the agent is connected to the controller.
197+
*/
198+
default boolean isConnected() {
199+
return isOnline();
200+
}
201+
187202
/**
188203
* @return the number of {@link IExecutor}s that are idle right now.
189204
*/

test/src/test/java/hudson/model/ComputerTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.hamcrest.Matchers.containsInAnyOrder;
3232
import static org.hamcrest.Matchers.containsString;
3333
import static org.hamcrest.Matchers.equalTo;
34+
import static org.hamcrest.Matchers.is;
3435
import static org.hamcrest.Matchers.isA;
3536
import static org.hamcrest.Matchers.not;
3637
import static org.hamcrest.Matchers.nullValue;
@@ -320,4 +321,37 @@ void computersCollected() throws Exception {
320321
MemoryAssert.assertGC(channelRef, false);
321322
}
322323

324+
@Test
325+
public void isConnectedTest() throws Exception {
326+
var agent = j.createSlave();
327+
var computer = agent.toComputer();
328+
329+
// Verify initial state: computer is not connected
330+
assertThat(computer.isOnline(), is(false));
331+
assertThat(computer.isConnected(), is(false));
332+
333+
// Connect the computer
334+
computer.connect(false);
335+
await("computer should be online after connect").until(() -> computer.isOnline(), is(true));
336+
assertThat(computer.isConnected(), is(true));
337+
assertThat(computer.isOffline(), is(false));
338+
339+
// Mark computer temporary offline
340+
computer.doToggleOffline(null);
341+
assertThat("temporary offline agent is still connected", computer.isConnected(), is(true));
342+
assertThat("temporary offline agent is not available for scheduling", computer.isOnline(), is(false));
343+
assertThat(computer.isOffline(), is(true));
344+
345+
// Bring it back online
346+
computer.doToggleOffline(null);
347+
assertThat(computer.isOnline(), is(true));
348+
assertThat(computer.isConnected(), is(true)); // channel is still there.
349+
350+
// Disconnect the computer
351+
computer.disconnect(new OfflineCause.UserCause(null, null));
352+
// wait for the slave process to be killed
353+
await("disconnected agent is not available for scheduling").until(() -> computer.isOnline(), is(false));
354+
assertThat(computer.isConnected(), is(false));
355+
assertThat(computer.isOffline(), is(true));
356+
}
323357
}

0 commit comments

Comments
 (0)