Files
firecrawl/apps/api/src/controllers/status.ts
T

60 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-20 16:38:05 -07:00
import { Request, Response } from "express";
import { getWebScraperQueue } from "../../src/services/queue-service";
2024-06-27 16:00:45 -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 crawlJobStatusPreviewController(req: Request, res: Response) {
try {
const job = await getWebScraperQueue().getJob(req.params.jobId);
if (!job) {
return res.status(404).json({ error: "Job not found" });
}
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-27 16:00:45 -03:00
let data = job.returnvalue;
2024-07-13 03:50:46 +09:00
if (process.env.USE_DB_AUTHENTICATION === "true") {
2024-06-27 16:00:45 -03:00
const supabaseData = await supabaseGetJobById(req.params.jobId);
if (supabaseData) {
data = supabaseData.docs;
}
}
2024-07-18 13:19:44 -04:00
let jobStatus = await job.getState();
2024-07-30 13:27:23 -04:00
if (jobStatus === 'waiting' || jobStatus === 'delayed' || jobStatus === 'waiting-children' || jobStatus === 'unknown' || jobStatus === 'prioritized') {
2024-07-18 13:19:44 -04:00
jobStatus = 'active';
}
2024-06-27 16:00:45 -03:00
2024-04-20 16:38:05 -07:00
res.json({
2024-06-27 16:00:45 -03:00
status: jobStatus,
2024-04-20 16:38:05 -07:00
// progress: job.progress(),
2024-06-27 16:00:45 -03:00
current,
current_url,
current_step,
total,
data: data ? data : null,
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 });
}
}