commit d1f3b74772093aa6c467ba71d53f342be7cf7ccc Author: chris Date: Thu Feb 21 19:54:44 2019 +0100 repo init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5f19d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..1d72416 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Why is this tool required? +Since the game ATLAS does clutter its redis DB with RawTravelData entries, it can happen that it grows to several GB of size. This results in no longer working grid travel. It seems that the vanilla game does not give its redis entries an expiration time, with which the redis DB could cleanup itself. That's why this tool will simply add an expiration time to all RawTravelData entries. + +# How to use it? +## Prerequisites +1. You'll need to install [Node](https://nodejs.org/en/download/) on your machine first +2. Then download the zip file from this Repository and extract it + +## Installation +3. Go into the extracted folder, where you'll see the "package.json" file +4. Now press and hold Shift on your keyboard and press, without selecting anything inside that folder, but on an empty space your right mouse button. +5. And open the windows Powershell, like so: http://prntscr.com/moatcu (I expect that Linux users know how to open a shell at that location ^^) +6. You now need to install the required node packages, therefore type in "npm i" and hit enter, like so: http://prntscr.com/moaupk + - Note: if the initial installation went through and node constantly repeats itself, simply stop it with Ctrl + C (example: http://prntscr.com/moayfj) + +## Setup your connection +7. Open the file "index.js" and adjust this part - http://prntscr.com/mob1m3 - as you like. + +## Execution +8. As soon as Node did install required submodules and you put in your correct password, ip and port, you'll be able to start the cleaner tool with "npm start", like so: http://prntscr.com/moaz2k diff --git a/index.js b/index.js new file mode 100644 index 0000000..e1eb09e --- /dev/null +++ b/index.js @@ -0,0 +1,70 @@ +/** + * Autor: Impulse + * License: MIT + **/ + +const { promisify } = require('util'); +const redis = require("redis"); + +const intervalTimer = 600 * 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); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..8f8e0b9 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "rawtraveldatacleaner", + "version": "1.0.0", + "description": "To periodically clean the ATLAS redis db", + "main": "index.js", + "scripts": { + "install": "npm i", + "start": "node index.js" + }, + "author": "Impulse", + "license": "MIT", + "dependencies": { + "redis": "^2.8.0" + } +}