Skip to content

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

feat: add tenant-based concurrency queue

Marc Seitzfa619cc03b9a
diff --git a/lib/swr/use-billing.ts b/lib/swr/use-billing.ts
index c0825f04..f3f22791 100644
--- a/lib/swr/use-billing.ts
+++ b/lib/swr/use-billing.ts
@@ -37,13 +37,7 @@ export function useBilling() {
};
}
-export type BasePlan =
- | "free"
- | "starter"
- | "pro"
- | "trial"
- | "business"
- | "datarooms";
+export type BasePlan = "free" | "starter" | "pro" | "business" | "datarooms";
type PlanWithTrial = `${BasePlan}+drtrial`;
type PlanWithOld = `${BasePlan}+old` | `${BasePlan}+drtrial+old`;
diff --git a/lib/utils/trigger-utils.ts b/lib/utils/trigger-utils.ts
new file mode 100644
index 00000000..11ac4768
--- /dev/null
+++ b/lib/utils/trigger-utils.ts
@@ -0,0 +1,23 @@
+import { BasePlan } from "../swr/use-billing";
+
+type TQueueConfig = {
+ name: string;
+ concurrencyLimit: number;
+};
+
+const concurrencyConfig: Record<string, number> = {
+ free: 1,
+ starter: 1,
+ pro: 2,
+ business: 10,
+ datarooms: 10,
+};
+
+export const conversionQueue = (plan: string): TQueueConfig => {
+ const planName = plan.split("+")[0] as BasePlan;
+
+ return {
+ name: `conversion-${planName}`,
+ concurrencyLimit: concurrencyConfig[planName],
+ };
+};
diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
index 682c5938..aa0badb5 100644
--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
@@ -12,6 +12,7 @@ import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";
import { convertPdfToImage } from "@/lib/trigger/pdf-to-image";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
+import { conversionQueue } from "@/lib/utils/trigger-utils";
export default async function handle(
req: NextApiRequest,
@@ -42,7 +43,7 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- const { document } = await getTeamWithUsersAndDocument({
+ const { team, document } = await getTeamWithUsersAndDocument({
teamId,
userId,
docId: documentId,
@@ -102,8 +103,6 @@ export default async function handle(
});
if (type === "docs" || type === "slides") {
- console.log("converting docx or pptx to pdf");
- // Trigger convert-files-to-pdf task
await convertFilesToPdfTask.trigger(
{
documentVersionId: version.id,
@@ -117,15 +116,13 @@ export default async function handle(
`document_${documentId}`,
`version:${version.id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
}
// trigger document uploaded event to trigger convert-pdf-to-image job
if (type === "pdf") {
- // if (
- // teamId === "cluqtfmcr0001zkza4xcgqatw" ||
- // teamId === "clup33by90000oewh4rfvp2eg" // local
- // ) {
await convertPdfToImage.trigger(
{
documentId: documentId,
@@ -141,20 +138,10 @@ export default async function handle(
`document_${documentId}`,
`version:${version.id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
- // } else {
- // await client.sendEvent({
- // id: version.id,
- // name: "document.uploaded",
- // payload: {
- // documentVersionId: version.id,
- // versionNumber: version.versionNumber,
- // documentId: documentId,
- // teamId: teamId,
- // },
- // });
- // }
}
if (type === "sheet" && document?.advancedExcelEnabled) {
diff --git a/pages/api/teams/[teamId]/documents/agreement.ts b/pages/api/teams/[teamId]/documents/agreement.ts
index 497266d6..50c1450b 100644
--- a/pages/api/teams/[teamId]/documents/agreement.ts
+++ b/pages/api/teams/[teamId]/documents/agreement.ts
@@ -13,6 +13,7 @@ import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";
import { convertPdfToImage } from "@/lib/trigger/pdf-to-image";
import { CustomUser } from "@/lib/types";
import { getExtension, log } from "@/lib/utils";
+import { conversionQueue } from "@/lib/utils/trigger-utils";
import { authOptions } from "../../../auth/[...nextauth]";
@@ -54,7 +55,7 @@ export default async function handle(
};
try {
- await getTeamWithUsersAndDocument({
+ const { team } = await getTeamWithUsersAndDocument({
teamId,
userId,
});
@@ -118,8 +119,6 @@ export default async function handle(
});
if (type === "docs") {
- console.log("converting docx to pdf");
- // Trigger convert-files-to-pdf task
await convertFilesToPdfTask.trigger(
{
documentId: document.id,
@@ -133,11 +132,12 @@ export default async function handle(
`document_${document.id}`,
`version:${document.versions[0].id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
}
- // skip triggering convert-pdf-to-image job for "notion" / "excel" documents
if (type === "pdf") {
await convertPdfToImage.trigger(
{
@@ -153,18 +153,10 @@ export default async function handle(
`document_${document.id}`,
`version:${document.versions[0].id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
- // // trigger document uploaded event to trigger convert-pdf-to-image job
- // await client.sendEvent({
- // id: document.versions[0].id, // unique eventId for the run
- // name: "document.uploaded",
- // payload: {
- // documentVersionId: document.versions[0].id,
- // teamId: teamId,
- // documentId: document.id,
- // },
- // });
}
return res.status(201).json(document);
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index e3a48f2b..bb98f81d 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -20,6 +20,7 @@ import { processVideo } from "@/lib/trigger/optimize-video-files";
import { convertPdfToImage } from "@/lib/trigger/pdf-to-image";
import { CustomUser } from "@/lib/types";
import { getExtension, log } from "@/lib/utils";
+import { conversionQueue } from "@/lib/utils/trigger-utils";
export default async function handle(
req: NextApiRequest,
@@ -186,7 +187,7 @@ export default async function handle(
};
try {
- await getTeamWithUsersAndDocument({
+ const { team } = await getTeamWithUsersAndDocument({
teamId,
userId,
});
@@ -267,8 +268,6 @@ export default async function handle(
});
if (type === "docs" || type === "slides") {
- console.log("converting docx or pptx to pdf");
- // Trigger convert-files-to-pdf task
await convertFilesToPdfTask.trigger(
{
documentId: document.id,
@@ -282,13 +281,13 @@ export default async function handle(
`document_${document.id}`,
`version:${document.versions[0].id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
}
if (type === "cad") {
- console.log("converting cad to pdf");
- // Trigger convert-files-to-pdf task
await convertCadToPdfTask.trigger(
{
documentId: document.id,
@@ -302,6 +301,8 @@ export default async function handle(
`document_${document.id}`,
`version:${document.versions[0].id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
}
@@ -322,17 +323,14 @@ export default async function handle(
`document_${document.id}`,
`version:${document.versions[0].id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
}
// skip triggering convert-pdf-to-image job for "notion" / "excel" documents
if (type === "pdf") {
- // trigger document uploaded event to trigger convert-pdf-to-image job
- // if (
- // teamId === "cluqtfmcr0001zkza4xcgqatw" ||
- // teamId === "clup33by90000oewh4rfvp2eg" // local
- // ) {
await convertPdfToImage.trigger(
{
documentId: document.id,
@@ -347,19 +345,10 @@ export default async function handle(
`document_${document.id}`,
`version:${document.versions[0].id}`,
],
+ queue: conversionQueue(team.plan),
+ concurrencyKey: teamId,
},
);
- // } else {
- // await client.sendEvent({
- // id: document.versions[0].id, // unique eventId for the run
- // name: "document.uploaded",
- // payload: {
- // documentVersionId: document.versions[0].id,
- // teamId: teamId,
- // documentId: document.id,
- // },
- // });
- // }
}
return res.status(201).json(document);