Bundling code now with Rollup for its flat bundling and tree shaking support. Front end code still goes through Webpack for browser support.
109 lines
2.4 KiB
JavaScript
Executable File
109 lines
2.4 KiB
JavaScript
Executable File
#!/usr/bin/env -S node -r esm
|
|
"use strict";
|
|
import path from "path";
|
|
import fs from "fs";
|
|
|
|
process.chdir(path.resolve(path.dirname(fs.realpathSync(__filename)), ".."));
|
|
|
|
import * as config from "../lib/config";
|
|
import * as logger from "../lib/logger";
|
|
import * as cluster from "../lib/cluster";
|
|
import * as server from "../lib/server";
|
|
import { listener } from "../lib/nbi";
|
|
import * as db from "../lib/db";
|
|
import * as extensions from "../lib/extensions";
|
|
import * as cache from "../lib/cache";
|
|
import { version as VERSION } from "../package.json";
|
|
|
|
logger.init("nbi", VERSION);
|
|
|
|
const SERVICE_ADDRESS = config.get("NBI_INTERFACE");
|
|
const SERVICE_PORT = config.get("NBI_PORT");
|
|
|
|
function exitWorkerGracefully() {
|
|
db.disconnect();
|
|
cache.disconnect();
|
|
extensions.killAll();
|
|
cluster.worker.disconnect();
|
|
}
|
|
|
|
function exitWorkerUngracefully() {
|
|
extensions.killAll(() => {
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
if (!cluster.worker) {
|
|
const WORKER_COUNT = config.get("NBI_WORKER_PROCESSES");
|
|
|
|
logger.info({
|
|
message: `genieacs-nbi starting`,
|
|
pid: process.pid,
|
|
version: VERSION
|
|
});
|
|
|
|
cluster.start(WORKER_COUNT, SERVICE_PORT, SERVICE_ADDRESS);
|
|
|
|
process.on("SIGINT", () => {
|
|
logger.info({
|
|
message: "Received signal SIGINT, exiting",
|
|
pid: process.pid
|
|
});
|
|
|
|
cluster.stop();
|
|
});
|
|
|
|
process.on("SIGTERM", () => {
|
|
logger.info({
|
|
message: "Received signal SIGTERM, exiting",
|
|
pid: process.pid
|
|
});
|
|
|
|
cluster.stop();
|
|
});
|
|
} else {
|
|
let ssl;
|
|
if (config.get("NBI_SSL")) {
|
|
ssl = {
|
|
key: path.resolve(config.get("CONFIG_DIR"), "nbi.key"),
|
|
cert: path.resolve(config.get("CONFIG_DIR"), "nbi.crt"),
|
|
ca: path.resolve(config.get("CONFIG_DIR"), "nbi.ca-bundle")
|
|
};
|
|
}
|
|
|
|
process.on("uncaughtException", err => {
|
|
if (err.code === "ERR_IPC_DISCONNECTED") return;
|
|
logger.error({
|
|
message: "Uncaught exception",
|
|
exception: err,
|
|
pid: process.pid
|
|
});
|
|
server
|
|
.stop()
|
|
.then(exitWorkerGracefully)
|
|
.catch(exitWorkerUngracefully);
|
|
});
|
|
|
|
process.on("SIGINT", () => {
|
|
server
|
|
.stop()
|
|
.then(exitWorkerGracefully)
|
|
.catch(exitWorkerUngracefully);
|
|
});
|
|
|
|
process.on("SIGTERM", () => {
|
|
server
|
|
.stop()
|
|
.then(exitWorkerGracefully)
|
|
.catch(exitWorkerUngracefully);
|
|
});
|
|
|
|
db.connect(err => {
|
|
if (err) throw err;
|
|
cache.connect(err => {
|
|
if (err) throw err;
|
|
server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, listener);
|
|
});
|
|
});
|
|
}
|