diff --git a/env b/env index 73f9863..d9cce48 100644 --- a/env +++ b/env @@ -15,6 +15,8 @@ RUNNING_ON_MULTIPLE_PORT=NO # PISAHKAN DENGAN MENGGUNAKAN SEPARATOR KOMA TANPA SPASI PORTS=3001,3002,3003,3004 +# ===================== CLUSTER NODE STICKY SESSION(YES or NO) =========================== +CLUSTER_NODE_STICKY_SESSION=YES # =========================== KONFIGURASI UNTUK SERVER =========================== # SERVER_FOR=development diff --git a/index.js b/index.js index 2e98822..9adfdf2 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const totalCPUs = require('node:os').cpus().length; const process = require('node:process'); const winston = require('./application/helpers/winston.logger'); const app = require('./server'); -// const sticky = require('sticky-session'); +const sticky = require('sticky-session'); var serverHSMStatus = 'HSM SERVER NOT CONNECTED'; if (process.env.NUMBER_SERVER_ACTIVE == '1') { @@ -60,20 +60,6 @@ function startExpress() { // set port, listen for requests const PORT = process.env.PORT || 3000; - let message = `Server is running on version ${ - process.env.VERSION - } environment: ${process.env.NODE_ENV.toUpperCase()} port: ${PORT} on ${serverActive}. HSM STATUS : ${serverHSMStatus} `; - - if (process.env.CLUSTER_NODE.toUpperCase() == 'YES') { - message = `Server is running with cluster node on version ${ - process.env.VERSION - } environment: ${process.env.NODE_ENV.toUpperCase()} port: ${PORT} on ${serverActive}. HSM STATUS : ${serverHSMStatus} `; - } - - app.listen(PORT, () => { - winston.logger.info(message); - }); - // jika settingan running on multiple if ( process.env.RUNNING_ON_MULTIPLE_PORT.toUpperCase() == 'YES' && @@ -107,18 +93,46 @@ function startExpress() { winston.logger.warn( `If you want running the service on multiple port, please set CLUSTER_NODE = NO in .env file ` ); + } else if ( + process.env.CLUSTER_NODE_STICKY_SESSION.toUpperCase() == 'YES' && + process.env.CLUSTER_NODE.toUpperCase() == 'YES' + ) { + winston.logger.warn( + `If you want running the service with sticky session, please set CLUSTER_NODE = NO in .env file ` + ); } - // const server = app.listen(PORT, () => { - // console.log( - // `Worker ${process.pid} started. Listening on port ${ - // server.address().port - // }` - // ); - // }); + let message = `Server is running on version ${ + process.env.VERSION + } environment: ${process.env.NODE_ENV.toUpperCase()} port: ${PORT} on ${serverActive}. HSM STATUS : ${serverHSMStatus} `; - // // Use sticky-session with the server instance - // sticky.listen(server); + if (process.env.CLUSTER_NODE.toUpperCase() == 'YES') { + message = `Server is running with cluster node on version ${ + process.env.VERSION + } environment: ${process.env.NODE_ENV.toUpperCase()} port: ${PORT} on ${serverActive}. HSM STATUS : ${serverHSMStatus} `; + } else if (process.env.CLUSTER_NODE_STICKY_SESSION.toUpperCase() == 'YES') { + message = `Server is running with sticky session on version ${ + process.env.VERSION + } environment: ${process.env.NODE_ENV.toUpperCase()} port: ${PORT} on ${serverActive}. HSM STATUS : ${serverHSMStatus} `; + } + + if (process.env.CLUSTER_NODE_STICKY_SESSION.toUpperCase() == 'YES') { + const server = app.listen(PORT, () => { + console.log( + `Worker ${process.pid} started. Listening on port ${ + server.address().port + }` + ); + winston.logger.info(message); + }); + + // Use sticky-session with the server instance + sticky.listen(server); + } else { + app.listen(PORT, () => { + winston.logger.info(message); + }); + } } // Use sticky-session to handle sticky sessions