From e69320005cdcf9a48a892d9b684497ec598873c5 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Thu, 9 Jul 2026 21:45:30 +0700 Subject: [PATCH] fixed service stop working --- src/app.ts | 18 ++++++++++++++++++ src/controllers/PermissionController.ts | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index c2cbe5f7..2a75e6fe 100644 --- a/src/app.ts +++ b/src/app.ts @@ -21,6 +21,24 @@ import { DateSerializer } from "./interfaces/date-serializer"; import { initWebSocket } from "./services/webSocket"; import { RetirementService } from "./services/RetirementService"; +// ── Process-level safety nets ────────────────────────────────────────────── +// ตาข่ายกัน service ตายจาก promise ที่ reject แล้วไม่ถูกจับ / exception ที่หลุด +// ออกมา (เช่น bug เรียก async โดยไม่ await ใน PermissionController). +// ลงทะเบียนไว้ให้เร็วที่สุด ก่อนที่ main() จะเริ่มทำงาน +process.on("unhandledRejection", (reason) => { + console.error( + "[APP][unhandledRejection] A promise was rejected but never caught:", + reason, + ); +}); +process.on("uncaughtException", (err) => { + // คง service ไว้เพื่อ availability สูงสุด + // หมายเหตุ: process อาจอยู่ในสถานะไม่สม่ำเสมอ ควร monitor log นี้และ restart + // ในช่วงเวลาที่เหมาะสม ถ้าต้องการ crash-on-error ให้เปลี่ยนเป็น graceful + // shutdown + process.exit(1) ตรงนี้ + console.error("[APP][uncaughtException] An exception escaped all handlers:", err); +}); + async function main() { await AppDataSource.initialize(); diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 44747b09..f7a22517 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -810,7 +810,7 @@ export class PermissionController extends Controller { }); const getAsync = promisify(redisClient.get).bind(redisClient); - let org = this.PermissionOrg(request, system, action); + let org = await this.PermissionOrg(request, system, action); let reply = await getAsync("user_" + id); if (reply != null) { reply = JSON.parse(reply);