2024-04-20 16:38:05 -07:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { authenticateUser } from "./auth";
|
|
|
|
import { RateLimiterMode } from "../../src/types";
|
|
|
|
import { addWebScraperJob } from "../../src/services/queue-jobs";
|
|
|
|
import { getWebScraperQueue } from "../../src/services/queue-service";
|
2024-06-26 18:23:28 -03:00
|
|
|
import { supabaseGetJobById } from "../../src/lib/supabase-jobs";
|
2024-07-25 09:48:06 -03:00
|
|
|
import { Logger } from "../../src/lib/logger";
|
2024-04-20 16:38:05 -07:00
|
|
|
|
|
|
|
export async function crawlStatusController(req: Request, res: Response) {
|
|
|
|
try {
|
|
|
|
const { success, team_id, error, status } = await authenticateUser(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
RateLimiterMode.CrawlStatus
|
|
|
|
);
|
|
|
|
if (!success) {
|
|
|
|
return res.status(status).json({ error });
|
|
|
|
}
|
|
|
|
const job = await getWebScraperQueue().getJob(req.params.jobId);
|
|
|
|
if (!job) {
|
|
|
|
return res.status(404).json({ error: "Job not found" });
|
|
|
|
}
|
|
|
|
|
2024-08-06 16:26:46 +02:00
|
|
|
const isCancelled = await (await getWebScraperQueue().client).exists("cancelled:" + req.params.jobId);
|
|
|
|
|
|
|
|
if (isCancelled) {
|
|
|
|
return res.json({
|
2024-08-06 16:35:55 +02:00
|
|
|
status: "failed",
|
2024-08-06 17:06:21 +02:00
|
|
|
data: null,
|
|
|
|
partial_data: [],
|
2024-08-06 16:26:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-30 13:27:23 -04:00
|
|
|
let progress = job.progress;
|
|
|
|
if(typeof progress !== 'object') {
|
|
|
|
progress = {
|
|
|
|
current: 0,
|
|
|
|
current_url: '',
|
|
|
|
total: 0,
|
|
|
|
current_step: '',
|
|
|
|
partialDocs: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const {
|
|
|
|
current = 0,
|
|
|
|
current_url = '',
|
|
|
|
total = 0,
|
|
|
|
current_step = '',
|
|
|
|
partialDocs = []
|
|
|
|
} = progress as { current: number, current_url: string, total: number, current_step: string, partialDocs: any[] };
|
2024-06-26 18:23:28 -03:00
|
|
|
|
|
|
|
let data = job.returnvalue;
|
2024-07-13 03:50:46 +09:00
|
|
|
if (process.env.USE_DB_AUTHENTICATION === "true") {
|
2024-06-26 18:23:28 -03:00
|
|
|
const supabaseData = await supabaseGetJobById(req.params.jobId);
|
|
|
|
|
|
|
|
if (supabaseData) {
|
|
|
|
data = supabaseData.docs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobStatus = await job.getState();
|
|
|
|
|
2024-04-20 16:38:05 -07:00
|
|
|
res.json({
|
2024-06-26 18:23:28 -03:00
|
|
|
status: jobStatus,
|
2024-04-20 16:38:05 -07:00
|
|
|
// progress: job.progress(),
|
2024-06-26 18:23:28 -03:00
|
|
|
current,
|
|
|
|
current_url,
|
|
|
|
current_step,
|
|
|
|
total,
|
2024-06-27 16:00:45 -03:00
|
|
|
data: data ? data : null,
|
2024-06-26 18:23:28 -03:00
|
|
|
partial_data: jobStatus == 'completed' ? [] : partialDocs,
|
2024-04-20 16:38:05 -07:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-07-25 09:48:06 -03:00
|
|
|
Logger.error(error);
|
2024-04-20 16:38:05 -07:00
|
|
|
return res.status(500).json({ error: error.message });
|
|
|
|
}
|
|
|
|
}
|