Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Improve logging in SessionLock #133

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/utils/SessionLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,29 @@ export const SESSION_LOCK_CONSTANTS = {
* @returns true if any instance is currently active
*/
export function checkSessionLockFree(): boolean {
const prefixedLogger = logger.getChild(`checkSessionLockFree`);

const lastPingTime = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_PING);
if (lastPingTime === null) {
// no other holder
prefixedLogger.info("No other session has the lock");
return true;
}

const lockHolder = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_OWNER);

// see if it has expired
const timeAgo = Date.now() - parseInt(lastPingTime);
return timeAgo > SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS;

const remaining = SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS - timeAgo;
if (remaining <= 0) {
// another session claimed the lock, but it is stale.
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is free`);
return true;
}

prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is taken`);
return false;
}

/**
Expand All @@ -95,7 +109,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
/** unique ID for this session */
const sessionIdentifier = uuidv4();

const prefixedLogger = logger.withPrefix(`getSessionLock[${sessionIdentifier}]`);
const prefixedLogger = logger.getChild(`getSessionLock[${sessionIdentifier}]`);

/** The ID of our regular task to service the lock.
*
Expand Down Expand Up @@ -133,7 +147,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
return 0;
}

prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting`);
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting ${remaining}ms`);
return remaining;
}

Expand Down
Loading