@@ -66,15 +66,29 @@ export const SESSION_LOCK_CONSTANTS = {
66
66
* @returns true if any instance is currently active
67
67
*/
68
68
export function checkSessionLockFree ( ) : boolean {
69
+ const prefixedLogger = logger . getChild ( `checkSessionLockFree` ) ;
70
+
69
71
const lastPingTime = window . localStorage . getItem ( SESSION_LOCK_CONSTANTS . STORAGE_ITEM_PING ) ;
70
72
if ( lastPingTime === null ) {
71
73
// no other holder
74
+ prefixedLogger . info ( "No other session has the lock" ) ;
72
75
return true ;
73
76
}
74
77
78
+ const lockHolder = window . localStorage . getItem ( SESSION_LOCK_CONSTANTS . STORAGE_ITEM_OWNER ) ;
79
+
75
80
// see if it has expired
76
81
const timeAgo = Date . now ( ) - parseInt ( lastPingTime ) ;
77
- return timeAgo > SESSION_LOCK_CONSTANTS . LOCK_EXPIRY_TIME_MS ;
82
+
83
+ const remaining = SESSION_LOCK_CONSTANTS . LOCK_EXPIRY_TIME_MS - timeAgo ;
84
+ if ( remaining <= 0 ) {
85
+ // another session claimed the lock, but it is stale.
86
+ prefixedLogger . info ( `Last ping (from ${ lockHolder } ) was ${ timeAgo } ms ago: lock is free` ) ;
87
+ return true ;
88
+ }
89
+
90
+ prefixedLogger . info ( `Last ping (from ${ lockHolder } ) was ${ timeAgo } ms ago: lock is taken` ) ;
91
+ return false ;
78
92
}
79
93
80
94
/**
@@ -95,7 +109,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
95
109
/** unique ID for this session */
96
110
const sessionIdentifier = uuidv4 ( ) ;
97
111
98
- const prefixedLogger = logger . withPrefix ( `getSessionLock[${ sessionIdentifier } ]` ) ;
112
+ const prefixedLogger = logger . getChild ( `getSessionLock[${ sessionIdentifier } ]` ) ;
99
113
100
114
/** The ID of our regular task to service the lock.
101
115
*
@@ -133,7 +147,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
133
147
return 0 ;
134
148
}
135
149
136
- prefixedLogger . info ( `Last ping (from ${ lockHolder } ) was ${ timeAgo } ms ago, waiting` ) ;
150
+ prefixedLogger . info ( `Last ping (from ${ lockHolder } ) was ${ timeAgo } ms ago, waiting ${ remaining } ms ` ) ;
137
151
return remaining ;
138
152
}
139
153
0 commit comments