(finally) implement redis for info updates, will have other uses in the future.

This commit is contained in:
Holly Stubbs 2022-04-20 07:29:21 +01:00
parent 6a57e5cfe8
commit 3b81210c46
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
4 changed files with 2194 additions and 1832 deletions

View file

@ -3,6 +3,11 @@
"compression": true,
"prometheusEnabled": false,
"prometheusPort": 1234,
"redisEnabled": false,
"redisAddress": "127.0.0.1",
"redisPort": 6379,
"redisDatabase": 0,
"redisPassword": "",
"databaseAddress": "127.0.0.1",
"databasePort": 3306,
"databaseUsername": "username",

3984
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -15,6 +15,7 @@
"mysql": "^2.18.1",
"osu-packet": "^4.1.2",
"prom-client": "^13.2.0",
"redis": "^4.0.6",
"request": "^2.88.2",
"uuid": "^8.3.2"
}

View file

@ -6,6 +6,7 @@ const osu = require("osu-packet"),
parseUserData = require("./util/parseUserData.js"),
User = require("./User.js"),
getUserFromToken = require("./util/getUserByToken.js"),
getUserById = require("./util/getUserById.js"),
bakedResponses = require("./bakedResponses.js"),
Streams = require("./Streams.js"),
DatabaseHelperClass = require("./DatabaseHelper.js"),
@ -23,9 +24,38 @@ global.botUser.location[1] = -32;
global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseUsername, config.databasePassword, config.databaseName);
// Start a loop that gets new data for users from the database for use on the user panel
// TODO: Some way of informing bancho that a user has set a score so details can be pulled down quickly
// Possible solution, TCP socket between the score submit server and bancho? redis? (score submit is on a different server, redis probably wouldn't work)
async function subscribeToChannel(channelName = "", callback = function(message) {}) {
// Dup and connect new client for channel subscription (required)
const scoreSubmitUpdateClient = global.promClient.duplicate();
await scoreSubmitUpdateClient.connect();
// Subscribe to channel
await scoreSubmitUpdateClient.subscribe(channelName, callback);
consoleHelper.printBancho(`Subscribed to ${channelName} channel`);
}
// Do redis if it's enabled
if (config.redisEnabled) {
(async () => {
const { createClient } = require("redis");
global.promClient = createClient({
url: `redis://${config.redisPassword.replaceAll(" ", "") == "" ? "" : `${config.redisPassword}@`}${config.redisAddress}:${config.redisPort}/${config.redisDatabase}`
});
global.promClient.on('error', e => consoleHelper.printBancho(e));
const connectionStartTime = Date.now();
await global.promClient.connect();
consoleHelper.printBancho(`Connected to redis server. Took ${Date.now() - connectionStartTime}ms`);
// Score submit update channel
subscribeToChannel("binato:update_user_stats", (message) => {
// Update user info
getUserById(parseInt(message)).updateUserInfo(true);
});
})();
} else consoleHelper.printWarn("Redis is disabled!");
// User timeout interval
setInterval(() => {
for (let User of global.users.getIterableItems()) {
if (User.id == 3) continue; // Ignore the bot