Skip to content

Commit

Permalink
Add Wait and Timeout to Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
casesandberg committed Sep 25, 2024
1 parent 4893dab commit 0c1e301
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
1 change: 1 addition & 0 deletions packages/finance/lib/executeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function executeTransaction({
return transaction
},
{
maxWait: 5000,
timeout: 10000,
}
)
Expand Down
70 changes: 38 additions & 32 deletions packages/lists/lib/createList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,49 @@ export async function createList({
)
)

const createdList = await db.$transaction(async (tx) => {
const list = await tx.list.create({
data: {
title,
description,
ownerId,
slug,
tags: tags?.map((tag) => slugifyTitle(tag)),
markets: {
createMany: {
data: createdMarkets.map((market) => ({
marketId: market.id,
})),
const createdList = await db.$transaction(
async (tx) => {
const list = await tx.list.create({
data: {
title,
description,
ownerId,
slug,
tags: tags?.map((tag) => slugifyTitle(tag)),
markets: {
createMany: {
data: createdMarkets.map((market) => ({
marketId: market.id,
})),
},
},
contributionPolicy,
},
contributionPolicy,
},
include: {
markets: true,
},
})
include: {
markets: true,
},
})

await Promise.all(
createdMarkets.map((market) => {
return tx.market.update({
where: {
id: market.id,
},
data: {
parentListId: list.id,
},
await Promise.all(
createdMarkets.map((market) => {
return tx.market.update({
where: {
id: market.id,
},
data: {
parentListId: list.id,
},
})
})
})
)
)

return list
})
return list
},
{
maxWait: 5000,
timeout: 10000,
}
)

return createdList
}
54 changes: 30 additions & 24 deletions packages/markets/lib/resolveMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,38 @@ export async function resolveMarket({
throw new Error('User cannot resolve market')
}

await db.$transaction(async (tx) => {
const now = new Date()
await db.$transaction(
async (tx) => {
const now = new Date()

await tx.marketResolution.upsert({
where: { marketId },
create: {
marketId,
resolutionId: optionId,
supportingLink,
resolvedById: resolverId,
createdAt: now,
updatedAt: now,
},
update: {
resolutionId: optionId,
supportingLink,
resolvedById: resolverId,
updatedAt: now,
},
})
await tx.marketResolution.upsert({
where: { marketId },
create: {
marketId,
resolutionId: optionId,
supportingLink,
resolvedById: resolverId,
createdAt: now,
updatedAt: now,
},
update: {
resolutionId: optionId,
supportingLink,
resolvedById: resolverId,
updatedAt: now,
},
})

await tx.market.update({
where: { id: marketId },
data: { resolvedAt: now, closeDate: now, updatedAt: now },
})
})
await tx.market.update({
where: { id: marketId },
data: { resolvedAt: now, closeDate: now, updatedAt: now },
})
},
{
maxWait: 5000,
timeout: 10000,
}
)

await createMarketResolveLossTransactions({
marketId,
Expand Down

0 comments on commit 0c1e301

Please sign in to comment.