impl a simple dead socket handler

This commit is contained in:
Lee 2024-01-16 15:29:59 +00:00
commit 7749a570d6
4 changed files with 25 additions and 6 deletions

@ -4,12 +4,12 @@ import { parseTimeToMilliseconds } from "../../utils/timeUtils";
// Query to get the player count history for tge last 24 hours in 1 hour intervals
const getPlayerHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|> range(start: -{})
|> filter(fn: (r) => r["_measurement"] == "scoresaber")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["type"] == "player_count")
|> aggregateWindow(every: {}, fn: mean)
|> yield()
|> range(start: -{})
|> filter(fn: (r) => r["_measurement"] == "scoresaber")
|> filter(fn: (r) => r["type"] == "score")
|> filter(fn: (r) => r["_field"] == "player_id")
|> unique()
|> aggregateWindow(every: {}, fn: count)
`;
const getScoreCountHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")

@ -11,6 +11,9 @@ let totalUpdated: number | undefined;
let totalCreated: number | undefined;
let totalDeleted: number | undefined;
// Thee last time a map was heard
let lastMap: number = Date.now();
async function update() {
if (totalCreated == undefined) {
throw new Error("Total created is undefined");
@ -125,3 +128,9 @@ async function connectWebsocket() {
connectWebsocket();
cron.schedule("*/5 * * * *", update);
cron.schedule("*/1 * * * *", () => {
if (lastMap && Date.now() - lastMap > 10000) {
console.log("Beatsaver websocket is dead, reconnecting");
connectWebsocket();
}
});

@ -10,6 +10,9 @@ import { normalizedRegionName } from "../utils/regionUtils";
let totalScores: number | undefined;
// Thee last time a score was heard
let lastScore: number = Date.now();
/**
* Updates the player count and adds data to influx
*/
@ -62,6 +65,7 @@ async function connectWebsocket() {
};
socket.onmessage = async (message) => {
lastScore = Date.now();
const data = message.data;
let json;
try {
@ -152,3 +156,9 @@ async function connectWebsocket() {
update();
connectWebsocket();
cron.schedule("*/1 * * * *", update);
cron.schedule("*/1 * * * *", () => {
if (lastScore && Date.now() - lastScore > 10000) {
console.log("Scoresaber websocket is dead, reconnecting");
connectWebsocket();
}
});