Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.takari.orchestra.plugins.nexus.perf.AgentTask Maven / Gradle / Ivy
package io.takari.orchestra.plugins.nexus.perf;
import io.takari.bpm.api.BpmnError;
import io.takari.orchestra.common.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
import java.net.InetAddress;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Named
public class AgentTask implements Task {
private static final Logger log = LoggerFactory.getLogger(AgentTask.class);
private static final int DEFAULT_AGENTS_COUNT = 1;
private final AgentPoolHolder poolHolder;
@Inject
public AgentTask(AgentPoolHolder poolHolder) {
this.poolHolder = poolHolder;
}
@Override
public String getKey() {
return "perf";
}
public void connect(String txId, String[] urls) {
poolHolder.connect(txId, urls);
log.info("connect ['{}', {}] -> done", txId, urls);
}
public void loadAndStart(String txId, String scenarioDirTemplate, Collection> scenarios, String nexusUrl, Collection memberUrls, String nexusUsername, String nexusPassword) {
try {
AgentPoolManager m = poolHolder.get(txId);
Map globals = new HashMap<>();
// TODO constants
globals.put("nexus.baseurl", nexusUrl);
if (memberUrls != null && !memberUrls.isEmpty()) {
globals.put("nexus.memberurls", String.join(",", memberUrls));
}
globals.put("nexus.username", nexusUsername);
globals.put("nexus.password", nexusPassword);
for (Map s : scenarios) {
String name = (String) s.get("name");
String scenario = String.format(scenarioDirTemplate, name);
int agents = getInt(s, "agents", DEFAULT_AGENTS_COUNT);
Map overrides = new HashMap<>(globals);
String duration = (String) s.get("duration");
if (duration != null) {
overrides.put("test.duration", duration);
}
m.load(scenario, agents, overrides);
m.start();
log.info("loadAndStart ['{}'] -> started {} agent(s) (scenario '{}', duration: '{}')", txId, agents, scenario, duration != null ? duration : "default");
}
} catch (Exception e) {
throw new BpmnError("loadAndStartError", e);
}
}
public void waitToStart(String txId, long timeout) {
try {
AgentPoolManager m = poolHolder.get(txId);
m.waitToStart(timeout);
log.info("waitToStart ['{}', {}] -> done", txId, timeout);
} catch (Exception e) {
log.error("waitToStart ['{}', {}] -> error", txId, timeout);
throw new BpmnError("waitToStartError");
}
}
public void waitToFinish(String txId, long timeout) {
try {
AgentPoolManager m = poolHolder.get(txId);
m.waitToFinish(timeout);
log.info("waitToFinish ['{}', {}] -> done", txId, timeout);
} catch (Exception e) {
log.error("waitToFinish ['{}', {}] -> error", txId, timeout);
throw new BpmnError("waitToFinishError", e);
}
}
public String[] resolveHost(String host, int port) {
try {
InetAddress[] as = InetAddress.getAllByName(host);
String[] result = new String[as.length];
for (int i = 0; i < as.length; i++) {
result[i] = as[i].getHostAddress() + ":" + port;
}
return result;
} catch (Exception e) {
log.error("resolveHost ['{}', {}] -> error", host, port, e);
throw new BpmnError("resolveHostError", e);
}
}
public void release(String txId) {
try {
AgentPoolManager m = poolHolder.get(txId);
if (m != null) {
m.release();
}
log.info("release ['{}'] -> done", txId);
} catch (Exception e) {
log.error("release ['{}'] -> error", txId, e);
throw new BpmnError("releaseError", e);
}
}
public boolean hasFailures(String txId) {
try {
AgentPoolManager m = poolHolder.get(txId);
boolean result = m.hasFailures();
log.info("hasFailures ['{}'] -> {}", txId, result);
return result;
} catch (Exception e) {
log.error("hasFailures ['{}'] -> error", e);
throw new BpmnError("hasFailuresError", e);
}
}
private static int getInt(Map m, String k, int defaultValue) {
Integer i = (Integer) m.get(k);
return i != null ? i : defaultValue;
}
}