refactor: harden session store updates
Co-authored-by: Tyler Yust <tyler6204@users.noreply.github.com>
This commit is contained in:
@@ -11,7 +11,7 @@ import {
|
||||
loadSessionStore,
|
||||
resolveSessionKey,
|
||||
resolveStorePath,
|
||||
saveSessionStore,
|
||||
updateSessionStore,
|
||||
} from "../../config/sessions.js";
|
||||
import { emitHeartbeatEvent } from "../../infra/heartbeat-events.js";
|
||||
import { getChildLogger } from "../../logging.js";
|
||||
@@ -72,7 +72,14 @@ export async function runWebHeartbeatOnce(opts: {
|
||||
sessionId,
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
await saveSessionStore(storePath, store);
|
||||
await updateSessionStore(storePath, (nextStore) => {
|
||||
const nextCurrent = nextStore[sessionKey] ?? current;
|
||||
nextStore[sessionKey] = {
|
||||
...nextCurrent,
|
||||
sessionId,
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
});
|
||||
}
|
||||
const sessionSnapshot = getSessionSnapshot(cfg, to, true);
|
||||
if (verbose) {
|
||||
@@ -163,7 +170,14 @@ export async function runWebHeartbeatOnce(opts: {
|
||||
const store = loadSessionStore(storePath);
|
||||
if (sessionSnapshot.entry && store[sessionSnapshot.key]) {
|
||||
store[sessionSnapshot.key].updatedAt = sessionSnapshot.entry.updatedAt;
|
||||
await saveSessionStore(storePath, store);
|
||||
await updateSessionStore(storePath, (nextStore) => {
|
||||
const nextEntry = nextStore[sessionSnapshot.key];
|
||||
if (!nextEntry) return;
|
||||
nextStore[sessionSnapshot.key] = {
|
||||
...nextEntry,
|
||||
updatedAt: sessionSnapshot.entry.updatedAt,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
heartbeatLogger.info(
|
||||
|
||||
Reference in New Issue
Block a user