backend: Enhance room cleanup logic in RecordingService to improve handling of locks and in-progress recordings

This commit is contained in:
Carlos Santos 2025-04-01 18:57:44 +02:00
parent d7e61f5212
commit dece70b7e1

View File

@ -581,7 +581,7 @@ export class RecordingService {
const lockExists = await this.mutexService.lockExists(lockKey); const lockExists = await this.mutexService.lockExists(lockKey);
if (!lockExists) { if (!lockExists) {
this.logger.debug(`Lock for room ${roomId} no longer exists, skipping`); this.logger.debug(`Lock for room ${roomId} no longer exists, skipping cleanup`);
return; return;
} }
@ -596,53 +596,39 @@ export class RecordingService {
return; return;
} }
const room = await this.livekitService.getRoom(roomId); const roomExists = await this.livekitService.roomExists(roomId);
if (room.numPublishers === 0) { if (roomExists) {
const inProgressRecordings = await this.livekitService.getInProgressRecordingsEgress(roomId); // Room exists, check if it has publishers
this.logger.debug(`Room ${roomId} exists, checking for publishers`);
const room = await this.livekitService.getRoom(roomId);
const hasPublishers = room.numPublishers > 0;
if (inProgressRecordings.length > 0) { if (hasPublishers) {
this.logger.debug( // Room has publishers, but no in-progress recordings
`Room ${roomId} has ${inProgressRecordings.length} recordings in transition, keeping lock` this.logger.debug(`Room ${roomId} has publishers, checking for in-progress recordings`);
); } else {
return; // Room has no publishers
this.logger.debug(`Room ${roomId} has no publishers, checking for in-progress recordings`);
} }
} else {
this.logger.info(`Room ${roomId} no longer exists, releasing orphaned lock`); // Room does not exist, and no in-progress recordings, release the lock
await this.mutexService.release(lockKey); this.logger.debug(`Room ${roomId} no longer exists, checking for in-progress recordings`);
return;
} }
// The room has participants, check if it has in-progress recordings // Verify if in-progress recordings exist
const inProgressRecordings = await this.livekitService.getInProgressRecordingsEgress(roomId); const inProgressRecordings = await this.livekitService.getInProgressRecordingsEgress(roomId);
const hasInProgressRecordings = inProgressRecordings.length > 0;
if (inProgressRecordings.length === 0) { if (hasInProgressRecordings) {
// If the room has no active recording, release the lock this.logger.debug(`Room ${roomId} has in-progress recordings, skipping cleanup`);
this.logger.info(`No active recordings for room ${roomId}, releasing orphaned lock`);
await this.mutexService.release(lockKey);
}
} catch (error) {
if (error instanceof OpenViduMeetError && error.statusCode === 404) {
this.logger.info(`Room ${roomId} no longer exists, releasing orphaned lock`);
try {
const inProgressRecordings = await this.livekitService.getInProgressRecordingsEgress(roomId);
if (inProgressRecordings.length > 0) {
this.logger.debug(
`Room ${roomId} has ${inProgressRecordings.length} recordings in transition, keeping lock`
);
return;
}
await this.mutexService.release(lockKey);
} catch (error) {
this.logger.error(`Error releasing lock for room ${roomId}:`, error);
}
return; return;
} }
this.logger.info(`Room ${roomId} has no in-progress recordings, releasing orphaned lock`);
await this.mutexService.release(lockKey);
} catch (error) {
this.logger.error(`Error processing orphan lock for room ${roomId}:`, error);
throw error; throw error;
} }
} }