From 0bbb8bb24ebcbd8d3f2f518415e44dc5cee829a6 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 26 Aug 2024 17:17:12 -0300 Subject: [PATCH] Nick: --- .../api/src/controllers/v1/admin/queue.ts.WIP | 87 ------------------- .../controllers/v1/admin/redis-health.ts.WIP | 85 ------------------ 2 files changed, 172 deletions(-) delete mode 100644 apps/api/src/controllers/v1/admin/queue.ts.WIP delete mode 100644 apps/api/src/controllers/v1/admin/redis-health.ts.WIP diff --git a/apps/api/src/controllers/v1/admin/queue.ts.WIP b/apps/api/src/controllers/v1/admin/queue.ts.WIP deleted file mode 100644 index cb5f99ed..00000000 --- a/apps/api/src/controllers/v1/admin/queue.ts.WIP +++ /dev/null @@ -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[] = []; - 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 }); - } - } \ No newline at end of file diff --git a/apps/api/src/controllers/v1/admin/redis-health.ts.WIP b/apps/api/src/controllers/v1/admin/redis-health.ts.WIP deleted file mode 100644 index 3b1e2518..00000000 --- a/apps/api/src/controllers/v1/admin/redis-health.ts.WIP +++ /dev/null @@ -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 }); - } -}