forked from fippo/rtcstats-server
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathOrphanFileHelper.js
More file actions
103 lines (84 loc) · 3.27 KB
/
OrphanFileHelper.js
File metadata and controls
103 lines (84 loc) · 3.27 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const fs = require('fs');
const logger = require('./logging');
const fileStore = require('./store/file');
const utils = require('./utils/utils');
/**
*
*/
class OrphanFileHelper {
/**
*
*/
constructor({ tempPath, orphanFileCleanupTimeoutMinutes, wsHandler, cleanupCronHour }) {
this.tempPath = tempPath;
this.orphanFileCleanupTimeoutMs = orphanFileCleanupTimeoutMinutes * 60 * 1000;
this.wsHandler = wsHandler;
this.cleanupCronHour = cleanupCronHour;
this.processOldFiles = this.processOldFiles.bind(this);
}
/**
* Remove old files from the temp folder.
*/
processOldFiles() {
logger.info('[OrphanFileHelper] Waiting for connections to reconnect.');
if (fs.existsSync(this.tempPath)) {
fs.readdirSync(this.tempPath).forEach(fname => {
const filePath = utils.getDumpPath(this.tempPath, fname);
logger.debug(`[OrphanFileHelper] Trying to process file ${filePath}`);
fs.stat(filePath, (err, stats) => {
if (err) {
logger.error(`[OrphanFileHelper] File does not exist! ${filePath}`);
}
this.processIfExpired(stats, filePath, fname);
});
});
} else {
logger.error('[OrphanFileHelper] Temp path doesn\'t exists. path: ', this.tempPath);
throw new Error(`Temp path doesn't exists. tempPath: ${this.tempPath}`);
}
this.scheduleNext(this.cleanupCronHour);
}
/**
*
*/
processIfExpired(stats, filePath, fname) {
const lastModifiedDurationMs = Math.abs(Date.now() - stats.mtime.getTime());
logger.debug(`[OrphanFileHelper] File last modified ${lastModifiedDurationMs} ms ago:`);
if (lastModifiedDurationMs > this.orphanFileCleanupTimeoutMs) {
logger.debug(`[OrphanFileHelper] Start processing the file ${`${filePath}`}`);
const response = fileStore.getObjectsByKeys(
filePath, [ 'connectionInfo', 'identity' ]);
response.then(
obj => {
const jsonObj = obj;
let meta;
let connectionInfo;
if (jsonObj?.connectionInfo) {
meta = JSON.parse(jsonObj?.connectionInfo);
meta.dumpPath = `${filePath}`;
}
if (jsonObj?.identity) {
connectionInfo = jsonObj?.identity;
}
this.wsHandler.processData(fname, meta, connectionInfo);
})
.catch(e => {
logger.error(`[OrphanFileHelper] ${e}`);
logger.info(`[OrphanFileHelper] New connection. File doesn't exist. ${filePath}`);
});
}
}
/**
*
* @param {*} func
*/
scheduleNext(hour) {
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, hour, 0, 0, 0);
const wait = start.getTime() - now.getTime();
setTimeout(() => { // Wait until the specified hour
this.processOldFiles();
}, wait);
}
}
module.exports = OrphanFileHelper;