Avoid duplicate assignment.

This commit is contained in:
Michael Lipp 2025-01-31 22:09:17 +01:00
parent b78b33a6f1
commit b5ae22a8ea

View file

@ -264,8 +264,6 @@ public class VmMonitor extends
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void onAssignVm(AssignVm event) public void onAssignVm(AssignVm event)
throws ApiException, InterruptedException { throws ApiException, InterruptedException {
VmPool vmPool = null;
while (true) {
// Search for existing assignment. // Search for existing assignment.
var assignedVm = channelManager.channels().stream() var assignedVm = channelManager.channels().stream()
.filter(c -> c.vmDefinition().assignedFrom() .filter(c -> c.vmDefinition().assignedFrom()
@ -280,14 +278,12 @@ public class VmMonitor extends
} }
// Get the pool definition for retention time calculations // Get the pool definition for retention time calculations
if (vmPool == null) { VmPool vmPool = newEventPipeline().fire(new GetPools()
vmPool = newEventPipeline().fire(new GetPools()
.withName(event.fromPool())).get().stream().findFirst() .withName(event.fromPool())).get().stream().findFirst()
.orElse(null); .orElse(null);
if (vmPool == null) { if (vmPool == null) {
return; return;
} }
}
// Find available VM. // Find available VM.
var pool = vmPool; var pool = vmPool;
@ -316,12 +312,12 @@ public class VmMonitor extends
assignment.set("lastUsed", Instant.now().toString()); assignment.set("lastUsed", Instant.now().toString());
return status; return status;
}); });
event.setResult(new VmData(vmDef, assignedVm.get()));
// Make sure that a newly assigned VM is running. // Make sure that a newly assigned VM is running.
fire(new ModifyVm(vmDef.name(), "state", "Running", fire(new ModifyVm(vmDef.name(), "state", "Running",
assignedVm.get())); assignedVm.get()));
} }
}
private static Comparator<VmChannel> preferRunning private static Comparator<VmChannel> preferRunning
= new Comparator<>() { = new Comparator<>() {