Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. πŸ”₯

Merge branch 'main' into v1-webscraper

Nicolas4d0acc9722b6
diff --cc apps/api/src/controllers/v0/admin/queue.ts
index a5b23e333,06844bead..717480026
--- a/apps/api/src/controllers/v0/admin/queue.ts
+++ b/apps/api/src/controllers/v0/admin/queue.ts
@@@ -1,9 -1,11 +1,10 @@@
import { Request, Response } from "express";
import { Job } from "bullmq";
-import { Logger } from "../../lib/logger";
-import { getScrapeQueue } from "../../services/queue-service";
-import { checkAlerts } from "../../services/alerts";
-import { exec } from "node:child_process";
-import { sendSlackWebhook } from "../../services/alerts/slack";
+import { Logger } from "../../../lib/logger";
+import { getScrapeQueue } from "../../../services/queue-service";
+import { checkAlerts } from "../../../services/alerts";
++import { sendSlackWebhook } from "../../../services/alerts/slack";
export async function cleanBefore24hCompleteJobsController(
req: Request,
@@@ -54,34 -56,109 +55,145 @@@
}
}
-
export async function checkQueuesController(req: Request, res: Response) {
- try {
- await checkAlerts();
- return res.status(200).send("Alerts initialized");
- } catch (error) {
- Logger.debug(`Failed to initialize alerts: ${error}`);
- return res.status(500).send("Failed to initialize alerts");
- }
+ try {
+ await checkAlerts();
+ return res.status(200).send("Alerts initialized");
+ } catch (error) {
+ Logger.debug(`Failed to initialize alerts: ${error}`);
+ return res.status(500).send("Failed to initialize alerts");
}
+ }
- // Use this as a "health check" that way we dont destroy the server
+ // Use this as a "health check" that way we dont destroy the server
export async function queuesController(req: Request, res: Response) {
- try {
- const scrapeQueue = getScrapeQueue();
+ try {
+ const scrapeQueue = getScrapeQueue();
+
+ const [webScraperActive] = await Promise.all([
+ scrapeQueue.getActiveCount(),
+ ]);
+
+ const noActiveJobs = webScraperActive === 0;
+ // 200 if no active jobs, 503 if there are active jobs
+ return res.status(noActiveJobs ? 200 : 500).json({
+ webScraperActive,
+ noActiveJobs,
+ });
+ } catch (error) {
+ Logger.error(error);
+ return res.status(500).json({ error: error.message });
+ }
+ }
+
+ export async function autoscalerController(req: Request, res: Response) {
+ try {
+ const maxNumberOfMachines = 80;
+ const minNumberOfMachines = 20;
+
+ const scrapeQueue = getScrapeQueue();
- const [webScraperActive] = await Promise.all([
- const [webScraperActive, webScraperWaiting, webScraperPriority] = await Promise.all([
- scrapeQueue.getActiveCount(),
- scrapeQueue.getWaitingCount(),
- scrapeQueue.getPrioritizedCount(),
- ]);
++ const [webScraperActive, webScraperWaiting, webScraperPriority] =
++ await Promise.all([
+ scrapeQueue.getActiveCount(),
++ scrapeQueue.getWaitingCount(),
++ scrapeQueue.getPrioritizedCount(),
+ ]);
- const noActiveJobs = webScraperActive === 0;
- // 200 if no active jobs, 503 if there are active jobs
- return res.status(noActiveJobs ? 200 : 500).json({
- webScraperActive,
- noActiveJobs,
+ let waitingAndPriorityCount = webScraperWaiting + webScraperPriority;
+
+ // get number of machines active
- const request = await fetch('https://api.machines.dev/v1/apps/firecrawl-scraper-js/machines',
++ const request = await fetch(
++ "https://api.machines.dev/v1/apps/firecrawl-scraper-js/machines",
+ {
+ headers: {
- 'Authorization': `Bearer ${process.env.FLY_API_TOKEN}`
- }
++ Authorization: `Bearer ${process.env.FLY_API_TOKEN}`,
++ },
+ }
- )
++ );
+ const machines = await request.json();
+
+ // Only worker machines
- const activeMachines = machines.filter(machine => (machine.state === 'started' || machine.state === "starting" || machine.state === "replacing") && machine.config.env["FLY_PROCESS_GROUP"] === "worker").length;
++ const activeMachines = machines.filter(
++ (machine) =>
++ (machine.state === "started" ||
++ machine.state === "starting" ||
++ machine.state === "replacing") &&
++ machine.config.env["FLY_PROCESS_GROUP"] === "worker"
++ ).length;
+
+ let targetMachineCount = activeMachines;
+
+ const baseScaleUp = 10;
+ // Slow scale down
+ const baseScaleDown = 2;
+
+ // Scale up logic
+ if (webScraperActive > 9000 || waitingAndPriorityCount > 2000) {
- targetMachineCount = Math.min(maxNumberOfMachines, activeMachines + (baseScaleUp * 3));
++ targetMachineCount = Math.min(
++ maxNumberOfMachines,
++ activeMachines + baseScaleUp * 3
++ );
+ } else if (webScraperActive > 5000 || waitingAndPriorityCount > 1000) {
- targetMachineCount = Math.min(maxNumberOfMachines, activeMachines + (baseScaleUp * 2));
++ targetMachineCount = Math.min(
++ maxNumberOfMachines,
++ activeMachines + baseScaleUp * 2
++ );
+ } else if (webScraperActive > 1000 || waitingAndPriorityCount > 500) {
- targetMachineCount = Math.min(maxNumberOfMachines, activeMachines + baseScaleUp);
++ targetMachineCount = Math.min(
++ maxNumberOfMachines,
++ activeMachines + baseScaleUp
++ );
+ }
+
+ // Scale down logic
+ if (webScraperActive < 100 && waitingAndPriorityCount < 50) {
- targetMachineCount = Math.max(minNumberOfMachines, activeMachines - (baseScaleDown * 3));
++ targetMachineCount = Math.max(
++ minNumberOfMachines,
++ activeMachines - baseScaleDown * 3
++ );
+ } else if (webScraperActive < 500 && waitingAndPriorityCount < 200) {
- targetMachineCount = Math.max(minNumberOfMachines, activeMachines - (baseScaleDown * 2));
++ targetMachineCount = Math.max(
++ minNumberOfMachines,
++ activeMachines - baseScaleDown * 2
++ );
+ } else if (webScraperActive < 1000 && waitingAndPriorityCount < 500) {
- targetMachineCount = Math.max(minNumberOfMachines, activeMachines - baseScaleDown);
++ targetMachineCount = Math.max(
++ minNumberOfMachines,
++ activeMachines - baseScaleDown
++ );
+ }
+
+ if (targetMachineCount !== activeMachines) {
- Logger.info(`πŸ‚ Scaling from ${activeMachines} to ${targetMachineCount} - ${webScraperActive} active, ${webScraperWaiting} waiting`);
++ Logger.info(
++ `πŸ‚ Scaling from ${activeMachines} to ${targetMachineCount} - ${webScraperActive} active, ${webScraperWaiting} waiting`
++ );
+
- if(targetMachineCount > activeMachines) {
- sendSlackWebhook(`πŸ‚ Scaling from ${activeMachines} to ${targetMachineCount} - ${webScraperActive} active, ${webScraperWaiting} waiting - Current DateTime: ${new Date().toISOString()}`, false, process.env.SLACK_AUTOSCALER ?? "");
++ if (targetMachineCount > activeMachines) {
++ sendSlackWebhook(
++ `πŸ‚ Scaling from ${activeMachines} to ${targetMachineCount} - ${webScraperActive} active, ${webScraperWaiting} waiting - Current DateTime: ${new Date().toISOString()}`,
++ false,
++ process.env.SLACK_AUTOSCALER ?? ""
++ );
+ } else {
- sendSlackWebhook(`πŸ‚ Scaling from ${activeMachines} to ${targetMachineCount} - ${webScraperActive} active, ${webScraperWaiting} waiting - Current DateTime: ${new Date().toISOString()}`, false, process.env.SLACK_AUTOSCALER ?? "");
++ sendSlackWebhook(
++ `πŸ‚ Scaling from ${activeMachines} to ${targetMachineCount} - ${webScraperActive} active, ${webScraperWaiting} waiting - Current DateTime: ${new Date().toISOString()}`,
++ false,
++ process.env.SLACK_AUTOSCALER ?? ""
++ );
+ }
+ return res.status(200).json({
+ mode: "scale-descale",
+ count: targetMachineCount,
});
- } catch (error) {
- Logger.error(error);
- return res.status(500).json({ error: error.message });
}
- }
+
+ return res.status(200).json({
+ mode: "normal",
+ count: activeMachines,
+ });
+ } catch (error) {
+ Logger.error(error);
+ return res.status(500).send("Failed to initialize autoscaler");
+ }
+ }
diff --cc apps/api/src/routes/admin.ts
index d8f1b1e40,d32808ce9..38611eac3
--- a/apps/api/src/routes/admin.ts
+++ b/apps/api/src/routes/admin.ts
@@@ -1,6 -1,7 +1,7 @@@
import express from "express";
-import { redisHealthController } from "../controllers/admin/redis-health";
+import { redisHealthController } from "../controllers/v0/admin/redis-health";
import {
+ autoscalerController,
checkQueuesController,
cleanBefore24hCompleteJobsController,
queuesController,