2024-10-01 16:04:39 -03:00
|
|
|
import {
|
|
|
|
|
ConcurrencyCheckParams,
|
|
|
|
|
ConcurrencyCheckResponse,
|
2024-12-11 19:51:08 -03:00
|
|
|
RequestWithAuth,
|
2024-10-01 16:04:39 -03:00
|
|
|
} from "./types";
|
|
|
|
|
import { Response } from "express";
|
2025-05-28 09:58:04 +02:00
|
|
|
import { redisEvictConnection } from "../../../src/services/redis";
|
2025-01-30 13:47:29 -03:00
|
|
|
|
2024-10-01 16:04:39 -03:00
|
|
|
// Basically just middleware and error wrapping
|
|
|
|
|
export async function concurrencyCheckController(
|
|
|
|
|
req: RequestWithAuth<ConcurrencyCheckParams, undefined, undefined>,
|
2024-12-11 19:51:08 -03:00
|
|
|
res: Response<ConcurrencyCheckResponse>,
|
2024-10-01 16:04:39 -03:00
|
|
|
) {
|
2024-10-01 16:11:12 -03:00
|
|
|
const concurrencyLimiterKey = "concurrency-limiter:" + req.auth.team_id;
|
2024-10-01 16:04:39 -03:00
|
|
|
const now = Date.now();
|
2025-05-28 09:58:04 +02:00
|
|
|
const activeJobsOfTeam = await redisEvictConnection.zrangebyscore(
|
2024-10-01 16:04:39 -03:00
|
|
|
concurrencyLimiterKey,
|
|
|
|
|
now,
|
2024-12-11 19:51:08 -03:00
|
|
|
Infinity,
|
2024-10-01 16:04:39 -03:00
|
|
|
);
|
2025-01-30 13:47:29 -03:00
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
concurrency: activeJobsOfTeam.length,
|
2025-04-10 18:49:23 +02:00
|
|
|
maxConcurrency: req.acuc.concurrency,
|
2025-01-30 13:47:29 -03:00
|
|
|
});
|
2024-10-01 16:04:39 -03:00
|
|
|
}
|