Files
firecrawl/apps/api/src/controllers/v0/crawl-cancel.ts
T

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-05-06 17:16:43 -07:00
import { Request, Response } from "express";
import { authenticateUser } from "./auth";
2024-08-15 21:51:59 +02:00
import { RateLimiterMode } from "../../../src/types";
import { supabase_service } from "../../../src/services/supabase";
import { Logger } from "../../../src/lib/logger";
import { getCrawl, saveCrawl } from "../../../src/lib/crawl-redis";
import * as Sentry from "@sentry/node";
2024-05-06 17:16:43 -07:00
export async function crawlCancelController(req: Request, res: Response) {
try {
2024-07-07 15:06:31 +02:00
const useDbAuthentication = process.env.USE_DB_AUTHENTICATION === 'true';
2024-05-06 17:16:43 -07:00
const { success, team_id, error, status } = await authenticateUser(
req,
res,
RateLimiterMode.CrawlStatus
);
if (!success) {
return res.status(status).json({ error });
}
2024-08-13 20:51:43 +02:00
const sc = await getCrawl(req.params.jobId);
if (!sc) {
2024-05-06 17:16:43 -07:00
return res.status(404).json({ error: "Job not found" });
}
// check if the job belongs to the team
2024-07-07 15:06:31 +02:00
if (useDbAuthentication) {
const { data, error: supaError } = await supabase_service
.from("bulljobs_teams")
.select("*")
.eq("job_id", req.params.jobId)
.eq("team_id", team_id);
if (supaError) {
return res.status(500).json({ error: supaError.message });
}
2024-05-06 17:16:43 -07:00
2024-07-07 15:06:31 +02:00
if (data.length === 0) {
return res.status(403).json({ error: "Unauthorized" });
}
2024-05-06 17:16:43 -07:00
}
2024-07-07 15:06:31 +02:00
2024-05-06 17:16:43 -07:00
try {
2024-08-13 20:51:43 +02:00
sc.cancelled = true;
await saveCrawl(req.params.jobId, sc);
2024-05-06 17:16:43 -07:00
} catch (error) {
2024-07-23 17:30:46 -03:00
Logger.error(error);
2024-05-06 17:16:43 -07:00
}
res.json({
2024-07-18 13:43:03 -04:00
status: "cancelled"
2024-05-06 17:16:43 -07:00
});
} catch (error) {
Sentry.captureException(error);
2024-07-23 17:30:46 -03:00
Logger.error(error);
2024-05-06 17:16:43 -07:00
return res.status(500).json({ error: error.message });
}
}