Resolve issues from DB lock collection migration

This commit is contained in:
Zaid Humaimidi committed 2023-11-16 15:58:26 -08:00
1 parent 7a142a4c08
commit d122b224b0
3 files changed
+10 -6

No files matched your search

+3 -4
View File
@@ -39,6 +39,7 @@ import {
} from "./ui/db";
import * as common from "./util";
import * as cache from "./cache";
import { lockExists } from "./lock";
import {
getRevision,
getConfig,
@@ -211,8 +212,7 @@ export async function awaitSessionStart(
);
const li = (device["_lastInform"] as Date).getTime();
if (li > lastInform) return true;
const token = await cache.get(`cwmp_session_${deviceId}`);
if (token) return true;
if (await lockExists(`cwmp_session_${deviceId}`)) return true;
if (timeout < 500) return false;
await new Promise((resolve) => setTimeout(resolve, 500));
timeout -= Date.now() - now;
@@ -224,8 +224,7 @@ export async function awaitSessionEnd(
timeout: number
): Promise<boolean> {
const now = Date.now();
const token = await cache.get(`cwmp_session_${deviceId}`);
if (!token) return true;
if (!(await lockExists(`cwmp_session_${deviceId}`))) return true;
if (timeout < 500) return false;
await new Promise((resolve) => setTimeout(resolve, 500));
timeout -= Date.now() - now;
+2 -2
View File
@@ -67,7 +67,7 @@ export class LocalCache<T> {
return;
}
const lockToken = await acquireLock(this.cacheKey + "_lock", 5000);
const lockToken = await acquireLock(this.cacheKey, 5000);
const [hash, snapshot] = await this.callback();
@@ -84,7 +84,7 @@ export class LocalCache<T> {
if (lockToken) {
if (hash !== dbHash) await set(this.cacheKey, hash, 300);
await releaseLock(this.cacheKey + "_lock", lockToken);
await releaseLock(this.cacheKey, lockToken);
}
this.nextRefresh = now + (REFRESH - (now % REFRESH));
+5
View File
@@ -43,3 +43,8 @@ export async function releaseLock(
});
if (res.deletedCount !== 1) throw new Error("Lock expired");
}
export async function lockExists(lockName: string): Promise<boolean> {
const res = await collections.locks.findOne({ _id: lockName });
return res != null;
}