Refactor LockManager to improve key handling and event broadcasting

- Updated constructor to use socketClient instead of socketManager for better clarity.
- Changed key management methods to use a consistent naming convention for setting and deleting locks.
- Enhanced event listener setup to utilize socketClient for broadcasting lock updates.
- Improved logging for lock and unlock operations to provide clearer insights into the locking mechanism.
This commit is contained in:
Tom Butcher 2025-08-18 01:08:49 +01:00
parent f5bfbe0d63
commit d695772a3a

View File

@ -11,8 +11,8 @@ logger.level = config.server.logLevel;
* LockManager handles distributed locking using Etcd and broadcasts lock events via websockets. * LockManager handles distributed locking using Etcd and broadcasts lock events via websockets.
*/ */
export class LockManager { export class LockManager {
constructor(socketManager) { constructor(socketClient) {
this.socketManager = socketManager; this.socketClient = socketClient;
this.setupLocksListeners(); this.setupLocksListeners();
} }
@ -20,7 +20,10 @@ export class LockManager {
// Add a 'lock' event to the 'locks' stream // Add a 'lock' event to the 'locks' stream
logger.debug('Locking object:', object._id); logger.debug('Locking object:', object._id);
try { try {
await etcdServer.set(`/locks/${object.type}s/${object._id}`, object); await etcdServer.setKey(`/${object.type}s/${object._id}/lock`, {
...object,
locked: true
});
logger.info(`Lock event to id: ${object._id}`); logger.info(`Lock event to id: ${object._id}`);
return true; return true;
} catch (err) { } catch (err) {
@ -31,16 +34,15 @@ export class LockManager {
async unlockObject(object) { async unlockObject(object) {
// Add an 'unlock' event to the 'locks' stream // Add an 'unlock' event to the 'locks' stream
const key = `/locks/${object.type}s/${object._id}`; const key = `/${object.type}s/${object._id}/lock`;
console.log('unlocking');
try { try {
logger.debug('Checking user can unlock:', object._id); logger.debug('Checking user can unlock:', object._id);
const lockEvent = await etcdServer.get(key); const lockEvent = await etcdServer.getKey(key);
if (lockEvent?.user === object.user) { if (lockEvent?.user === object.user) {
logger.debug('Unlocking object:', object._id); logger.debug('Unlocking object:', object._id);
await etcdServer.delete(key); await etcdServer.deleteKey(key);
logger.info(`Unlocked object: ${object._id}`); logger.info(`Unlocked object: ${object._id}`);
return true; return true;
} }
@ -54,8 +56,8 @@ export class LockManager {
// Get the current lock status of an object and broadcast it // Get the current lock status of an object and broadcast it
logger.info('Getting lock status for object:', object._id); logger.info('Getting lock status for object:', object._id);
try { try {
const lockKey = `/locks/${object.type}s/${object._id}`; const lockKey = `/${object.type}s/${object._id}/lock`;
const lockValue = await etcdServer.get(lockKey); const lockValue = await etcdServer.getKey(lockKey);
if (lockValue) { if (lockValue) {
// Object is locked // Object is locked
@ -79,15 +81,19 @@ export class LockManager {
} }
setupLocksListeners() { setupLocksListeners() {
etcdServer.onPrefixPut('/locks', (key, value) => { etcdServer.onPrefixPutEvent(
const id = key.split('/').pop(); '/locks',
logger.debug('Lock object event:', id); this.socketClient.id,
this.socketManager.broadcast('notify_lock_update', { (key, value) => {
...value, const id = key.split('/').pop();
locked: true logger.debug('Lock object event:', id);
}); this.socketManager.broadcast('notify_lock_update', {
}); ...value,
etcdServer.onPrefixDelete('/locks', key => { locked: true
});
}
);
etcdServer.onPrefixDeleteEvent('/locks', this.socketClient.id, key => {
const id = key.split('/').pop(); const id = key.split('/').pop();
logger.debug('Unlock object event:', id); logger.debug('Unlock object event:', id);
this.socketManager.broadcast('notify_lock_update', { this.socketManager.broadcast('notify_lock_update', {