Files
firecrawl/apps/api/src/lib/scrape-events.ts
T

71 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-07-24 18:44:14 +02:00
import { Job, JobId } from "bull";
2024-07-24 14:31:25 +02:00
import type { baseScrapers } from "../scraper/WebScraper/single_url";
import { supabase_service as supabase } from "../services/supabase";
export type ScrapeErrorEvent = {
type: "error",
message: string,
stack?: string,
}
export type ScrapeScrapeEvent = {
type: "scrape",
url: string,
worker?: string,
2024-07-24 14:31:25 +02:00
method: (typeof baseScrapers)[number],
result: null | {
success: boolean,
response_code?: number,
response_size?: number,
2024-07-24 14:46:41 +02:00
error?: string | object,
2024-07-24 14:31:25 +02:00
// proxy?: string,
time_taken: number,
},
}
export type ScrapeQueueEvent = {
type: "queue",
2024-07-24 18:44:14 +02:00
event: "waiting" | "active" | "completed" | "paused" | "resumed" | "removed",
2024-07-24 14:31:25 +02:00
worker?: string,
}
export type ScrapeEvent = ScrapeErrorEvent | ScrapeScrapeEvent | ScrapeQueueEvent;
export class ScrapeEvents {
static async insert(jobId: string, content: ScrapeEvent) {
if (jobId === "TEST") return null;
2024-07-24 14:46:41 +02:00
if (process.env.USE_DB_AUTHENTICATION) {
2024-07-24 14:31:25 +02:00
const result = await supabase.from("scrape_events").insert({
job_id: jobId,
type: content.type,
content: content,
// created_at
2024-07-24 14:46:41 +02:00
}).select().single();
2024-07-24 14:31:25 +02:00
return (result.data as any).id;
}
return null;
}
static async updateScrapeResult(logId: number | null, result: ScrapeScrapeEvent["result"]) {
if (logId === null) return;
const previousLog = (await supabase.from("scrape_events").select().eq("id", logId).single()).data as any;
await supabase.from("scrape_events").update({
content: {
...previousLog.content,
result,
}
}).eq("id", logId);
}
2024-07-24 18:44:14 +02:00
static async logJobEvent(job: Job | JobId, event: ScrapeQueueEvent["event"]) {
await this.insert(((job as any).id ? (job as any).id : job) as string, {
type: "queue",
event,
worker: process.env.FLY_MACHINE_ID,
});
}
2024-07-24 14:31:25 +02:00
}