mirror of
https://git.mirrors.martin98.com/https://github.com/mendableai/firecrawl
synced 2025-08-05 20:16:03 +08:00
Nick:
This commit is contained in:
parent
98a770f38f
commit
0bbb8bb24e
@ -1,87 +0,0 @@
|
|||||||
import { Request, Response } from "express";
|
|
||||||
|
|
||||||
import { Job } from "bull";
|
|
||||||
import { Logger } from "../../lib/logger";
|
|
||||||
import { getWebScraperQueue } from "../../services/queue-service";
|
|
||||||
import { checkAlerts } from "../../services/alerts";
|
|
||||||
|
|
||||||
export async function cleanBefore24hCompleteJobsController(
|
|
||||||
req: Request,
|
|
||||||
res: Response
|
|
||||||
) {
|
|
||||||
Logger.info("🐂 Cleaning jobs older than 24h");
|
|
||||||
try {
|
|
||||||
const webScraperQueue = getWebScraperQueue();
|
|
||||||
const batchSize = 10;
|
|
||||||
const numberOfBatches = 9; // Adjust based on your needs
|
|
||||||
const completedJobsPromises: Promise<Job[]>[] = [];
|
|
||||||
for (let i = 0; i < numberOfBatches; i++) {
|
|
||||||
completedJobsPromises.push(
|
|
||||||
webScraperQueue.getJobs(
|
|
||||||
["completed"],
|
|
||||||
i * batchSize,
|
|
||||||
i * batchSize + batchSize,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const completedJobs: Job[] = (
|
|
||||||
await Promise.all(completedJobsPromises)
|
|
||||||
).flat();
|
|
||||||
const before24hJobs =
|
|
||||||
completedJobs.filter(
|
|
||||||
(job) => job.finishedOn < Date.now() - 24 * 60 * 60 * 1000
|
|
||||||
) || [];
|
|
||||||
|
|
||||||
let count = 0;
|
|
||||||
|
|
||||||
if (!before24hJobs) {
|
|
||||||
return res.status(200).send(`No jobs to remove.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const job of before24hJobs) {
|
|
||||||
try {
|
|
||||||
await job.remove();
|
|
||||||
count++;
|
|
||||||
} catch (jobError) {
|
|
||||||
Logger.error(`🐂 Failed to remove job with ID ${job.id}: ${jobError}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res.status(200).send(`Removed ${count} completed jobs.`);
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(`🐂 Failed to clean last 24h complete jobs: ${error}`);
|
|
||||||
return res.status(500).send("Failed to clean jobs");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use this as a "health check" that way we dont destroy the server
|
|
||||||
export async function queuesController(req: Request, res: Response) {
|
|
||||||
try {
|
|
||||||
const webScraperQueue = getWebScraperQueue();
|
|
||||||
|
|
||||||
const [webScraperActive] = await Promise.all([
|
|
||||||
webScraperQueue.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 });
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,85 +0,0 @@
|
|||||||
import { Request, Response } from "express";
|
|
||||||
import Redis from "ioredis";
|
|
||||||
import { Logger } from "../../lib/logger";
|
|
||||||
import { redisRateLimitClient } from "../../services/rate-limiter";
|
|
||||||
|
|
||||||
export async function redisHealthController(req: Request, res: Response) {
|
|
||||||
const retryOperation = async (operation, retries = 3) => {
|
|
||||||
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
||||||
try {
|
|
||||||
return await operation();
|
|
||||||
} catch (error) {
|
|
||||||
if (attempt === retries) throw error;
|
|
||||||
Logger.warn(`Attempt ${attempt} failed: ${error.message}. Retrying...`);
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 2000)); // Wait 2 seconds before retrying
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const queueRedis = new Redis(process.env.REDIS_URL);
|
|
||||||
|
|
||||||
const testKey = "test";
|
|
||||||
const testValue = "test";
|
|
||||||
|
|
||||||
// Test queueRedis
|
|
||||||
let queueRedisHealth;
|
|
||||||
try {
|
|
||||||
await retryOperation(() => queueRedis.set(testKey, testValue));
|
|
||||||
queueRedisHealth = await retryOperation(() => queueRedis.get(testKey));
|
|
||||||
await retryOperation(() => queueRedis.del(testKey));
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(`queueRedis health check failed: ${error}`);
|
|
||||||
queueRedisHealth = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test redisRateLimitClient
|
|
||||||
let redisRateLimitHealth;
|
|
||||||
try {
|
|
||||||
await retryOperation(() => redisRateLimitClient.set(testKey, testValue));
|
|
||||||
redisRateLimitHealth = await retryOperation(() =>
|
|
||||||
redisRateLimitClient.get(testKey)
|
|
||||||
);
|
|
||||||
await retryOperation(() => redisRateLimitClient.del(testKey));
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(`redisRateLimitClient health check failed: ${error}`);
|
|
||||||
redisRateLimitHealth = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const healthStatus = {
|
|
||||||
queueRedis: queueRedisHealth === testValue ? "healthy" : "unhealthy",
|
|
||||||
redisRateLimitClient:
|
|
||||||
redisRateLimitHealth === testValue ? "healthy" : "unhealthy",
|
|
||||||
};
|
|
||||||
|
|
||||||
if (
|
|
||||||
healthStatus.queueRedis === "healthy" &&
|
|
||||||
healthStatus.redisRateLimitClient === "healthy"
|
|
||||||
) {
|
|
||||||
Logger.info("Both Redis instances are healthy");
|
|
||||||
return res.status(200).json({ status: "healthy", details: healthStatus });
|
|
||||||
} else {
|
|
||||||
Logger.info(
|
|
||||||
`Redis instances health check: ${JSON.stringify(healthStatus)}`
|
|
||||||
);
|
|
||||||
// await sendSlackWebhook(
|
|
||||||
// `[REDIS DOWN] Redis instances health check: ${JSON.stringify(
|
|
||||||
// healthStatus
|
|
||||||
// )}`,
|
|
||||||
// true
|
|
||||||
// );
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({ status: "unhealthy", details: healthStatus });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(`Redis health check failed: ${error}`);
|
|
||||||
// await sendSlackWebhook(
|
|
||||||
// `[REDIS DOWN] Redis instances health check: ${error.message}`,
|
|
||||||
// true
|
|
||||||
// );
|
|
||||||
return res
|
|
||||||
.status(500)
|
|
||||||
.json({ status: "unhealthy", message: error.message });
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user