forked from mtusi2026/mtusi2026lab5
fix: close cleanup and replay races, integer thumbnail sizing, sharp deadline, contract tests
This commit is contained in:
@@ -55,6 +55,7 @@ export async function reconcile(
|
||||
s3,
|
||||
(options.now ?? new Date()).getTime() - options.stagingRetentionMs,
|
||||
options.apply,
|
||||
options.prefix,
|
||||
);
|
||||
}
|
||||
for (const prefix of options.prefix ? [options.prefix] : DEFAULT_PREFIXES) {
|
||||
@@ -69,10 +70,11 @@ async function expireFailedStaging(
|
||||
s3: S3Service,
|
||||
before: number,
|
||||
apply: boolean,
|
||||
prefix?: string,
|
||||
): Promise<number> {
|
||||
const rows = await prisma.attachment.findMany({
|
||||
where: {
|
||||
stagingKey: { not: null },
|
||||
stagingKey: { not: null, startsWith: prefix },
|
||||
OR: [{ status: 'ready' }, { status: 'failed', updatedAt: { lt: new Date(before) } }],
|
||||
},
|
||||
select: { id: true, stagingKey: true },
|
||||
@@ -83,20 +85,30 @@ async function expireFailedStaging(
|
||||
}
|
||||
let expired = 0;
|
||||
for (const row of rows) {
|
||||
try {
|
||||
await s3.delete(row.stagingKey!);
|
||||
await prisma.attachment.update({
|
||||
where: { id: row.id },
|
||||
data: { stagingKey: null },
|
||||
});
|
||||
expired++;
|
||||
} catch {
|
||||
const released = await prisma.attachment.updateMany({
|
||||
where: {
|
||||
id: row.id,
|
||||
stagingKey: row.stagingKey,
|
||||
OR: [{ status: 'ready' }, { status: 'failed', updatedAt: { lt: new Date(before) } }],
|
||||
},
|
||||
data: { stagingKey: null },
|
||||
});
|
||||
if (released.count === 0) {
|
||||
continue;
|
||||
}
|
||||
await s3.delete(row.stagingKey!).then(
|
||||
() => expired++,
|
||||
() => undefined,
|
||||
);
|
||||
}
|
||||
return expired;
|
||||
}
|
||||
|
||||
function attachmentIdOf(key: string): string | null {
|
||||
const match = /^attachments\/([0-9a-f-]{36})(\/|$)/.exec(key);
|
||||
return match ? match[1] : null;
|
||||
}
|
||||
|
||||
async function reconcilePrefix(
|
||||
prisma: ReconcilePrisma,
|
||||
s3: S3Service,
|
||||
@@ -121,12 +133,18 @@ async function reconcilePrefix(
|
||||
where: { objectKey: { in: keys } },
|
||||
select: { objectKey: true },
|
||||
});
|
||||
const ids = [...new Set(keys.map(attachmentIdOf).filter((id): id is string => id !== null))];
|
||||
const owners = ids.length
|
||||
? await prisma.attachment.findMany({ where: { id: { in: ids } }, select: { id: true } })
|
||||
: [];
|
||||
const owned = new Set(owners.map((row) => row.id));
|
||||
const known = new Set([
|
||||
...rows.flatMap((row) => [row.objectKey, row.stagingKey]),
|
||||
...variants.map((row) => row.objectKey),
|
||||
]);
|
||||
for (const { key } of old) {
|
||||
if (known.has(key)) {
|
||||
const owner = attachmentIdOf(key);
|
||||
if (known.has(key) || (owner !== null && owned.has(owner))) {
|
||||
report.referenced++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user