-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteKeysJob.js
34 lines (28 loc) · 1.03 KB
/
deleteKeysJob.js
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
34
const schedule = require('node-schedule');
const fs = require("fs");
async function deleteKeysJob(TimeToDeleteFile, server_number) {
console.log("time to delete files = " + new Date(TimeToDeleteFile));
try {
if (isNaN(new Date(TimeToDeleteFile))) {
throw new Error("Invalid date");
}
const public_path = `keys/${server_number}/public_key.pem`;
const private_path = `keys/${server_number}/private_key.pem`;
const publicKeyExists = fs.existsSync(public_path);
const privateKeysExists = fs.existsSync(private_path);
if (!publicKeyExists || !privateKeysExists) {
throw new Error("keys doesn't exist");
}
schedule.scheduleJob(new Date(TimeToDeleteFile), function () {
try {
fs.unlinkSync(public_path);
fs.unlinkSync(private_path);
} catch (e) {
console.log(e);
}
});
} catch (e) {
console.log(e);
}
}
module.exports = { deleteKeysJob }