thinknagent 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/agent.js +50 -53
- package/package.json +1 -1
package/lib/agent.js
CHANGED
|
@@ -38,65 +38,62 @@ class Agent {
|
|
|
38
38
|
this._active = false;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
console.log(`[agent] Connecting to ${cfg.serverUrl}...`);
|
|
49
|
-
this.conn.connect();
|
|
41
|
+
start() {
|
|
42
|
+
const cfg = store.read();
|
|
43
|
+
if (!cfg.serverUrl) {
|
|
44
|
+
console.error('[agent] Not initialized.');
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
console.log(`[agent] Connecting to ${cfg.serverUrl}...`);
|
|
49
|
+
|
|
50
|
+
// ✅ Pehle connect karo
|
|
51
|
+
this.conn.connect();
|
|
52
|
+
|
|
53
|
+
// ✅ Socket ready hone ke baad events bind karo
|
|
54
|
+
// conn.socket ab available hai kyunki connect() sync mein socket banata hai
|
|
55
|
+
this.conn.socket.on('agent:rules_updated', ({ rules }) => {
|
|
56
|
+
store.set('alerts', rules);
|
|
57
|
+
this.alerts.reloadRules(rules);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
this.conn.socket.on('agent:send_metrics_now', () => {
|
|
61
|
+
console.log('[agent] Metrics poll requested');
|
|
62
|
+
this.metrics.pollNow();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
this.conn.socket.on('agent:logs_updated', ({ logs }) => {
|
|
66
|
+
store.set('logs', logs);
|
|
67
|
+
this.logs.stop();
|
|
68
|
+
this.logs = new LogWatcher({ connection: this.conn, logPaths: logs });
|
|
69
|
+
if (this._active) this.logs.start(); // ✅ sirf tab start karo agar active ho
|
|
70
|
+
});
|
|
53
71
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.alerts.reloadRules(rules);
|
|
58
|
-
});
|
|
72
|
+
process.on('SIGTERM', () => this._shutdown('SIGTERM'));
|
|
73
|
+
process.on('SIGINT', () => this._shutdown('SIGINT'));
|
|
74
|
+
}
|
|
59
75
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
});
|
|
76
|
+
_onReady(role, roomId) {
|
|
77
|
+
if (this._active) {
|
|
78
|
+
// Reconnect case — metrics/logs already chal rahe hain
|
|
79
|
+
console.log(`[agent] Reconnected. Role: ${role}`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
67
82
|
|
|
68
|
-
this.
|
|
69
|
-
console.log(
|
|
70
|
-
this.metrics.pollNow();
|
|
71
|
-
});
|
|
83
|
+
this._active = true;
|
|
84
|
+
console.log(`[agent] Active! Role: ${role} | Room: ${roomId}`);
|
|
72
85
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
86
|
+
// ✅ Metrics emit ko alert engine se connect karo
|
|
87
|
+
const origEmit = this.conn.emit.bind(this.conn);
|
|
88
|
+
this.conn.emit = (event, data) => {
|
|
89
|
+
if (event === 'agent:metrics') this.alerts.evaluate(data);
|
|
90
|
+
origEmit(event, data);
|
|
91
|
+
};
|
|
77
92
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.metrics.start();
|
|
84
|
-
this.logs.start();
|
|
85
|
-
this.shell.start();
|
|
86
|
-
|
|
87
|
-
// Patch metrics poller to also feed alert engine
|
|
88
|
-
const origEmit = this.conn.emit.bind(this.conn);
|
|
89
|
-
this.conn.emit = (event, data) => {
|
|
90
|
-
if (event === 'agent:metrics') this.alerts.evaluate(data);
|
|
91
|
-
origEmit(event, data);
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
this.conn.emit('agent:ready', {
|
|
95
|
-
hostname: require('os').hostname(),
|
|
96
|
-
platform: process.platform,
|
|
97
|
-
nodeVersion: process.version,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
93
|
+
this.metrics.start();
|
|
94
|
+
this.logs.start();
|
|
95
|
+
this.shell.start();
|
|
96
|
+
}
|
|
100
97
|
|
|
101
98
|
|
|
102
99
|
_onDisconnect(reason) {
|