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

@ -11,6 +11,9 @@ let totalUpdated: number | undefined;
let totalCreated: number | undefined; let totalCreated: number | undefined;
let totalDeleted: number | undefined; let totalDeleted: number | undefined;
// Thee last time a map was heard
let lastMap: number = Date.now();
async function update() { async function update() {
if (totalCreated == undefined) { if (totalCreated == undefined) {
throw new Error("Total created is undefined"); throw new Error("Total created is undefined");
@ -125,3 +128,9 @@ async function connectWebsocket() {
connectWebsocket(); connectWebsocket();
cron.schedule("*/5 * * * *", update); 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; 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 * Updates the player count and adds data to influx
*/ */
@ -62,6 +65,7 @@ async function connectWebsocket() {
}; };
socket.onmessage = async (message) => { socket.onmessage = async (message) => {
lastScore = Date.now();
const data = message.data; const data = message.data;
let json; let json;
try { try {
@ -152,3 +156,9 @@ async function connectWebsocket() {
update(); update();
connectWebsocket(); connectWebsocket();
cron.schedule("*/1 * * * *", update); cron.schedule("*/1 * * * *", update);
cron.schedule("*/1 * * * *", () => {
if (lastScore && Date.now() - lastScore > 10000) {
console.log("Scoresaber websocket is dead, reconnecting");
connectWebsocket();
}
});