fix: เพิ่ม Graceful Shutdown - ป้องกัน connection in app file, Log Mnddleware + Memory Store

This commit is contained in:
Warunee Tamkoo 2026-01-28 17:22:10 +07:00
parent a194d8594b
commit e068aafe3a
3 changed files with 137 additions and 33 deletions

View file

@ -1,9 +1,6 @@
import { NextFunction, Request, Response } from "express";
import { Client } from "@elastic/elasticsearch";
import { AppDataSource } from "../database/data-source";
import { PosMaster } from "../entities/PosMaster";
import { OrgRevision } from "../entities/OrgRevision";
import { Profile } from "../entities/Profile";
import { logMemoryStore } from "../utils/log-memory-store";
if (!process.env.ELASTICSEARCH_INDEX) {
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
@ -27,9 +24,6 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
if (!req.url.startsWith("/api/")) return next();
let data: any;
const repoPosmaster = AppDataSource.getRepository(PosMaster);
const repoProfile = AppDataSource.getRepository(Profile);
const repoRevision = AppDataSource.getRepository(OrgRevision);
const originalJson = res.json;
@ -42,13 +36,6 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
req.app.locals.logData = {};
const revision = await repoRevision.findOne({
where: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
});
res.on("finish", async () => {
try {
if (!req.url.startsWith("/api/")) return;
@ -69,7 +56,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
if (req.url.startsWith("/api/v1/org/profile/")) system = "registry";
if (req.url.startsWith("/api/v1/org/profile-employee/")) system = "registry";
if (req.url.startsWith("/api/v1/org/profile-temp/")) system = "registry";
if (req.url.startsWith("/api/v1/org/commandType/admin")) system = "admin";
if (req.url.startsWith("/api/v1/org/commandSys/")) system = "admin";
if (req.url.startsWith("/api/v1/org/commandSalary/")) system = "admin";
@ -79,16 +66,15 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
if (req.url.startsWith("/api/v1/org/keycloak/")) system = "registry";
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
const profileByKeycloak = await repoProfile.findOne({
where: { keycloak: req.app.locals.logData.userId },
});
const rootId = await repoPosmaster.findOne({
where: {
current_holderId: profileByKeycloak?.id,
orgRevisionId: revision?.id,
},
select: ["orgRootId"],
});
// Get profile from cache
const profileByKeycloak = await logMemoryStore.getProfileByKeycloak(
req.app.locals.logData.userId,
);
// Get rootId from cache
const rootId = await logMemoryStore.getRootIdByProfileId(profileByKeycloak?.id);
// console.log("ancestorDNA:", rootId);
if (level === 1 && res.statusCode < 500) return;
if (level === 2 && res.statusCode < 400) return;
@ -96,7 +82,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
const obj = {
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
ip: req.ip,
rootId: rootId?.orgRootId ?? null,
rootId: rootId ?? null,
systemName: system,
startTimeStamp: timestamp,
endTimeStamp: new Date().toISOString(),