Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(media): re-probe ffmpeg after a failed availability check
Yudhi Armyndharis084c04f8ce2c
diff --git a/src/modules/media/media-conversion.service.spec.ts b/src/modules/media/media-conversion.service.spec.tsindex e40601bb5..4bf69153c 100644--- a/src/modules/media/media-conversion.service.spec.ts+++ b/src/modules/media/media-conversion.service.spec.ts@@ -109,6 +109,17 @@ describe('MediaConversionService', () => {expect(probeFfmpeg).toHaveBeenCalledTimes(1);});++ // A probe that timed out on a loaded host, or hit EAGAIN, says nothing lasting about the binary.+ it('probes again after a failed probe instead of reporting unavailable until restart', async () => {+ probeFfmpeg.mockResolvedValueOnce(false).mockResolvedValueOnce(true);+ const service = makeService(config());++ await expect(service.isAvailable()).resolves.toBe(false);+ await expect(service.isAvailable()).resolves.toBe(true);+ await expect(service.isAvailable()).resolves.toBe(true);+ expect(probeFfmpeg).toHaveBeenCalledTimes(2);+ });});describe('resolving the caller’s media', () => {diff --git a/src/modules/media/media-conversion.service.ts b/src/modules/media/media-conversion.service.tsindex cf5d788bd..9cb383f64 100644--- a/src/modules/media/media-conversion.service.ts+++ b/src/modules/media/media-conversion.service.ts@@ -26,7 +26,10 @@ export interface ConvertedMedia {@Injectable()export class MediaConversionService {private readonly logger = createLogger('MediaConversionService');- /** Result of the binary probe. Cached because it cannot change without a restart. */+ /**+ * Result of the binary probe. Only a successful probe is kept: a failure can be a timeout or a+ * transient spawn error on a loaded host, so the next request probes again.+ */private binaryAvailable?: Promise<boolean>;/*** Bounds concurrent ffmpeg processes (the rate limiter caps admission per second, not how many@@ -176,13 +179,16 @@ export class MediaConversionService {}/**- * Probe once per process. The promise itself is memoised rather than its result, so concurrent+ * Probe until a probe succeeds. The promise itself is memoised rather than its result, so concurrent* first requests share one probe instead of each spawning their own.*/private probeOnce(): Promise<boolean> {this.binaryAvailable ??= probeFfmpeg(this.configService.get<string>('mediaConversion.ffmpegPath', 'ffmpeg')).then(available => {- if (!available) this.logger.warn('Media conversion is enabled but ffmpeg could not be run');+ if (!available) {+ this.binaryAvailable = undefined;+ this.logger.warn('Media conversion is enabled but ffmpeg could not be run');+ }return available;},);