forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (52 loc) · 1.74 KB
/
Copy pathserver.js
File metadata and controls
65 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import app from "./app.js";
import logger from "./src/config/logger.js";
import { initRedis, closeRedis } from "./src/config/redis.js";
import { startJobs, stopJobs } from "./src/jobs/queue.js";
import "./src/jobs/handlers.js";
const PORT = process.env.PORT || 5000;
// Initialize Redis
initRedis().catch((err) => {
logger.warn(
"⚠️ Redis initialization failed, continuing without cache:",
err.message
);
});
const server = app.listen(PORT, () => {
logger.info(`🚀🕌 DeenBridge API running on port ${PORT}`);
logger.info(`Environment: ${process.env.NODE_ENV}`);
logger.info(`Process ID: ${process.pid}`);
});
startJobs().catch((err) => logger.error(err, "Background job startup failed"));
// Start payment ingestion worker if enabled
let stopIngestionWorker;
if (process.env.INGESTION_WORKER_ENABLED === "true") {
import("./src/workers/paymentIngestionWorker.js").then(
({ startIngestionWorker, stopIngestionWorker: stopFn }) => {
stopIngestionWorker = stopFn;
startIngestionWorker().catch((err) =>
logger.error(err, "Ingestion worker startup failed")
);
}
);
}
// Graceful shutdown
const gracefulShutdown = async (signal) => {
logger.info(`${signal} received. Starting graceful shutdown...`);
server.close(async () => {
logger.info("HTTP server closed");
await stopJobs();
if (stopIngestionWorker) {
await stopIngestionWorker();
}
// Close Redis connection
await closeRedis();
process.exit(0);
});
// Force shutdown after 10 seconds
setTimeout(() => {
logger.error("Forced shutdown after timeout");
process.exit(1);
}, 10000);
};
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("SIGINT", () => gracefulShutdown("SIGINT"));