diff --git a/src/app.ts b/src/app.ts index e93a2d2..9446b4c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -16,10 +16,11 @@ import { import { checkChromiumExecutable, logger, - randomActivateAudio, registerExitHandler, resolvePackagePath, sleep, + startRandomActivateAudio, + stopRandomActivateAudio, stopUpdateSystemStats, } from './utils' import { calculateVmafScore } from './vmaf' @@ -99,7 +100,7 @@ export async function setupApplication( // Start the local sessions. if ((config.url || config.customUrlHandler) && config.sessions) { if (config.randomAudioPeriod) { - await randomActivateAudio( + startRandomActivateAudio( stats.sessions, config.randomAudioPeriod, config.randomAudioProbability, @@ -131,6 +132,8 @@ export async function setupApplication( return async (): Promise => { log.debug('Stopping') + stopRandomActivateAudio() + if (server) { server.stop() } diff --git a/src/utils.ts b/src/utils.ts index e830ce1..c06fe56 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -223,6 +223,28 @@ declare global { } let randomActivateAudioTimeoutId: NodeJS.Timeout | null = null +let randomActivateAudioRunning = false + +export function startRandomActivateAudio( + sessions: Map, + randomAudioPeriod: number, + randomAudioProbability: number, + randomAudioRange: number, +): void { + if (randomActivateAudioRunning) return + randomActivateAudioRunning = true + void randomActivateAudio( + sessions, + randomAudioPeriod, + randomAudioProbability, + randomAudioRange, + ) +} + +export function stopRandomActivateAudio(): void { + randomActivateAudioRunning = false + randomActivateAudioTimeoutId && clearTimeout(randomActivateAudioTimeoutId) +} /** * Randomly activate audio from one tab at time. @@ -237,7 +259,7 @@ export async function randomActivateAudio( randomAudioProbability: number, randomAudioRange: number, ): Promise { - if (!randomAudioPeriod) { + if (!randomAudioPeriod || !randomActivateAudioRunning) { return } try { @@ -620,8 +642,8 @@ export async function resolveIP( return ip } }) - .catch(err => { - log.error(`resolveIP error: ${(err as Error).stack}`) + .catch(_err => { + // log.error(`resolveIP error: ${(err as Error).stack}`) ipCache.set(ip, { host: ip, timestamp }) }), ])