Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): check the newest stuck desktop recordings too, so empty caps cannot delay recovery for days

Richie McIlroybe85e844b526
diff --git a/apps/web/lib/desktop-segments-recovery.ts b/apps/web/lib/desktop-segments-recovery.ts
index 3d93cdddc..f304ece9d 100644
--- a/apps/web/lib/desktop-segments-recovery.ts
+++ b/apps/web/lib/desktop-segments-recovery.ts
@@ -9,6 +9,7 @@ import { type User, Video } from "@cap/web-domain";
import {
and,
asc,
+ desc,
eq,
gte,
inArray,
@@ -253,35 +254,50 @@ export async function recoverStaleDesktopSegments({
const staleBefore = new Date(
now.getTime() - DESKTOP_SEGMENTS_RECOVERY_MIN_AGE_MS,
);
- const legacy = await db()
- .select({ videoId: videos.id, ownerId: videos.ownerId })
- .from(videoUploads)
- .innerJoin(videos, eq(videoUploads.videoId, videos.id))
- .leftJoin(
- videoProcessingJobs,
- eq(videoUploads.videoId, videoProcessingJobs.videoId),
- )
- .where(
- and(
- inArray(videoUploads.phase, RECOVERABLE_UPLOAD_PHASES),
- lte(videoUploads.updatedAt, staleBefore),
- gte(
- videoUploads.startedAt,
- new Date(now.getTime() - DESKTOP_SEGMENTS_LEGACY_RECOVERY_MAX_AGE_MS),
- ),
- isNull(videoProcessingJobs.videoId),
- or(
- isNull(videoUploads.processingError),
- notLike(
- videoUploads.processingError,
- `${DESKTOP_RECORDING_SOURCE_REUPLOAD_REQUIRED}:%`,
+ const listLegacy = (order: typeof asc, limit: number) =>
+ db()
+ .select({ videoId: videos.id, ownerId: videos.ownerId })
+ .from(videoUploads)
+ .innerJoin(videos, eq(videoUploads.videoId, videos.id))
+ .leftJoin(
+ videoProcessingJobs,
+ eq(videoUploads.videoId, videoProcessingJobs.videoId),
+ )
+ .where(
+ and(
+ inArray(videoUploads.phase, RECOVERABLE_UPLOAD_PHASES),
+ lte(videoUploads.updatedAt, staleBefore),
+ gte(
+ videoUploads.startedAt,
+ new Date(
+ now.getTime() - DESKTOP_SEGMENTS_LEGACY_RECOVERY_MAX_AGE_MS,
+ ),
),
+ isNull(videoProcessingJobs.videoId),
+ or(
+ isNull(videoUploads.processingError),
+ notLike(
+ videoUploads.processingError,
+ `${DESKTOP_RECORDING_SOURCE_REUPLOAD_REQUIRED}:%`,
+ ),
+ ),
+ sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.source}, '$.type')) = 'desktopSegments'`,
),
- sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.source}, '$.type')) = 'desktopSegments'`,
- ),
- )
- .orderBy(asc(videoUploads.updatedAt), asc(videoUploads.videoId))
- .limit(remaining);
+ )
+ .orderBy(order(videoUploads.updatedAt), order(videoUploads.videoId))
+ .limit(limit);
+ // Legacy rows that never finish keep their oldest `updatedAt`, so an
+ // oldest-first pass alone checks each recording only shortly before it
+ // leaves the window. Most slots keep that pass; the rest go to the newest
+ // eligible rows, so a recording is first checked soon after it qualifies.
+ const newestSlots = Math.floor(remaining / 2);
+ const newest = newestSlots > 0 ? await listLegacy(desc, newestSlots) : [];
+ const oldest = await listLegacy(asc, remaining);
+ const picked = new Set(newest.map((candidate) => candidate.videoId));
+ const legacy = [
+ ...newest,
+ ...oldest.filter((candidate) => !picked.has(candidate.videoId)),
+ ].slice(0, remaining);
for (const candidate of legacy) {
try {
const result = await completeDesktopSegmentsManifestAndQueue({