backend: Refactor recording lock acquisition logic to prevent unnecessary cleanup on active recordings

This commit is contained in:
Carlos Santos 2025-04-29 17:49:37 +02:00
parent 1a13d3d98b
commit 7376f1dc04

View File

@ -64,6 +64,11 @@ export class RecordingService {
let timeoutId: NodeJS.Timeout | undefined;
try {
// Attempt to acquire lock. If the lock is not acquired, the recording is already active.
acquiredLock = await this.acquireRoomRecordingActiveLock(roomId);
if (!acquiredLock) throw errorRecordingAlreadyStarted(roomId);
const room = await this.roomService.getMeetRoom(roomId);
if (!room) throw errorRoomNotFound(roomId);
@ -78,11 +83,6 @@ export class RecordingService {
if (!hasParticipants) throw errorRoomHasNoParticipants(roomId);
// Attempt to acquire lock. If the lock is not acquired, the recording is already active.
acquiredLock = await this.acquireRoomRecordingActiveLock(roomId);
if (!acquiredLock) throw errorRecordingAlreadyStarted(roomId);
const startTimeoutPromise = new Promise<never>((_, reject) => {
timeoutId = setTimeout(() => {
this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener);
@ -123,10 +123,14 @@ export class RecordingService {
throw error;
} finally {
try {
clearTimeout(timeoutId);
this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener);
if (acquiredLock) await this.releaseRecordingLockIfNoEgress(roomId);
if (acquiredLock) {
// Only clean up resources if the lock was successfully acquired.
// This prevents unnecessary cleanup operations when the request was rejected
// due to another recording already in progress in this room.
clearTimeout(timeoutId);
this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener);
await this.releaseRecordingLockIfNoEgress(roomId);
}
} catch (e) {
this.logger.warn(`Failed to release recording lock: ${e}`);
}