forked from stackimpact/stackimpact-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigLoader.java
More file actions
84 lines (67 loc) · 2.16 KB
/
ConfigLoader.java
File metadata and controls
84 lines (67 loc) · 2.16 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.stackimpact.agent;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
public class ConfigLoader {
public final static long LOAD_DELAY = 2 * 1000;
public final static long LOAD_INTERVAL = 120 * 1000;
private Agent agent;
private Timer loadTimer;
private long lastLoadTS;
public ConfigLoader(Agent agent) {
this.agent = agent;
}
public void start() {
if (!agent.isAutoProfilingMode()) {
return;
}
loadTimer = new Timer();
loadTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
load();
}
catch(Exception ex) {
agent.logException(ex);
}
}
}, LOAD_DELAY, LOAD_INTERVAL);
}
public void stop() {
if (loadTimer != null) {
loadTimer.cancel();
}
}
public void load() throws Exception {
long now = AgentUtils.millis();
if (!agent.isAutoProfilingMode() && lastLoadTS > now - LOAD_INTERVAL) {
return;
}
lastLoadTS = now;
Object response = agent.getAPIRequest().post("config", new HashMap());
if (response instanceof HashMap) {
HashMap config = (HashMap)response;
agent.setAgentEnabled("yes".equals(config.get("agent_enabled")));
agent.setProfilingDisabled("yes".equals(config.get("profiling_disabled")));
}
if (agent.isAgentEnabled() && !agent.isProfilingDisabled()) {
agent.getCPUReporter().start();
agent.getLockReporter().start();
agent.logInfo("Profiling enabled.");
}
else {
agent.getCPUReporter().stop();
agent.getLockReporter().stop();
agent.logInfo("Profiling disabled.");
}
if (agent.isAgentEnabled()) {
agent.getProcessReporter().start();
agent.logInfo("Agent activated.");
}
else {
agent.getProcessReporter().stop();
agent.logInfo("Agent deactivated.");
}
}
}