mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
fix(stats): set initial tracks played to 4814 and robust bidirectional redis sync
This commit is contained in:
1 parent
5714f7b2c8
commit
70f124294d
2 files changed
+22
-4
No files matched your search
@@ -39,7 +39,14 @@ class TrackStatsManager {
|
||||
|
||||
setRedis(redisHandler) {
|
||||
this._redis = redisHandler;
|
||||
const client = this._redis?.client;
|
||||
if (client) {
|
||||
if (client.isOpen) {
|
||||
this._syncRedis();
|
||||
} else {
|
||||
client.on("ready", () => this._syncRedis());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async _syncRedis() {
|
||||
@@ -47,14 +54,20 @@ class TrackStatsManager {
|
||||
if (!client || !client.isOpen) return;
|
||||
|
||||
try {
|
||||
await client.set(REDIS_KEY, String(this._tracksPlayed), { NX: true });
|
||||
const val = await client.get(REDIS_KEY);
|
||||
if (val != null) {
|
||||
const parsed = parseInt(val, 10);
|
||||
if (Number.isFinite(parsed)) {
|
||||
this._tracksPlayed = Math.max(this._tracksPlayed, parsed);
|
||||
if (parsed > this._tracksPlayed) {
|
||||
this._tracksPlayed = parsed;
|
||||
this.save();
|
||||
} else if (this._tracksPlayed > parsed) {
|
||||
await client.set(REDIS_KEY, String(this._tracksPlayed));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await client.set(REDIS_KEY, String(this._tracksPlayed));
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn("[TrackStats] Redis sync error:", e?.message);
|
||||
}
|
||||
@@ -103,9 +116,14 @@ class TrackStatsManager {
|
||||
if (val != null) {
|
||||
const parsed = parseInt(val, 10);
|
||||
if (Number.isFinite(parsed)) {
|
||||
this._tracksPlayed = Math.max(this._tracksPlayed, parsed);
|
||||
if (parsed > this._tracksPlayed) {
|
||||
this._tracksPlayed = parsed;
|
||||
this._scheduleSave();
|
||||
}
|
||||
return this._tracksPlayed;
|
||||
}
|
||||
} else {
|
||||
await client.set(REDIS_KEY, String(this._tracksPlayed));
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"playlists": 30,
|
||||
"songs": 600
|
||||
"songs": 4814
|
||||
}
|
||||
Reference in new issue
Block a user