Skip to content

Commit 5823cdd

Browse files
author
smhc
committed
Guarantee uniqueness of agent name
1 parent 5591181 commit 5823cdd

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/main/java/org/jenkinsci/plugins/docker/swarm/BuildScheduler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import java.io.IOException;
1212
import java.util.logging.Level;
1313
import java.util.logging.Logger;
14+
import java.util.concurrent.atomic.AtomicInteger;
1415

1516
public class BuildScheduler {
1617
private static final Logger LOGGER = Logger.getLogger(BuildScheduler.class.getName());
18+
private static AtomicInteger counter = new AtomicInteger(1);
19+
1720
public static void scheduleBuild(final Queue.BuildableItem bi) {
1821
try (ACLContext _ = ACL.as(ACL.SYSTEM)) {
1922
final DockerSwarmLabelAssignmentAction action = createLabelAssignmentAction(bi.task.getDisplayName());
@@ -35,18 +38,20 @@ public static void scheduleBuild(final Queue.BuildableItem bi) {
3538
}
3639

3740
private static DockerSwarmLabelAssignmentAction createLabelAssignmentAction(String taskName) {
41+
3842
try {
3943
Thread.sleep(5, 10);
4044
} catch (final InterruptedException e) {
4145
LOGGER.log(Level.INFO,"couldn't add agent", e);
4246
}
4347
taskName = taskName.replaceAll("[^a-zA-Z0-9]", "_");
44-
if (taskName.length() > 15) {
45-
taskName = taskName.substring(taskName.length() - 15);
48+
if (taskName.length() > 30) {
49+
taskName = taskName.substring(taskName.length() - 30);
4650
}
4751
String truncatedTime = Long.toString(System.nanoTime());
48-
truncatedTime = truncatedTime.substring(truncatedTime.length() - 5);
49-
50-
return new DockerSwarmLabelAssignmentAction("agent-" + taskName + "-" + truncatedTime);
52+
truncatedTime = truncatedTime.substring(truncatedTime.length() - 10);
53+
54+
return new DockerSwarmLabelAssignmentAction("agent-" + taskName + "-" +
55+
truncatedTime + "-" + BuildScheduler.counter.incrementAndGet());
5156
}
5257
}

0 commit comments

Comments
 (0)