Skip to content

Retry batch item completion #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added isPrismaRetriableError()
  • Loading branch information
matt-aitken committed Feb 6, 2025
commit 1ba00b149aa89480574e888b43e12fb4615d437c
15 changes: 13 additions & 2 deletions internal-packages/database/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ function isTransactionClient(prisma: PrismaClientOrTransaction): prisma is Prism
return !("$transaction" in prisma);
}

function isPrismaKnownError(error: unknown): error is Prisma.PrismaClientKnownRequestError {
export function isPrismaKnownError(error: unknown): error is Prisma.PrismaClientKnownRequestError {
return (
typeof error === "object" && error !== null && "code" in error && typeof error.code === "string"
);
}

export function isPrismaRetriableError(error: unknown): boolean {
if (!isPrismaKnownError(error)) {
return false;
}

return retryCodes.includes(error.code);
}

export type PrismaTransactionOptions = {
/** The maximum amount of time (in ms) Prisma Client will wait to acquire a transaction from the database. The default value is 2000ms. */
maxWait?: number;
Expand All @@ -39,6 +47,9 @@ export type PrismaTransactionOptions = {
maxRetries?: number;
};

//retry these Prisma error codes
const retryCodes = ["P2034", "P2028"];

export async function $transaction<R>(
prisma: PrismaClientOrTransaction,
fn: (prisma: PrismaTransactionClient) => Promise<R>,
Expand All @@ -55,7 +66,7 @@ export async function $transaction<R>(
} catch (error) {
if (isPrismaKnownError(error)) {
if (
error.code === "P2034" &&
retryCodes.includes(error.code) &&
typeof options?.maxRetries === "number" &&
attempt < options.maxRetries
) {
Expand Down