Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Add export job cancellation support for background tasks
Cursor Agent25c4224e7130
diff --git a/components/datarooms/export-visits-modal.tsx b/components/datarooms/export-visits-modal.tsxindex 309901ad..cf481022 100644--- a/components/datarooms/export-visits-modal.tsx+++ b/components/datarooms/export-visits-modal.tsx@@ -201,6 +201,29 @@ export function ExportVisitsModal({}};+ // Cancel export+ const cancelExport = async () => {+ if (!exportStatus?.exportId) return;++ try {+ const response = await fetch(+ `/api/teams/${teamId}/export-jobs/${exportStatus.exportId}`,+ { method: "PATCH" },+ );++ if (response.ok) {+ toast.success("Export cancelled successfully");+ handleClose();+ } else {+ const errorData = await response.json();+ toast.error(errorData.error || "Failed to cancel export");+ }+ } catch (error) {+ console.error("Error cancelling export:", error);+ toast.error("Failed to cancel export");+ }+ };+// Handle close - cleanup and call parent onCloseconst handleClose = () => {if (pollIntervalRef.current) {@@ -288,6 +311,18 @@ export function ExportVisitsModal({Your export will be ready shortly...</div>)}++ {/* Cancel button - only show if export is in progress */}+ {exportStatus?.exportId && (+ <div className="flex gap-2">+ <button+ onClick={cancelExport}+ className="flex-1 rounded-md border border-red-300 bg-white px-4 py-2 text-sm text-red-600 transition-colors hover:bg-red-50 dark:border-red-600 dark:bg-gray-900 dark:text-red-400 dark:hover:bg-red-950"+ >+ Cancel Export+ </button>+ </div>+ )}</div></div></div>diff --git a/components/documents/export-visits-modal.tsx b/components/documents/export-visits-modal.tsxindex 1d7dd0c7..b0df43cc 100644--- a/components/documents/export-visits-modal.tsx+++ b/components/documents/export-visits-modal.tsx@@ -196,6 +196,29 @@ export function ExportVisitsModal({}};+ // Cancel export+ const cancelExport = async () => {+ if (!exportStatus?.exportId) return;++ try {+ const response = await fetch(+ `/api/teams/${teamId}/export-jobs/${exportStatus.exportId}`,+ { method: "PATCH" },+ );++ if (response.ok) {+ toast.success("Export cancelled successfully");+ handleClose();+ } else {+ const errorData = await response.json();+ toast.error(errorData.error || "Failed to cancel export");+ }+ } catch (error) {+ console.error("Error cancelling export:", error);+ toast.error("Failed to cancel export");+ }+ };+// Handle close - cleanup and call parent onCloseconst handleClose = () => {if (pollIntervalRef.current) {@@ -275,6 +298,18 @@ export function ExportVisitsModal({Your export will be ready shortly...</div>)}++ {/* Cancel button - only show if export is in progress */}+ {exportStatus?.exportId && (+ <div className="flex gap-2">+ <button+ onClick={cancelExport}+ className="flex-1 rounded-md border border-red-300 bg-white px-4 py-2 text-sm text-red-600 transition-colors hover:bg-red-50 dark:border-red-600 dark:bg-gray-900 dark:text-red-400 dark:hover:bg-red-950"+ >+ Cancel Export+ </button>+ </div>+ )}</div></div></div>diff --git a/lib/redis-job-store.ts b/lib/redis-job-store.tsindex dd3769a5..595f134c 100644--- a/lib/redis-job-store.ts+++ b/lib/redis-job-store.ts@@ -15,6 +15,7 @@ export interface ExportJob {error?: string;userId: string;teamId: string;+ triggerRunId?: string; // Trigger.dev run ID for cancellationcreatedAt: string;updatedAt: string;completedAt?: string;diff --git a/pages/api/teams/[teamId]/datarooms/[id]/export-visits.ts b/pages/api/teams/[teamId]/datarooms/[id]/export-visits.tsindex 59ff282e..1433195c 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/export-visits.ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/export-visits.ts@@ -79,7 +79,7 @@ export default async function handler(});// Trigger the background task- await exportVisitsTask.trigger(+ const handle = await exportVisitsTask.trigger({type: "dataroom",teamId,@@ -97,9 +97,14 @@ export default async function handler(},);+ // Update the job with the trigger run ID for cancellation+ const updatedJob = await jobStore.updateJob(exportJob.id, {+ triggerRunId: handle.id,+ });+return res.status(200).json({- exportId: exportJob.id,- status: exportJob.status,+ exportId: updatedJob?.id || exportJob.id,+ status: updatedJob?.status || exportJob.status,message: "Export job created successfully. You will be notified when it's ready.",});} catch (error) {diff --git a/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/export-visits.ts b/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/export-visits.tsindex 7f2bc13c..fef47d86 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/export-visits.ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/export-visits.ts@@ -85,7 +85,7 @@ export default async function handler(});// Trigger the background task- await exportVisitsTask.trigger(+ const handle = await exportVisitsTask.trigger({type: "dataroom",teamId,@@ -104,9 +104,14 @@ export default async function handler(},);+ // Update the job with the trigger run ID for cancellation+ const updatedJob = await jobStore.updateJob(exportJob.id, {+ triggerRunId: handle.id,+ });+return res.status(200).json({- exportId: exportJob.id,- status: exportJob.status,+ exportId: updatedJob?.id || exportJob.id,+ status: updatedJob?.status || exportJob.status,message: "Export job created successfully. You will be notified when it's ready.",});} catch (error) {diff --git a/pages/api/teams/[teamId]/documents/[id]/export-visits.ts b/pages/api/teams/[teamId]/documents/[id]/export-visits.tsindex ef30bde8..16bb4a8b 100644--- a/pages/api/teams/[teamId]/documents/[id]/export-visits.ts+++ b/pages/api/teams/[teamId]/documents/[id]/export-visits.ts@@ -76,7 +76,7 @@ export default async function handler(});// Trigger the background task- await exportVisitsTask.trigger(+ const handle = await exportVisitsTask.trigger({type: "document",teamId,@@ -94,9 +94,14 @@ export default async function handler(},);+ // Update the job with the trigger run ID for cancellation+ const updatedJob = await jobStore.updateJob(exportJob.id, {+ triggerRunId: handle.id,+ });+return res.status(200).json({- exportId: exportJob.id,- status: exportJob.status,+ exportId: updatedJob?.id || exportJob.id,+ status: updatedJob?.status || exportJob.status,message: "Export job created successfully. You will be notified when it's ready.",});} catch (error) {diff --git a/pages/api/teams/[teamId]/export-jobs.ts b/pages/api/teams/[teamId]/export-jobs.tsindex f0d5eae9..efe52a08 100644--- a/pages/api/teams/[teamId]/export-jobs.ts+++ b/pages/api/teams/[teamId]/export-jobs.ts@@ -67,7 +67,7 @@ export default async function handler(});// Trigger the background task- await exportVisitsTask.trigger(+ const handle = await exportVisitsTask.trigger({type,teamId,@@ -86,9 +86,14 @@ export default async function handler(},);+ // Update the job with the trigger run ID for cancellation+ const updatedJob = await jobStore.updateJob(exportJob.id, {+ triggerRunId: handle.id,+ });+return res.status(200).json({- exportId: exportJob.id,- status: exportJob.status,+ exportId: updatedJob?.id || exportJob.id,+ status: updatedJob?.status || exportJob.status,message: "Export job created successfully",});} catch (error) {diff --git a/pages/api/teams/[teamId]/export-jobs/[exportId].ts b/pages/api/teams/[teamId]/export-jobs/[exportId].tsindex 1d9703c4..f883d39b 100644--- a/pages/api/teams/[teamId]/export-jobs/[exportId].ts+++ b/pages/api/teams/[teamId]/export-jobs/[exportId].ts@@ -65,6 +65,53 @@ export default async function handler(}}+ if (req.method === "PATCH") {+ try {+ // Get the job first to verify ownership+ const exportJob = await jobStore.getJob(exportId);++ if (+ !exportJob ||+ exportJob.teamId !== teamId ||+ exportJob.userId !== userId+ ) {+ return res.status(404).json({ error: "Export job not found" });+ }++ // Check if job can be cancelled+ if (!["PENDING", "PROCESSING"].includes(exportJob.status)) {+ return res.status(400).json({+ error: "Export job cannot be cancelled in current state"+ });+ }++ // Cancel the trigger run if we have the run ID+ if (exportJob.triggerRunId) {+ try {+ const { runs } = await import("@trigger.dev/sdk/v3");+ await runs.cancel(exportJob.triggerRunId);+ } catch (error) {+ console.error("Failed to cancel trigger run:", error);+ // Continue with local cancellation even if trigger cancellation fails+ }+ }++ // Update job status to cancelled+ const updatedJob = await jobStore.updateJob(exportId, {+ status: "FAILED",+ error: "Export cancelled by user"+ });++ return res.status(200).json({+ message: "Export job cancelled successfully",+ job: updatedJob+ });+ } catch (error) {+ console.error("Error cancelling export job:", error);+ return res.status(500).json({ error: "Failed to cancel export job" });+ }+ }+if (req.method === "DELETE") {try {// Get the job first to verify ownership@@ -90,6 +137,6 @@ export default async function handler(}}- res.setHeader("Allow", ["GET", "DELETE"]);+ res.setHeader("Allow", ["GET", "PATCH", "DELETE"]);return res.status(405).end(`Method ${req.method} Not Allowed`);}