Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api/crawl-cancel): return 409 if crawl is completed

Gergő Móricz0a9f367ca594
diff --git a/apps/api/src/controllers/v0/crawl-cancel.ts b/apps/api/src/controllers/v0/crawl-cancel.ts
index 78d1cbf52..11ea5b9f6 100644
--- a/apps/api/src/controllers/v0/crawl-cancel.ts
+++ b/apps/api/src/controllers/v0/crawl-cancel.ts
@@ -6,6 +6,7 @@ import { getCrawl, saveCrawl } from "../../../src/lib/crawl-redis";
import * as Sentry from "@sentry/node";
import { configDotenv } from "dotenv";
import { redisEvictConnection } from "../../../src/services/redis";
+import { crawlGroup } from "../../services/worker/nuq";
configDotenv();
export async function crawlCancelController(req: Request, res: Response) {
@@ -53,6 +54,15 @@ export async function crawlCancelController(req: Request, res: Response) {
return res.status(403).json({ error: "Unauthorized" });
}
+ const group = await crawlGroup.getGroup(req.params.jobId);
+ if (!group) {
+ return res.status(404).json({ error: "Job not found" });
+ }
+
+ if (group.status === "completed") {
+ return res.status(409).json({ error: "Crawl is already completed" });
+ }
+
try {
sc.cancelled = true;
await saveCrawl(req.params.jobId, sc);
diff --git a/apps/api/src/controllers/v1/crawl-cancel.ts b/apps/api/src/controllers/v1/crawl-cancel.ts
index 8ddd9fdba..c260ec3d1 100644
--- a/apps/api/src/controllers/v1/crawl-cancel.ts
+++ b/apps/api/src/controllers/v1/crawl-cancel.ts
@@ -4,6 +4,7 @@ import { getCrawl, saveCrawl } from "../../lib/crawl-redis";
import * as Sentry from "@sentry/node";
import { configDotenv } from "dotenv";
import { RequestWithAuth } from "./types";
+import { crawlGroup } from "../../services/worker/nuq";
configDotenv();
export async function crawlCancelController(
@@ -20,6 +21,15 @@ export async function crawlCancelController(
return res.status(403).json({ error: "Unauthorized" });
}
+ const group = await crawlGroup.getGroup(req.params.jobId);
+ if (!group) {
+ return res.status(404).json({ error: "Job not found" });
+ }
+
+ if (group.status === "completed") {
+ return res.status(409).json({ error: "Crawl is already completed" });
+ }
+
try {
sc.cancelled = true;
await saveCrawl(req.params.jobId, sc);
diff --git a/apps/api/src/controllers/v2/crawl-cancel.ts b/apps/api/src/controllers/v2/crawl-cancel.ts
index a74939fb7..ba561ee6c 100644
--- a/apps/api/src/controllers/v2/crawl-cancel.ts
+++ b/apps/api/src/controllers/v2/crawl-cancel.ts
@@ -4,6 +4,7 @@ import { getCrawl, saveCrawl } from "../../lib/crawl-redis";
import * as Sentry from "@sentry/node";
import { configDotenv } from "dotenv";
import { RequestWithAuth } from "./types";
+import { crawlGroup } from "../../services/worker/nuq";
configDotenv();
export async function crawlCancelController(
@@ -21,6 +22,15 @@ export async function crawlCancelController(
return res.status(403).json({ error: "Unauthorized" });
}
+ const group = await crawlGroup.getGroup(req.params.jobId);
+ if (!group) {
+ return res.status(404).json({ error: "Job not found" });
+ }
+
+ if (group.status === "completed") {
+ return res.status(409).json({ error: "Crawl is already completed" });
+ }
+
try {
sc.cancelled = true;
await saveCrawl(req.params.jobId, sc);