-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentServerManager.java
60 lines (49 loc) · 1.64 KB
/
AgentServerManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.benchmarker.bmcontroller.agent;
import java.util.HashMap;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.benchmarker.bmagent.AgentInfo;
import org.benchmarker.bmagent.AgentStatus;
import org.springframework.stereotype.Component;
@Component
@Getter
@Slf4j
public class AgentServerManager {
// store current status of all agents
private final ConcurrentHashMap<String, AgentInfo> agentsUrl = new ConcurrentHashMap<>();
// key: running templateId, value: relate agent
private final HashMap<Long, String> agentMapped = new HashMap<>();
public void add(String url, AgentInfo agentInfo) {
agentsUrl.put(url, agentInfo);
}
public void update(AgentInfo agentInfo) {
if (agentsUrl.get(agentInfo.getServerUrl()) == null) {
log.error("cannot find agent");
return;
}
agentsUrl.put(agentInfo.getServerUrl(), agentInfo);
}
public Optional<AgentInfo> getReadyAgent() {
for (AgentInfo agentInfo : agentsUrl.values()) {
if (agentInfo.getStatus() == AgentStatus.READY) {
return Optional.of(agentInfo);
}
}
return Optional.empty();
}
public void remove(String url) {
agentsUrl.remove(url);
}
public void addTemplateRunnerAgent(Long id, String url) {
agentMapped.put(id, url);
}
public void removeTemplateRunnerAgent(Long id) {
String url = agentMapped.get(id);
if (url != null) {
agentMapped.remove(id);
agentsUrl.remove(url);
}
}
}