From 74d1b542b65681a152a5e5d7861a5c49784b0f7b Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Mon, 21 Apr 2025 17:32:31 +0200 Subject: [PATCH] backend: Refactor recording timeout handling to properly register and unregister event listeners --- backend/src/services/recording.service.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/backend/src/services/recording.service.ts b/backend/src/services/recording.service.ts index ca533e3..f510f62 100644 --- a/backend/src/services/recording.service.ts +++ b/backend/src/services/recording.service.ts @@ -85,25 +85,28 @@ export class RecordingService { const { recordingId } = recordingInfo; const recordingPromise = new Promise((resolve, reject) => { - this.taskSchedulerService.registerTask({ - name: `${roomId}_recording_timeout`, - type: 'timeout', - scheduleOrDelay: INTERNAL_CONFIG.RECORDING_STARTED_TIMEOUT, - callback: this.handleRecordingLockTimeout.bind(this, recordingId, roomId, reject) - }); - - this.systemEventService.once(SystemEventType.RECORDING_ACTIVE, (info: Record) => { + const eventListener = (info: Record) => { // This listener is triggered only for the instance that started the recording. // Check if the recording ID matches the one that was started const isEventForCurrentRecording = info?.recordingId === recordingId && info?.roomId === roomId; if (isEventForCurrentRecording) { this.taskSchedulerService.cancelTask(`${roomId}_recording_timeout`); + this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, eventListener); resolve(info as unknown as MeetRecordingInfo); } else { this.logger.error('Received recording active event with mismatched recording ID:', info); } + }; + + this.taskSchedulerService.registerTask({ + name: `${roomId}_recording_timeout`, + type: 'timeout', + scheduleOrDelay: INTERNAL_CONFIG.RECORDING_STARTED_TIMEOUT, + callback: this.handleRecordingLockTimeout.bind(this, recordingId, roomId, eventListener, reject) }); + + this.systemEventService.on(SystemEventType.RECORDING_ACTIVE, eventListener); }); return await recordingPromise; @@ -128,8 +131,6 @@ export class RecordingService { // Cancel the recording cleanup timer if it is running this.taskSchedulerService.cancelTask(`${roomId}_recording_timeout`); - // Remove the listener for the EGRESS_STARTED event. - this.systemEventService.off(SystemEventType.RECORDING_ACTIVE); switch (egress.status) { case EgressStatus.EGRESS_ACTIVE: @@ -491,8 +492,10 @@ export class RecordingService { protected async handleRecordingLockTimeout( recordingId: string, roomId: string, + listener: (info: Record) => void, rejectRequest: (reason?: unknown) => void ) { + this.systemEventService.off(SystemEventType.RECORDING_ACTIVE, listener); this.logger.debug(`Recording cleanup timer triggered for room '${roomId}'.`); let shouldReleaseLock = false;