-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.js
More file actions
33 lines (31 loc) · 874 Bytes
/
scheduler.js
File metadata and controls
33 lines (31 loc) · 874 Bytes
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
const File = require("./models/file");
const fs = require("fs");
const connectDB = require("./config/db");
connectDB();
async function fetchData() {
// Fetch all the 24 hours old files from database and then delete them one by one
const pastDate = new Date(Date.now() - 1000 * 60 * 60 * 24);
const files = File.find({
createdAt: { $lt: pastDate },
});
if (!files) {
console.log("No files found.");
}
if (files.length) {
for (const file of files) {
try {
fs.unlinkSync(file.path);
console.log("file deleted");
await file.remove();
console.log("deleted from database as well");
} catch (err) {
console.log("Error" + err);
}
}
}
console.log("out of for loop and end of function");
}
fetchData().then(()=>{
console.log("Scheduler finished successfully.");
process.exit();
});