diff --git a/package-lock.json b/package-lock.json index 920f070a9..323705476 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "compression": "^1.7.4", "cookie-parser": "^1.4.7", "cors": "2.8.5", + "crisp-status-reporter": "^1.2.2", "csrf": "^3.1.0", "dinero.js": "^1.9.1", "dotenv": "^16.4.5", @@ -5227,6 +5228,15 @@ "node": ">=10.0.0" } }, + "node_modules/crisp-status-reporter": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/crisp-status-reporter/-/crisp-status-reporter-1.2.2.tgz", + "integrity": "sha512-uRkNJdeqaQLllEiTO7H1uAgz99TycbyuEwjookSsZCsPmz7Igvv2o/M4GOBcu8OF0+s0R9WbOBG2VzRBdDGTTQ==", + "license": "MIT", + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/cross-fetch": { "version": "3.1.8", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz", diff --git a/package.json b/package.json index eec843a5c..072042a9a 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "compression": "^1.7.4", "cookie-parser": "^1.4.7", "cors": "2.8.5", + "crisp-status-reporter": "^1.2.2", "csrf": "^3.1.0", "dinero.js": "^1.9.1", "dotenv": "^16.4.5", diff --git a/server.js b/server.js index f98ec67da..1dee9d49c 100644 --- a/server.js +++ b/server.js @@ -22,6 +22,7 @@ const { applyIOHelpers } = require("./server/utils/ioHelpers"); const { redisSocketEvents } = require("./server/web-sockets/redisSocketEvents"); const { ElastiCacheClient, DescribeCacheClustersCommand } = require("@aws-sdk/client-elasticache"); const { InstanceRegion } = require("./server/utils/instanceMgr"); +const { StartStatusReporter } = require("./server/utils/statusReporter"); const CLUSTER_RETRY_BASE_DELAY = 100; const CLUSTER_RETRY_MAX_DELAY = 5000; @@ -297,6 +298,8 @@ const main = async () => { applyRoutes({ app }); redisSocketEvents({ io: ioRedis, redisHelpers, ioHelpers, logger }); + StartStatusReporter(); + try { await server.listen(port); logger.log(`Server started on port ${port}`, "INFO", "api"); diff --git a/server/utils/getHostNameOrIP.js b/server/utils/getHostNameOrIP.js new file mode 100644 index 000000000..94dace70f --- /dev/null +++ b/server/utils/getHostNameOrIP.js @@ -0,0 +1,26 @@ +// Load environment variables THIS MUST BE AT THE TOP +const path = require("path"); +require("dotenv").config({ + path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`) +}); + +const { networkInterfaces, hostname } = require("node:os"); + +const getHostNameOrIP = () => { + // Try to get the hostname first + const hostName = hostname(); + if (hostName) return hostName; + + const interfaces = networkInterfaces(); + for (const name of Object.keys(interfaces)) { + for (const iface of interfaces[name]) { + if (iface.family === "IPv4" && !iface.internal) { + return iface.address; + } + } + } + + return "127.0.0.1"; +}; + +module.exports = getHostNameOrIP; diff --git a/server/utils/logger.js b/server/utils/logger.js index b6f9e865c..9a27105fd 100644 --- a/server/utils/logger.js +++ b/server/utils/logger.js @@ -8,10 +8,10 @@ const InstanceManager = require("../utils/instanceMgr").default; const winston = require("winston"); const WinstonCloudWatch = require("winston-cloudwatch"); const { isString, isEmpty } = require("lodash"); -const { networkInterfaces, hostname } = require("node:os"); const { uploadFileToS3 } = require("./s3"); const { v4 } = require("uuid"); const { InstanceRegion } = require("./instanceMgr"); +const getHostNameOrIP = require("./getHostNameOrIP"); const LOG_LEVELS = { error: { level: 0, name: "error" }, @@ -76,22 +76,6 @@ const createLogger = () => { })(); }; - const getHostNameOrIP = () => { - // Try to get the hostname first - const hostName = hostname(); - if (hostName) return hostName; - - const interfaces = networkInterfaces(); - for (const name of Object.keys(interfaces)) { - for (const iface of interfaces[name]) { - if (iface.family === "IPv4" && !iface.internal) { - return iface.address; - } - } - } - - return "127.0.0.1"; - }; const createProductionTransport = (level, logStreamName, filters) => { return new WinstonCloudWatch({ level, diff --git a/server/utils/statusReporter.js b/server/utils/statusReporter.js new file mode 100644 index 000000000..0a6c3ad65 --- /dev/null +++ b/server/utils/statusReporter.js @@ -0,0 +1,36 @@ +// Load environment variables THIS MUST BE AT THE TOP +const path = require("path"); +const getHostNameOrIP = require("./getHostNameOrIP"); +require("dotenv").config({ + path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`) +}); +const CrispStatusReporter = require("crisp-status-reporter").CrispStatusReporter; +const InstanceManager = require("../utils/instanceMgr").default; + +exports.StartStatusReporter = function () { + //For ImEX Online. + + InstanceManager({ + executeFunction: true, + debug: true, + args: [], + imex: () => { + if ( + process.env.NODE_ENV === undefined || + !process.env.CRISP_SECRET_TOKEN || + !process.env.CRISP_SERVICE_IDENTIFIER || + !process.env.CRISP_NODE_IDENTIFIER + ) { + return; + } + var crispStatusReporter = new CrispStatusReporter({ + token: process.env.CRISP_SECRET_TOKEN, // Your reporter token (given by Crisp) + service_id: process.env.CRISP_SERVICE_IDENTIFIER, // Service ID containing the parent Node for Replica (given by Crisp) + node_id: process.env.CRISP_NODE_IDENTIFIER, // Node ID containing Replica (given by Crisp) + replica_id: getHostNameOrIP(), // Unique Replica ID for instance (ie. your IP on the LAN) + interval: 30, // Reporting interval (in seconds; defaults to 30 seconds if not set) + console: require("console") // Console instance if you need to debug issues, + }); + } + }); +};