Skip to content

Commit

Permalink
fix: 🐛 fix deadlock issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca180 committed Nov 17, 2024
1 parent ce2c162 commit 7b87a00
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion pages/api/v1/items/[id_name]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ export const fetchOwlsData = async (
},
});

await prisma.$transaction([updateAll, createAll]);
await prisma.$transaction([updateAll, createAll], {
isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
});
} else if (lastOwls && isSameDay(lastOwls.pricedAt, lastUpdated)) {
await prisma.owlsPrice.update({
where: {
Expand Down
5 changes: 4 additions & 1 deletion pages/api/v1/items/owls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isSameDay } from 'date-fns';
import { NextApiRequest, NextApiResponse } from 'next';
import prisma from '../../../../utils/prisma';
import { getManyItems } from './many';
import { Prisma } from '@prisma/client';

type OwlsRes = {
recent_updates: {
Expand Down Expand Up @@ -81,7 +82,9 @@ export const getLatestOwls = async (limit = 20) => {
},
});

await prisma.$transaction([updateAll, createAll]);
await prisma.$transaction([updateAll, createAll], {
isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
});

item.owls = {
value: owlsItem.owls_value,
Expand Down
23 changes: 14 additions & 9 deletions pages/api/v1/prices/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,20 @@ const POST = async (req: NextApiRequest, res: NextApiResponse) => {
},
});

const result = await prisma.$transaction([
prisma.itemPrices.createMany({ data: priceAddList }),
prisma.priceProcess2.updateMany({
data: { processed: true },
where: {
internal_id: { in: processedIDs },
},
}),
]);
const result = await prisma.$transaction(
[
prisma.itemPrices.createMany({ data: priceAddList, skipDuplicates: true }),
prisma.priceProcess2.updateMany({
data: { processed: true },
where: {
internal_id: { in: processedIDs },
},
}),
],
{
isolationLevel: Prisma.TransactionIsolationLevel.ReadUncommitted,
}
);

const manualCheckList = priceAddList.filter((x) => x.manual_check).map((x) => x.item_iid);

Expand Down

0 comments on commit 7b87a00

Please sign in to comment.