Files
genieacs/bin/genieacs-ui
T
Zaid Abdulla 0b59811924 Build script
A build process that generates a package with single executable for each
component.
2019-01-04 01:13:47 -08:00

113 lines
2.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
"use strict";
const path = require("path");
const fs = require("fs");
process.chdir(path.resolve(path.dirname(fs.realpathSync(__filename)), ".."));
const config = require("../lib/config");
const logger = require("../lib/logger");
const cluster = require("../lib/cluster");
const server = require("../lib/server");
const ui = require("../lib/ui");
const db = require("../lib/ui/db");
const cache = require("../lib/cache");
const VERSION = require("../package.json").version;
logger.init("ui", VERSION);
const SERVICE_ADDRESS = config.get("UI_INTERFACE");
const SERVICE_PORT = config.get("UI_PORT");
function exitWorkerGracefully() {
db.disconnect();
cache.disconnect();
cluster.worker.disconnect();
}
function exitWorkerUngracefully() {
process.exit(1);
}
if (!cluster.worker) {
const WORKER_COUNT = config.get("UI_WORKER_PROCESSES");
logger.info({
message: `genieacs-ui 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("UI_SSL")) {
ssl = {
key: path.resolve(config.get("CONFIG_DIR"), "ui.key"),
cert: path.resolve(config.get("CONFIG_DIR"), "ui.crt"),
ca: path.resolve(config.get("CONFIG_DIR"), "ui.ca-bundle")
};
}
let stopping = false;
process.on("uncaughtException", err => {
if (err.code === "ERR_IPC_DISCONNECTED") return;
logger.error({
message: "Uncaught exception",
exception: err,
pid: process.pid
});
stopping = true;
server
.stop()
.then(exitWorkerGracefully)
.catch(exitWorkerUngracefully);
});
process.on("SIGINT", () => {
stopping = true;
server
.stop()
.then(exitWorkerGracefully)
.catch(exitWorkerUngracefully);
});
process.on("SIGTERM", () => {
stopping = true;
server
.stop()
.then(exitWorkerGracefully)
.catch(exitWorkerUngracefully);
});
const listener = (req, res) => {
if (stopping) res.setHeader("Connection", "close");
ui.listener(req, res);
};
cache.connect(err => {
if (err) throw err;
server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, listener, ui.onConnection);
});
}