Skip to content

Commit d3c8866

Browse files
authored
Merge pull request #18 from bcgov/fix/filehandleLeak
feat: improve memory usage and fix filehandle leak
2 parents c020b34 + 39e11c7 commit d3c8866

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARG REPO_LOCATION=
2-
FROM ${REPO_LOCATION}node:20 as builder
2+
FROM ${REPO_LOCATION}node:20-alpine as builder
33

44
### --------------------------------- Build
55
# Install packages and build
@@ -10,7 +10,7 @@ RUN npm ci --no-audit && \
1010
npm ci --omit=dev --no-audit
1111

1212
# Deployment container
13-
FROM ${REPO_LOCATION}node:20
13+
FROM ${REPO_LOCATION}node:20-alpine
1414

1515
# LABEL org.opencontainers.image.description="NR Broker handles the business logic of authenticating and validating requests for automated processes to access secrets"
1616
# LABEL org.opencontainers.image.licenses=Apache-2.0
@@ -21,4 +21,4 @@ COPY --from=builder /app/node_modules ./node_modules
2121
COPY --from=builder /app/dist ./dist
2222

2323
# Start up command
24-
ENTRYPOINT ["node", "dist/index"]
24+
ENTRYPOINT ["node", "--expose-gc", "dist/index"]

src/cron/janitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export async function syncLogsDb(db: DatabaseService) {
1919
);
2020
for (const row of result.rows) {
2121
try {
22-
fs.openSync(`${row.path}`, 'r');
22+
const filehandle = fs.openSync(`${row.path}`, 'r');
23+
fs.closeSync(filehandle);
2324
} catch (err) {
2425
console.log(
2526
`janitor: delete database row ${row.id}; file missing: ${row.path}`,

src/cron/rotate.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,27 @@ export async function rotateLogs(db: DatabaseService) {
3434
await rotateLog(db, file);
3535
}
3636
if (LOGROTATE_POSTROTATE_COMMAND) {
37-
console.log('rotate: run post-rotate');
37+
console.log('rotate: post-rotate command [start]');
3838
await new Promise<void>((resolve, reject) => {
3939
exec(LOGROTATE_POSTROTATE_COMMAND, (error, stdout, stderr) => {
4040
if (error) {
4141
// node couldn't run the command
42+
console.log('rotate: command error');
4243
reject(error);
4344
return;
4445
}
4546
// Using process.stdout.write to prevent double new lines.
4647
if (stdout) {
47-
process.stdout.write(`stdout: ${stdout}`);
48+
process.stdout.write(`rotate: [stdout] ${stdout}`);
4849
}
4950
if (stderr) {
50-
process.stdout.write(`stderr: ${stderr}`);
51+
process.stdout.write(`rotate: [stderr] ${stderr}`);
5152
}
5253
resolve();
5354
});
5455
});
5556
}
57+
console.log('rotate: post-rotate command [end]');
5658
} else {
5759
console.log('rotate: no files to rotate');
5860
}

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,40 @@ import { compress } from './cron/compress';
1313

1414
console.log('Starting...');
1515

16+
function runGarbageCollection() {
17+
if (global.gc) {
18+
global.gc();
19+
}
20+
}
21+
1622
async function main() {
1723
const db = await DatabaseService.create();
1824

25+
if (!global.gc) {
26+
console.log(
27+
'Garbage collection unavailable. Pass --expose-gc ' +
28+
'when launching node to enable forced garbage collection.',
29+
);
30+
}
31+
1932
const rotateJob = Cron(CRON_ROTATE, async () => {
2033
await rotateLogs(db);
34+
runGarbageCollection();
2135
});
2236
const compressJob = Cron(CRON_COMPRESS, async () => {
2337
await syncLogsDb(db);
2438
await compress(db);
39+
runGarbageCollection();
2540
});
2641
const backupJob = Cron(CRON_BACKUP, async () => {
2742
await syncLogsDb(db);
2843
await backup(db);
44+
runGarbageCollection();
2945
});
3046
const janitorJob = Cron(CRON_JANITOR, async () => {
3147
await syncLogsDb(db);
3248
await removeOldLogs(db);
49+
runGarbageCollection();
3350
});
3451
console.log(`Rotate job next run: ${rotateJob.nextRun()}`);
3552
console.log(`Compress job next run: ${compressJob.nextRun()}`);

0 commit comments

Comments
 (0)