2026-06-03 13:28:15 -07:00
|
|
|
import { IJobQueue } from "@/service/index.js";
|
|
|
|
|
import { getQueueJobsQueryValidation } from "@/validation/queueValidation.js";
|
2026-01-09 15:07:02 -08:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
|
|
|
|
|
|
const SERVICE_NAME = "JobQueueController";
|
|
|
|
|
|
2026-03-20 11:43:37 -07:00
|
|
|
export interface IJobQueueController {
|
|
|
|
|
getMetrics(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
|
|
|
getJobs(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
|
|
|
|
getAllMetrics(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
|
|
|
|
flushQueue(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JobQueueController implements IJobQueueController {
|
2026-01-09 15:07:02 -08:00
|
|
|
static SERVICE_NAME = SERVICE_NAME;
|
2026-06-03 13:28:15 -07:00
|
|
|
private jobQueue: IJobQueue;
|
|
|
|
|
constructor(jobQueue: IJobQueue) {
|
2026-01-09 15:07:02 -08:00
|
|
|
this.jobQueue = jobQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get serviceName() {
|
|
|
|
|
return JobQueueController.SERVICE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
|
|
|
|
const metrics = await this.jobQueue.getMetrics();
|
|
|
|
|
res.status(200).json({
|
2026-01-09 15:32:53 -08:00
|
|
|
success: true,
|
2026-01-09 15:07:02 -08:00
|
|
|
msg: "Queue metrics fetched successfully",
|
|
|
|
|
data: metrics,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getJobs = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2026-06-03 13:28:15 -07:00
|
|
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
|
|
|
|
const { jobs, count } = await this.jobQueue.getJobs(pagination);
|
2026-01-09 15:07:02 -08:00
|
|
|
return res.status(200).json({
|
2026-01-09 15:32:53 -08:00
|
|
|
success: true,
|
2026-01-09 15:07:02 -08:00
|
|
|
msg: "Queue jobs fetched successfully",
|
2026-06-03 13:28:15 -07:00
|
|
|
data: { jobs, count },
|
2026-01-09 15:07:02 -08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
2026-06-03 13:28:15 -07:00
|
|
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
|
|
|
|
const { jobs, count } = await this.jobQueue.getJobs(pagination);
|
2026-01-09 15:07:02 -08:00
|
|
|
const metrics = await this.jobQueue.getMetrics();
|
|
|
|
|
return res.status(200).json({
|
2026-01-09 15:32:53 -08:00
|
|
|
success: true,
|
2026-01-09 15:07:02 -08:00
|
|
|
msg: "Queue metrics fetched successfully",
|
2026-06-03 13:28:15 -07:00
|
|
|
data: { jobs, count, metrics },
|
2026-01-09 15:07:02 -08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
flushQueue = async (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await this.jobQueue.flushQueues();
|
|
|
|
|
return res.status(200).json({
|
2026-01-09 15:32:53 -08:00
|
|
|
success: true,
|
2026-01-09 15:07:02 -08:00
|
|
|
msg: "Queue flushed successfully",
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
export default JobQueueController;
|