Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(trigger): add retry

Marc Seitzfd0dbddf8992
diff --git a/app/api/trigger/route.ts b/app/api/trigger/route.ts
index 1bc4304a..eb5dda1a 100644
--- a/app/api/trigger/route.ts
+++ b/app/api/trigger/route.ts
@@ -3,7 +3,7 @@ import { client } from "@/trigger";
import { createAppRoute } from "@trigger.dev/nextjs";
// This is the maximum duration of a job in seconds
-export const maxDuration = 120;
+export const maxDuration = 180;
// This route is used to send and receive data with Trigger.dev
export const { POST, dynamic } = createAppRoute(client);
diff --git a/jobs/convert-pdf-to-image.ts b/jobs/convert-pdf-to-image.ts
index cb5c3945..0d09b92d 100644
--- a/jobs/convert-pdf-to-image.ts
+++ b/jobs/convert-pdf-to-image.ts
@@ -134,7 +134,7 @@ client.defineJob({
currentPage = i + 1;
await io.runTask(
`upload-page-${currentPage}`,
- async () => {
+ async ({ attempts }) => {
// send page number to api/convert-page endpoint in a task and get back page img url
const response = await fetch(
`${process.env.NEXT_PUBLIC_BASE_URL}/api/mupdf/convert-page`,
@@ -145,6 +145,7 @@ client.defineJob({
pageNumber: currentPage,
url: signedUrl,
teamId: payload.teamId,
+ attempts: attempts - 1,
}),
headers: {
"Content-Type": "application/json",
@@ -171,7 +172,15 @@ client.defineJob({
return { documentPageId, payload };
},
// { retry: retry.standardBackoff },
- {},
+ {
+ retry: {
+ limit: 3,
+ minTimeoutInMs: 1000,
+ maxTimeoutInMs: 10000,
+ factor: 2,
+ randomize: true,
+ },
+ },
(error) => {
conversionWithoutError = false;
return { error: error as Error };
diff --git a/pages/api/mupdf/convert-page.ts b/pages/api/mupdf/convert-page.ts
index 1a1afa9d..f71189c8 100644
--- a/pages/api/mupdf/convert-page.ts
+++ b/pages/api/mupdf/convert-page.ts
@@ -12,6 +12,8 @@ export const config = {
maxDuration: 180,
};
+const SCALE_FACTORS = [3, 2, 1];
+
export default async (req: NextApiRequest, res: NextApiResponse) => {
// check if post method
if (req.method !== "POST") {
@@ -29,11 +31,18 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
return;
}
- const { documentVersionId, pageNumber, url, teamId } = req.body as {
+ const {
+ documentVersionId,
+ pageNumber,
+ url,
+ teamId,
+ attempt = 0,
+ } = req.body as {
documentVersionId: string;
pageNumber: number;
url: string;
teamId: string;
+ attempt: number;
};
try {
@@ -56,14 +65,18 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
var doc = new mupdf.PDFDocument(pdfData);
console.log("Original document size:", pdfData.byteLength);
- const desiredDPI = 216;
+ const scaleFactor = SCALE_FACTORS[attempt] || 1;
+ const desiredDPI = 72 * scaleFactor;
const currentDPI = 72;
// Calculate the scale factor to achieve the desired DPI
- const scaleFactor = desiredDPI / currentDPI;
- const doc_to_screen = mupdf.Matrix.scale(scaleFactor, scaleFactor);
+ const dpiScaleFactor = desiredDPI / currentDPI;
+ const doc_to_screen = mupdf.Matrix.scale(dpiScaleFactor, dpiScaleFactor);
- console.log("scaleFactor:", scaleFactor);
+ console.log(
+ `Converting with scale factor: ${scaleFactor}, DPI: ${desiredDPI}`,
+ );
+ console.log("attempt", attempt);
// Scale the document to 300 DPI
// const doc_to_screen = mupdf.Matrix.scale(216 / 72, 216 / 72); // scale 3x // to 216 DPI
@@ -165,7 +178,7 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
}
// Send the images as a response
- res.status(200).json({ documentPageId: documentPage.id });
+ res.status(200).json({ documentPageId: documentPage!.id });
return;
} catch (error) {
log({