Skip to content

Commit fa8511e

Browse files
author
Kushagra Singh Bisen
committed
Refactor logging and resource tracking in index.ts and start_notification_aggregator_process.ts
1 parent 7a082d3 commit fa8511e

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as bunyan from "bunyan";
33
import * as fs from 'fs';
44
import { program } from "commander";
55
const log_file = fs.createWriteStream('./logs/info.log', { flags: 'a' });
6+
const resource_used_log_file = `./logs/resource_used.csv`;
67

78
const logger = bunyan.createLogger({
89
name: 'solid-stream-notifications-cache',
@@ -19,7 +20,27 @@ const logger = bunyan.createLogger({
1920
}
2021
}
2122
}
22-
})
23+
});
24+
25+
interface MemoryUsage {
26+
rss: number;
27+
heapTotal: number;
28+
heapUsed: number;
29+
external: number;
30+
}
31+
32+
fs.writeFileSync(resource_used_log_file, `timestamp, cpu_user, cpu_system, rss, heapTotal, heapUsed, external\n`);
33+
34+
35+
function logCpuMemoryUsage() {
36+
const cpuUsage = process.cpuUsage(); // in microseconds
37+
const memoryUsage: MemoryUsage = process.memoryUsage(); // in bytes
38+
const timestamp = Date.now();
39+
const logData = `${timestamp}, ${cpuUsage.user}, ${cpuUsage.system}, ${memoryUsage.rss}, ${memoryUsage.heapTotal}, ${memoryUsage.heapUsed}, ${memoryUsage.external}\n`;
40+
fs.appendFileSync(resource_used_log_file, logData);
41+
}
42+
43+
setInterval(logCpuMemoryUsage, 500);
2344

2445
program
2546
.version('1.0.0')
Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
1-
const { spawn } = require('child_process');
2-
import * as fs from 'fs';
1+
import { fork, ChildProcess } from 'child_process';
32

4-
const child = spawn('npm', ['run', 'start'], {
5-
stdio: 'inherit',
6-
shell: true
7-
});
3+
export function startNotificationAggregatorProcess() {
4+
const aggregator_process: ChildProcess = fork('./dist/index.js')
85

6+
aggregator_process.on('message', (message: string) => {
7+
console.log(`Message from the notification aggregator process: ${message}`);
8+
});
99

10-
function getTimestamp() {
11-
const now = new Date();
12-
return `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}-${now.getHours().toString().padStart(2, '0')}-${now.getMinutes().toString().padStart(2, '0')}-${now.getSeconds().toString().padStart(2, '0')}`;
10+
aggregator_process.on('exit', (code: number, signal: string) => {
11+
console.log(`Notification aggregator process exited with code ${code} and signal ${signal}`);
12+
});
1313
}
1414

15-
interface MemoryUsage {
16-
rss: number;
17-
heapTotal: number;
18-
heapUsed: number;
19-
external: number;
20-
}
21-
22-
function logCPUMemoryUsage(logFile: string) {
23-
const cpuUsage = process.cpuUsage();
24-
const memoryUsage: MemoryUsage = process.memoryUsage();
25-
const timestamp = Date.now();
26-
const logLine = `${timestamp},${cpuUsage.user},${cpuUsage.system},${memoryUsage.rss},${memoryUsage.heapTotal},${memoryUsage.heapUsed},${memoryUsage.external}\n`;
27-
fs.appendFileSync(logFile, logLine);
28-
}
29-
30-
const timestamp = getTimestamp();
31-
const logFile = `notifications-stream-aggregator-resource-usage-${timestamp}.csv`;
32-
fs.writeFileSync(logFile, 'timestamp,cpu_user,cpu_system,memory_rss,memory_heapTotal,memory_heapUsed,memory_external\n');
33-
34-
setInterval(() => {
35-
logCPUMemoryUsage(logFile);
36-
}, 500);
37-
38-
child.on('exit', (code: any) => {
39-
console.log(`Child process exited with code ${code}`);
40-
process.exit(code);
41-
});
42-
43-
child.on('error', (err: any) => {
44-
console.log(`Child process error: ${err}`);
45-
process.exit(1);
46-
});
15+
startNotificationAggregatorProcess();

0 commit comments

Comments
 (0)