Skip to content

Instantly share code, notes, and snippets.

@khaosans
Last active February 10, 2016 21:37
Show Gist options
  • Save khaosans/1ea05d577dc187eb6a08 to your computer and use it in GitHub Desktop.
Save khaosans/1ea05d577dc187eb6a08 to your computer and use it in GitHub Desktop.
yo
public void indexItemsForOpenTenants() {
Integer numberOfTries = 0;
List<String> itemsToIndex = TenantManager.getTenants().stream().filter(isOpenTenant).collect(Collectors.toList());
try {
while (!itemsToIndex.isEmpty()) {
if (abort.get() || numberOfTries >= counterThreshold) { // break if abort is true or counter is more than 10 hours
logger.warn(String.format("Aborting indexAllTenants: abort: %s, counter: %s", abort.get(), numberOfTries));
break;
}
itemsToIndex.forEach(tenantId -> tenantService.unscheduleAllTenantJobs(tenantId));
List<String> listOfTenantsWithOutRunningJobs = itemsToIndex.stream()
.filter(noRunningJobs).collect(Collectors.toList());
itemsToIndex = itemsToIndex.stream()
.filter(tenantId -> !listOfTenantsWithOutRunningJobs.contains(tenantId)).collect(Collectors.toList());
listOfTenantsWithOutRunningJobs
.forEach(tenantId -> CompletableFuture.runAsync(() -> {
tenantService.setOpen(tenantId, false);
tenantService.reindexTenant(tenantId);
tenantService.setOpen(tenantId, true);
}));
if (!itemsToIndex.isEmpty()) {
logger.warn(itemsToIndex.toString() +"Still has running jobs");
Thread.sleep(delay);
numberOfTries++;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public Predicate<String> isOpenTenant = s -> {
TenantManager.setCurrentTenant(s);
return JamaGlobalContext.getTenant().isOpen();
};
public Predicate<String> noRunningJobs = s -> tenantService.getRunningJobsForTenant(s).isEmpty();
public void setCounterThreshold(Integer counterThreshold) {
this.counterThreshold = counterThreshold;
}
public void setDelay(Long delay) {
this.delay = delay;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment