Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d11924224 |
No files matched your search
@@ -946,12 +946,22 @@ export class ArchiveService {
|
||||
* `setFolderEvent` landing in the gap after the count was read) get silently taken back out by the delete, and
|
||||
* then fail with a foreign key error moving its photos in here — the "empty" check now lives in the delete's own
|
||||
* `WHERE`, so Postgres re-reads it against whatever just committed (BIT-31531).
|
||||
*
|
||||
* That same concurrent commit can instead make Postgres pick this as the deadlock victim (P2034) rather than
|
||||
* block it: its own advice is to retry, so a couple of immediate attempts happen here before giving up (BIT-31680).
|
||||
*/
|
||||
private async dropIfEmpty(eventId: string): Promise<void> {
|
||||
const { db } = this.deps;
|
||||
await db.event.deleteMany({
|
||||
where: { id: eventId, archivedAt: { not: null }, photos: { none: {} }, cloudFolders: { none: {} }, galleries: { none: {} }, deliveries: { none: {} } },
|
||||
});
|
||||
for (let attempt = 1; ; attempt++) {
|
||||
try {
|
||||
await db.event.deleteMany({
|
||||
where: { id: eventId, archivedAt: { not: null }, photos: { none: {} }, cloudFolders: { none: {} }, galleries: { none: {} }, deliveries: { none: {} } },
|
||||
});
|
||||
return;
|
||||
} catch (err) {
|
||||
if ((err as { code?: string }).code !== "P2034" || attempt >= MAX_ATTEMPTS) throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- indexing --------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -851,6 +851,65 @@ describe("archive from a Nextcloud", () => {
|
||||
expect(gamePhotos.every((p) => p.eventId === galaEventId)).toBe(true);
|
||||
});
|
||||
|
||||
it("retries a cleanup delete that Postgres picked as a deadlock victim (BIT-31680)", async () => {
|
||||
const source = await connect();
|
||||
cloud.authorised = true;
|
||||
await ctx.app.inject({ method: "GET", url: `/v1/cloud-sources/${source.id}`, headers: bearer(token) });
|
||||
await worker.scan(source.id);
|
||||
for (const id of await worker.claim(10)) await worker.index(id);
|
||||
|
||||
const gameFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "20250913 - SCP x ABC" } } });
|
||||
const galaFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "Gala 20250614" } } });
|
||||
const galaEventId = galaFolder.eventId!;
|
||||
|
||||
let misses = 2;
|
||||
const gated = ctx.db.$extends({
|
||||
query: {
|
||||
event: {
|
||||
async deleteMany({ args, query }) {
|
||||
if (args.where?.id === galaEventId && misses > 0) {
|
||||
misses--;
|
||||
throw Object.assign(new Error("Transaction failed due to a write conflict or a deadlock. Please retry your transaction"), { code: "P2034" });
|
||||
}
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const flakyWorker = new ArchiveService({ db: gated as unknown as TestContext["db"], secrets: new SecretBox(KEY), log: quiet, fetchImpl: fetchProxy });
|
||||
|
||||
await flakyWorker.setFolderEvent(galaFolder.id, { eventId: gameFolder.eventId! });
|
||||
expect(misses).toBe(0);
|
||||
expect(await ctx.db.event.findUnique({ where: { id: galaEventId } })).toBeNull();
|
||||
});
|
||||
|
||||
it("gives up after repeated deadlocks on a cleanup delete (BIT-31680)", async () => {
|
||||
const source = await connect();
|
||||
cloud.authorised = true;
|
||||
await ctx.app.inject({ method: "GET", url: `/v1/cloud-sources/${source.id}`, headers: bearer(token) });
|
||||
await worker.scan(source.id);
|
||||
for (const id of await worker.claim(10)) await worker.index(id);
|
||||
|
||||
const gameFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "20250913 - SCP x ABC" } } });
|
||||
const galaFolder = await ctx.db.cloudFolder.findFirstOrThrow({ where: { path: { endsWith: "Gala 20250614" } } });
|
||||
const galaEventId = galaFolder.eventId!;
|
||||
|
||||
const gated = ctx.db.$extends({
|
||||
query: {
|
||||
event: {
|
||||
async deleteMany({ args, query }) {
|
||||
if (args.where?.id === galaEventId) throw Object.assign(new Error("Transaction failed due to a write conflict or a deadlock. Please retry your transaction"), { code: "P2034" });
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const stuckWorker = new ArchiveService({ db: gated as unknown as TestContext["db"], secrets: new SecretBox(KEY), log: quiet, fetchImpl: fetchProxy });
|
||||
|
||||
await expect(stuckWorker.setFolderEvent(galaFolder.id, { eventId: gameFolder.eventId! })).rejects.toMatchObject({ code: "P2034" });
|
||||
expect(await ctx.db.event.findUnique({ where: { id: galaEventId } })).not.toBeNull();
|
||||
});
|
||||
|
||||
it("asks again when the cloud revokes the app password", async () => {
|
||||
const source = await connect();
|
||||
cloud.authorised = true;
|
||||
|
||||
Reference in new issue
Block a user