From e564aa6f226998b9c54e19ddd997d9e6e6aad641 Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 4 Nov 2026 23:18:31 +0000 Subject: [PATCH] fix(faces): analyse match photos ahead of reference headshots A tick that waited on reference photos never reached the match, and the heartbeat went stale so the library showed face analysis as down. --- apps/api/src/faces/service.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/apps/api/src/faces/service.ts b/apps/api/src/faces/service.ts index 440c59d..17005ae 100644 --- a/apps/api/src/faces/service.ts +++ b/apps/api/src/faces/service.ts @@ -1380,6 +1380,8 @@ export class FaceWorker { private ticking = false; private lastBeat = 0; + private beatTimer: ReturnType | undefined; + private referenceInFlight = false; constructor( private readonly service: FaceService, @@ -1413,6 +1415,12 @@ export class FaceWorker { if (n) this.opts.log.info({ events: n }, "faces: lineup photographs named from shirt numbers"); }) .catch((err) => this.opts.log.warn({ err }, "faces: lineup photographs not named")); + // The beat is not part of the tick. A reference headshot can spend minutes refreshing suggestions; + // the API must still see the worker as alive, and the photo queue must not wait behind that. + this.beatTimer = setInterval(() => { + if (!this.stopped) this.beat(); + }, 20_000); + this.beatTimer.unref?.(); let missedAt = 0; let failedAt = 0; let videosCheckedAt = Date.now(); @@ -1435,7 +1443,6 @@ export class FaceWorker { const n = await this.service.requeueVideos(); if (n) this.opts.log.info({ videos: n }, "faces: videos queued again"); } - for (const id of await this.service.claimReferences(5)) await this.service.processReference(id); // Archive videos one at a time, in one of the engine's places: with photos always arriving (a live game, // the archive's backlog) a video waiting for the photos to stop would never start. With a single place the // photos keep it, and a video waits until they leave it free. @@ -1458,6 +1465,18 @@ export class FaceWorker { }); } if (!shared && !ids.length && this.inFlight === 0 && this.videosInFlight === 0) await startVideo(); + // One reference headshot per tick, without waiting: naming it walks every recent game and must not + // hold up the photos of the game being shot. + if (!this.referenceInFlight) { + const refs = await this.service.claimReferences(1); + const personId = refs[0]; + if (personId) { + this.referenceInFlight = true; + void this.service.processReference(personId).finally(() => { + this.referenceInFlight = false; + }); + } + } } catch (err) { this.opts.log.warn({ err }, "faces: poll failed"); } finally { @@ -1471,6 +1490,7 @@ export class FaceWorker { async stop(timeoutMs = 25_000): Promise { this.stopped = true; if (this.timer) clearTimeout(this.timer); + if (this.beatTimer) clearInterval(this.beatTimer); // A video being read can take long: it stops at the next frame and goes back in the queue. this.service.stopVideos(); const drained = await waitUntil(() => !this.ticking && this.inFlight === 0 && this.videosInFlight === 0, { timeoutMs });