UNPKG

jsm-core

Version:
168 lines (167 loc) 7.03 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startServer = startServer; // We need this in order to use @Decorators require("reflect-metadata"); // require('module-alias/register'); const express_1 = __importDefault(require("express")); const jsm_logger_1 = __importStar(require("jsm-logger")); const os_1 = __importDefault(require("os")); const loaders_1 = __importDefault(require("./loaders")); const context_1 = require("../context"); const logger = (0, jsm_logger_1.default)(jsm_logger_1.LoggerContext.APPLICATION, 'APP'); const totalCPUs = os_1.default.cpus().length; const MAX_WORKERS = 4; function cleanupProcess() { return __awaiter(this, void 0, void 0, function* () { try { logger.warn("Cleaning up process before exit"); const cache = (0, context_1.getRegistry)().cache; yield cache.actions.unblockAllProcessActions(); yield cache.tasks.unfreezeAllProcessTasks(); } catch (error) { logger.error("Error cleaning up process", error); } }); } function startServer() { return __awaiter(this, void 0, void 0, function* () { const app = (0, express_1.default)(); app.get("/", (req, res) => { res.send("Hello World from main process"); }); const { httpServer } = yield (0, loaders_1.default)({ expressApp: app }); const port = (0, context_1.getRegistry)().getConfig('currentService.port') || 6000; // <-- ✅ use the port from the config // Use the httpServer from loader instead of app.listen() httpServer .listen(port, // <-- ✅ use the port from the config '0.0.0.0', // <-- ✅ bind to all interfaces () => { console.log(''); logger.success(`${(0, context_1.getRegistry)().getConfig('currentService.name')} Service listening on port: ${port} in env: ${process.env.NODE_ENV}`); console.log(''); }) .on('error', (err) => { console.log(''); logger.error(`${(0, context_1.getRegistry)().getConfig('currentService.name')} System error in env: ${process.env.NODE_ENV}`, err); console.log(''); process.exit(1); }); return { app, httpServer }; }); } /** * 'beforeExit': Emitted when the Node.js process is about to exit due to no more work to do. */ process.on('beforeExit', () => { logger.warn('Node.js process is about to exit.'); }); /** * 'exit': Emitted when the Node.js process is about to exit. * This event is not emitted for conditions that cause explicit termination, * such as calling process.exit(1) or uncaught exceptions. */ process.on('exit', () => { logger.error(`${(0, context_1.getRegistry)().getConfig('currentService.name')} is exiting`, process.argv); }); /** * 'uncaughtException': Emitted when an uncaught JavaScript exception bubbles all the way back to the event loop. */ process.on('uncaughtException', (error) => { logger.error(`${(0, context_1.getRegistry)().getConfig('currentService.name')} crashed due to uncaught exception`, error); // Optionally, you can exit the process if desired // process.exit(1); }); /** * 'unhandledRejection': Emitted when a Promise is rejected but there is no error handler to catch the rejection. */ process.on('unhandledRejection', (reason, promise) => { logger.error('Unhandled Rejection at:', promise, 'reason:', reason); }); /** * 'warning': Emitted when a warning occurs. * This is typically triggered by using deprecated features. */ process.on('warning', (warning) => { logger.warn('Node.js process warning:', warning); }); /** * 'message': Emitted when the child process uses process.send() to send messages. */ process.on('message', (message) => { logger.debug('Received message from parent process:', message); }); /** * 'disconnect': Emitted after calling the .disconnect() method in the parent process. */ process.on('disconnect', () => { logger.event('Parent process manually disconnected.'); }); /** * 'SIGINT': Emitted when the process is interrupted with <Ctrl>+C. */ process.on('SIGINT', () => { logger.event('Received SIGINT signal. Exiting...'); // Optionally, perform cleanup tasks before exiting cleanupProcess().then(() => process.exit(1)).catch(() => { process.exit(1); }); }); /** * 'SIGTERM': Emitted when the process is terminated. */ process.on('SIGTERM', () => { logger.warn('Received SIGTERM signal. Exiting...'); // Optionally, perform cleanup tasks before exiting cleanupProcess().then(() => process.exit(1)).catch(() => { process.exit(1); }); }); /** * 'SIGBREAK': Emitted when the process is interrupted with <Ctrl>+Break. */ process.on('SIGBREAK', () => { logger.debug('Received SIGBREAK signal.'); });