From 6ed3104eb6bd20fdf45e9f1fd64432000a280c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Tue, 22 Oct 2024 20:28:10 +0200 Subject: [PATCH 1/2] feat: clear ACUC cache endpoint based on team ID --- apps/api/src/controllers/auth.ts | 9 ++++++++- .../src/controllers/v0/admin/acuc-cache-clear.ts | 15 +++++++++++++++ apps/api/src/routes/admin.ts | 7 +++++++ apps/api/src/routes/v1.ts | 2 +- 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 apps/api/src/controllers/v0/admin/acuc-cache-clear.ts diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts index 5546bc17..552dd9b4 100644 --- a/apps/api/src/controllers/auth.ts +++ b/apps/api/src/controllers/auth.ts @@ -13,7 +13,7 @@ import { setTraceAttributes } from "@hyperdx/node-opentelemetry"; import { sendNotification } from "../services/notification/email_notification"; import { Logger } from "../lib/logger"; import { redlock } from "../services/redlock"; -import { getValue } from "../services/redis"; +import { deleteKey, getValue } from "../services/redis"; import { setValue } from "../services/redis"; import { validate } from "uuid"; import * as Sentry from "@sentry/node"; @@ -128,6 +128,13 @@ export async function getACUC( } } +export async function clearACUC( + api_key: string, +): Promise { + const cacheKeyACUC = `acuc_${api_key}`; + await deleteKey(cacheKeyACUC); +} + export async function authenticateUser( req, res, diff --git a/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts b/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts new file mode 100644 index 00000000..3ef1f7fb --- /dev/null +++ b/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts @@ -0,0 +1,15 @@ +import { Request, Response } from "express"; +import { supabase_service } from "../../../services/supabase"; +import { clearACUC } from "../../auth"; + +export async function acucCacheClearController(req: Request, res: Response) { + const team_id: string = req.body.team_id; + + const keys = await supabase_service.from("api_keys") + .select("*") + .eq("team_id", team_id); + + await Promise.all(keys.data.map(x => clearACUC(x.key))); + + res.json({ ok: true }); +} diff --git a/apps/api/src/routes/admin.ts b/apps/api/src/routes/admin.ts index 38611eac..88159060 100644 --- a/apps/api/src/routes/admin.ts +++ b/apps/api/src/routes/admin.ts @@ -6,6 +6,8 @@ import { cleanBefore24hCompleteJobsController, queuesController, } from "../controllers/v0/admin/queue"; +import { acucCacheClearController } from "../controllers/v0/admin/acuc-cache-clear"; +import { wrap } from "./v1"; export const adminRouter = express.Router(); @@ -33,3 +35,8 @@ adminRouter.get( `/admin/${process.env.BULL_AUTH_KEY}/autoscaler`, autoscalerController ); + +adminRouter.post( + `/admin/${process.env.BULL_AUTH_KEY}/acuc-cache-clear`, + wrap(acucCacheClearController), +); diff --git a/apps/api/src/routes/v1.ts b/apps/api/src/routes/v1.ts index 246f9b05..880ae885 100644 --- a/apps/api/src/routes/v1.ts +++ b/apps/api/src/routes/v1.ts @@ -94,7 +94,7 @@ function blocklistMiddleware(req: Request, res: Response, next: NextFunction) { next(); } -function wrap(controller: (req: Request, res: Response) => Promise): (req: Request, res: Response, next: NextFunction) => any { +export function wrap(controller: (req: Request, res: Response) => Promise): (req: Request, res: Response, next: NextFunction) => any { return (req, res, next) => { controller(req, res) .catch(err => next(err)) From d375bca167550e762bd8b426d978ee72098fffb6 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 22 Oct 2024 20:42:59 -0300 Subject: [PATCH 2/2] Update acuc-cache-clear.ts --- .../controllers/v0/admin/acuc-cache-clear.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts b/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts index 3ef1f7fb..876ca98a 100644 --- a/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts +++ b/apps/api/src/controllers/v0/admin/acuc-cache-clear.ts @@ -1,15 +1,22 @@ import { Request, Response } from "express"; import { supabase_service } from "../../../services/supabase"; import { clearACUC } from "../../auth"; +import { Logger } from "../../../lib/logger"; export async function acucCacheClearController(req: Request, res: Response) { + try { const team_id: string = req.body.team_id; - const keys = await supabase_service.from("api_keys") - .select("*") - .eq("team_id", team_id); - - await Promise.all(keys.data.map(x => clearACUC(x.key))); + const keys = await supabase_service + .from("api_keys") + .select("*") + .eq("team_id", team_id); + + await Promise.all(keys.data.map((x) => clearACUC(x.key))); res.json({ ok: true }); + } catch (error) { + Logger.error(`Error clearing ACUC cache via API route: ${error}`); + res.status(500).json({ error: "Internal server error" }); + } }