NSMutableArray<MInstance> startingInstances = new NSMutableArray<MInstance>();
for (int i = 0; i < numToStartPerHost; i++) {
for (MHost host : activeHosts) {
NSArray<MInstance> inactiveInstances = inactiveInstancesByHost.objectForKey(host);
if (inactiveInstances != null && inactiveInstances.count() >= i) {
MInstance instance = inactiveInstances.objectAtIndex(i);
log("Starting inactive instance " + instance.displayName() + " on host " + host.addressAsString());
startingInstances.addObject(instance);
} else {
log("Not enough inactive instances on host: " + host.addressAsString());
}
}
}
for (MInstance instance : startingInstances) {
if (useScheduling) {
instance.setSchedulingEnabled(Boolean.TRUE);
}
instance.setAutoRecover(Boolean.TRUE);
}
handler().sendUpdateInstancesToWotaskds(startingInstances, activeHosts.allObjects());
handler().sendStartInstancesToWotaskds(startingInstances, activeHosts.allObjects());
boolean waiting = true;
// wait until apps have started
while (waiting) {
handler().startReading();
try {
log("Checking for started instances");
handler().getInstanceStatusForHosts(activeHosts.allObjects());
boolean allStarted = true;
for (MInstance instance : startingInstances) {
allStarted &= instance.isRunning_M();
}
if (allStarted) {
waiting = false;
} else {
sleep(10 * 1000);
}
} finally {
handler().endReading();
}
}
log("Started instances sucessfully");
// turn scheduling off
for (MHost host : activeHosts) {
NSArray<MInstance> currentInstances = activeInstancesByHost.objectForKey(host);
for (MInstance instance : currentInstances) {
if (useScheduling) {
instance.setSchedulingEnabled(Boolean.FALSE);
}
instance.setAutoRecover(Boolean.FALSE);
}
}
handler().sendUpdateInstancesToWotaskds(runningInstances, activeHosts.allObjects());
// then start to refuse new sessions
for (MHost host : activeHosts) {
NSArray<MInstance> currentInstances = activeInstancesByHost.objectForKey(host);
for (MInstance instance : currentInstances) {
instance.setRefusingNewSessions(true);
}
}
handler().sendRefuseSessionToWotaskds(runningInstances, activeHosts.allObjects(), true);
log("Refused new sessions: " + runningInstances);
// turn scheduling on again, but only
NSMutableArray<MInstance> restarting = new NSMutableArray<MInstance>();
for (MHost host : activeHosts) {
NSArray<MInstance> currentInstances = activeInstancesByHost.objectForKey(host);
for (int i = 0; i < currentInstances.count() - numToStartPerHost; i++) {
MInstance instance = currentInstances.objectAtIndex(i);
if (useScheduling) {
instance.setSchedulingEnabled(Boolean.TRUE);
}
instance.setAutoRecover(Boolean.TRUE);
restarting.addObject(instance);
}
}
handler().sendUpdateInstancesToWotaskds(restarting, activeHosts.allObjects());
log("Started scheduling again: " + restarting);