forked from Impulse87/ATLAS-RawTravelDataCleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (61 loc) · 1.9 KB
/
index.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
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
/**
* Autor: Impulse
* License: MIT
**/
const { promisify } = require('util');
const redis = require("redis");
const intervalTimer = 10 * 60 * 1000; // 10 minutes
const expirationTime = 1800; // in seconds -> 1800 = 30 minutes
// if a key expires, the next 5 min fork cycle of the redis db will clear it from your harddrive
let intervalReference;
const client = redis.createClient({
host: "127.0.0.1",
port: "6379",
password: "foobared" // here goes your password!
});
const ttl = promisify(client.ttl).bind(client);
client.on("ready", () => {
performDbCheck(); // init right away on start
intervalReference = setInterval(() => {
performDbCheck();
}, intervalTimer);
});
function performDbCheck() {
console.info("[" + new Date() + "] RedisDB check...");
client.keys("RawTravelData:*", async (err, keys) => {
if (err) {
console.error(err);
return;
}
for (let key of keys) {
const remaining = await ttl(key);
if (remaining === -1) {
console.info("has no expiration time, setting new one");
await client.expire(key, expirationTime);
console.info(key + " - add new expiration seconds: " + await ttl(key));
} else if (remaining < 30) {
console.info(key + " - is about to expire, remaining seconds: " + remaining);
}
}
});
}
client.on("error", err => {
clearInterval(intervalReference);
if (err["code"] && err["code"].indexOf("ECONNRESET") > -1) {
console.info("Lost RedisDB connection, trying to reconnect...");
} else if (err["code"] && err["code"].indexOf("ECONNREFUSED") > -1) {
console.info("Cannot connect to RedisDB, retrying...");
} else {
console.error(err);
}
});
// Respond to 'Ctrl+C'
process.on("SIGINT", () => {
clearInterval(intervalReference);
process.exit(0);
});
// Respond to shutting down or closing
process.on("SIGTERM", () => {
clearInterval(intervalReference);
process.exit(0);
});