Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

Merge branch 'main' into feat/queue-scrapes

Gergő Móricz5fc7fcb77c64
diff --cc apps/api/src/services/queue-worker.ts
index e660378b0,cc92b3ab1..d1372e2c8
--- a/apps/api/src/services/queue-worker.ts
+++ b/apps/api/src/services/queue-worker.ts
@@@ -25,99 -16,14 +25,99 @@@ if (process.env.ENV === "production")
additionalInstrumentations: [],
});
}
+const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+const workerLockDuration = Number(process.env.WORKER_LOCK_DURATION) || 60000;
+const workerStalledCheckInterval =
+ Number(process.env.WORKER_STALLED_CHECK_INTERVAL) || 30000;
+const jobLockExtendInterval =
+ Number(process.env.JOB_LOCK_EXTEND_INTERVAL) || 15000;
+const jobLockExtensionTime =
+ Number(process.env.JOB_LOCK_EXTENSION_TIME) || 15000;
+
+const cantAcceptConnectionInterval =
+ Number(process.env.CANT_ACCEPT_CONNECTION_INTERVAL) || 2000;
+const connectionMonitorInterval =
+ Number(process.env.CONNECTION_MONITOR_INTERVAL) || 10;
+const gotJobInterval = Number(process.env.CONNECTION_MONITOR_INTERVAL) || 20;
const wsq = getWebScraperQueue();
+const sq = getScrapeQueue();
+
+const processJobInternal = async (token: string, job: Job) => {
+ const extendLockInterval = setInterval(async () => {
+ await job.extendLock(token, jobLockExtensionTime);
+ }, jobLockExtendInterval);
+
+ try {
+ const result = await processJob(job, token);
+ const jobState = await job.getState();
+ if(jobState !== "completed" && jobState !== "failed"){
+ try{
+ await job.moveToCompleted(result.docs, token, false); //3rd arg fetchNext
+ }catch(e){
+ // console.log("Job already completed, error:", e);
+ }
+ }
+ } catch (error) {
+ console.log("Job failed, error:", error);
+
+ await job.moveToFailed(error, token, false);
+ } finally {
+ clearInterval(extendLockInterval);
+ }
+};
+
+let isShuttingDown = false;
+
+process.on("SIGINT", () => {
+ console.log("Received SIGINT. Shutting down gracefully...");
+ isShuttingDown = true;
+});
+
+const workerFun = async (queueName: string, processJobInternal: (token: string, job: Job) => Promise<void>) => {
+ const worker = new Worker(queueName, null, {
+ connection: redisConnection,
+ lockDuration: 1 * 60 * 1000, // 1 minute
+ // lockRenewTime: 15 * 1000, // 15 seconds
+ stalledInterval: 30 * 1000, // 30 seconds
+ maxStalledCount: 10, // 10 times
+ });
+
+ worker.startStalledCheckTimer();
+
+ const monitor = await systemMonitor;
+
+ while (true) {
+ if (isShuttingDown) {
+ console.log("No longer accepting new jobs. SIGINT");
+ break;
+ }
+ const token = uuidv4();
+ const canAcceptConnection = await monitor.acceptConnection();
+ if (!canAcceptConnection) {
+ console.log("Cant accept connection");
+ await sleep(cantAcceptConnectionInterval); // more sleep
+ continue;
+ }
+
+ const job = await worker.getNextJob(token);
+ if (job) {
+ processJobInternal(token, job);
+ await sleep(gotJobInterval);
+ } else {
+ await sleep(connectionMonitorInterval);
+ }
+ }
+};
+
+workerFun(webScraperQueueName, processJobInternal);
+workerFun(scrapeQueueName, processJobInternal);
-async function processJob(job: Job, done) {
+async function processJob(job: Job, token: string) {
- Logger.debug(`🐂 Worker taking job ${job.id}`);
+ Logger.info(`🐂 Worker taking job ${job.id}`);
try {
- job.progress({
+ job.updateProgress({
current: 1,
total: 100,
current_step: "SCRAPING",
@@@ -172,11 -61,11 +172,11 @@@
pageOptions: job.data.pageOptions,
origin: job.data.origin,
});
- Logger.debug(`🐂 Job done ${job.id}`);
+ Logger.info(`🐂 Job done ${job.id}`);
- done(null, data);
+ return data;
} catch (error) {
Logger.error(`🐂 Job errored ${job.id} - ${error}`);
- if (await getWebScraperQueue().isPaused(false)) {
+ if (await getWebScraperQueue().isPaused()) {
Logger.debug("🐂Queue is paused, ignoring");
return;
}