Spaces:
Sleeping
Sleeping
File size: 3,536 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import { v7 as uuidv7 } from "uuid";
import { config } from "../../config";
import {
Document,
ScrapeOptions,
TeamFlags,
URLTrace,
scrapeOptions as scrapeOptionsSchema,
} from "../../controllers/v2/types";
import { waitForJob } from "../../services/queue-jobs";
import { addScrapeJob } from "../../services/queue-jobs";
import { getJobPriority } from "../job-priority";
import type { Logger } from "winston";
import { isUrlBlocked } from "../../scraper/WebScraper/utils/blocklist";
import { scrapeQueue } from "../../services/worker/nuq";
interface ScrapeDocumentOptions {
url: string;
teamId: string;
origin: string;
timeout: number;
isSingleUrl?: boolean;
flags: TeamFlags | null;
apiKeyId: number | null;
requestId?: string;
}
export async function scrapeDocument(
options: ScrapeDocumentOptions,
urlTraces: URLTrace[],
logger: Logger,
internalScrapeOptions: Partial<ScrapeOptions> = { onlyMainContent: false },
): Promise<Document | null> {
const trace = urlTraces.find(t => t.url === options.url);
if (trace) {
trace.status = "scraped";
trace.timing.scrapedAt = new Date().toISOString();
}
if (
isUrlBlocked(options.url, options.flags ?? null, {
team_id: options.teamId,
origin: options.origin,
})
) {
return null;
}
async function attemptScrape(timeout: number) {
const jobId = uuidv7();
const jobPriority = await getJobPriority({
team_id: options.teamId,
basePriority: 10,
});
await addScrapeJob(
{
url: options.url,
mode: "single_urls",
team_id: options.teamId,
scrapeOptions: scrapeOptionsSchema.parse({
...internalScrapeOptions,
maxAge: 4 * 60 * 60 * 1000,
}),
internalOptions: {
teamId: options.teamId,
saveScrapeResultToGCS: config.GCS_FIRE_ENGINE_BUCKET_NAME
? true
: false,
bypassBilling: true,
},
origin: options.origin,
is_scrape: true,
from_extract: true,
startTime: Date.now(),
zeroDataRetention: false, // not supported
apiKeyId: options.apiKeyId,
requestId: options.requestId,
},
jobId,
jobPriority,
false,
true,
);
const doc = await waitForJob(jobId, timeout, false, logger);
try {
await scrapeQueue.removeJob(jobId, logger);
} catch (error) {
logger.warn("Error removing job from queue", { error, scrapeId: jobId });
}
if (trace) {
trace.timing.completedAt = new Date().toISOString();
trace.contentStats = {
rawContentLength: doc.markdown?.length || 0,
processedContentLength: doc.markdown?.length || 0,
tokensUsed: 0,
};
}
return doc;
}
try {
try {
logger.debug("Attempting scrape...");
const x = await attemptScrape(options.timeout);
logger.debug("Scrape finished!");
return x;
} catch (timeoutError) {
logger.warn("Scrape failed.", { error: timeoutError });
if (options.isSingleUrl) {
// For single URLs, try again with double timeout
logger.debug("Attempting scrape...");
const x = await attemptScrape(options.timeout * 2);
logger.debug("Scrape finished!");
return x;
}
throw timeoutError;
}
} catch (error) {
logger.error(`error in scrapeDocument`, { error });
if (trace) {
trace.status = "error";
trace.error = error.message;
}
return null;
}
}
|