Skip to content

Commit 48f3468

Browse files
committed
feat: delete downloaded images files automatically after a minute and minor changes in mem monitoring
1 parent 0ca8f1f commit 48f3468

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ PROJECT_CANIS_ALIAS=Canis
33
# automatically reboot canis, if a potential memory leak has been detected,
44
# or the memory usage was just way too high
55
PROJECT_AUTO_RESTART=false
6-
PROJECT_MAX_MEMORY=1024 # 1GB
6+
PROJECT_THRESHOLD_MEMORY=1024
7+
PROJECT_MAX_MEMORY=2048 # 2GB
78

89
# whether to enable debug mode
910
# in debug mode, additional logging is enabled

src/components/utils/download.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path";
33
import crypto from "crypto";
44
import { MessageMedia } from "whatsapp-web.js";
55
import axios from "../axios";
6+
import log from "./log";
67

78
export async function download(
89
url: string,
@@ -20,6 +21,20 @@ export async function download(
2021

2122
try {
2223
await fs.writeFile(tempPath, response.data, "base64");
24+
25+
// Schedule deletion after 1 minute
26+
setTimeout(async () => {
27+
try {
28+
await fs.unlink(tempPath);
29+
log.info("Download", `Temporary file deleted: ${tempPath}`);
30+
} catch (err) {
31+
log.error(
32+
"Download",
33+
`Failed to delete temp file: ${(err as Error).message}`,
34+
);
35+
}
36+
}, 60 * 1000);
37+
2338
return MessageMedia.fromFilePath(tempPath);
2439
} catch (err) {
2540
throw new Error(

src/components/utils/memMonitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class MemoryMonitor {
2525

2626
constructor({
2727
interval = 60000,
28-
thresholdMB = 500,
28+
thresholdMB = 1024,
2929
}: MemoryMonitorOptions = {}) {
3030
this.interval = interval;
3131
this.thresholdMB = thresholdMB;

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import "./components/process";
1111
import "./components/server";
1212
import MemoryMonitor from "./components/utils/memMonitor";
1313

14-
const monitor = new MemoryMonitor({ interval: 30000 });
14+
const monitor = new MemoryMonitor({
15+
interval: 60000,
16+
thresholdMB: parseInt(
17+
process.env.PROJECT_THRESHOLD_MEMORY || "1024",
18+
10
19+
)
20+
});
21+
1522
monitor.start();
1623
checkRequirements();
1724

0 commit comments

Comments
 (0)