package com.cadrlife.devsearch.agent.action;
import com.cadrlife.devsearch.agent.Agent;
import com.cadrlife.devsearch.domain.Project;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
public class UpdateIndexAction extends AgentAction {
public UpdateIndexAction() {
super();
configFileOption.required();
checkoutPathOption.required();
repoNameOption.required();
}
@Override
public String getName() {
return "update-index";
}
@Override
public void localExecute(Agent agent) throws Exception {
agent.checkElasticsearchClient();
if (isClean()) {
agent.clean(updateScope);
}
UpdateIndexProjectHandler projectHandler = new UpdateIndexProjectHandler(agent);
Iterable<ListenableFuture<Project>> indexedFutures = transformAll(agent.pullFromSourceAsync(updateScope), projectHandler);
LOG.info("Everything started. Awaiting termination of jobs.");
Futures.allAsList(indexedFutures).get();
LOG.info("OK Done");
}
private <A,B> Iterable<ListenableFuture<B>> transformAll(Iterable<ListenableFuture<A>> futures, final Function<A,B> function) {
return Iterables.transform(futures, new Function<ListenableFuture<A>, ListenableFuture<B>>() {
@Override
public ListenableFuture<B> apply(ListenableFuture<A> future) {
return Futures.transform(future, function);
}
});
}
private class UpdateIndexProjectHandler implements Function<Project, Project> {
private Agent agent;
public UpdateIndexProjectHandler(Agent agent) {
this.agent = agent;
}
@Override
public Project apply(Project project) {
agent.indexFromLocal(project);
return project;
}
}
}