Build script

A build process that generates a package with single executable for each
component.
This commit is contained in:
Zaid Abdulla committed 2019-01-04 01:13:47 -08:00
1 parent 819e8a52a3
commit 0b59811924
23 files changed
+734 -688

No files matched your search

+7 -83
View File
@@ -20,44 +20,10 @@
const cluster = require("cluster");
const logger = require("./logger");
const config = require("./config");
const VERSION = require("../package.json").version;
let respawnTimestamp = 0;
let tooManyCrashesTimestamp = 0;
function getDependencyVersions() {
const dependencies = [`node@${process.versions.node}`];
try {
const npmls = JSON.parse(
require("child_process").execSync("npm ls --prod --json --depth 0")
);
for (const k in npmls.dependencies)
dependencies.push(`${k}@${npmls.dependencies[k].version}`);
} catch (err) {
// Continue regardless of error
}
return dependencies.join(",");
}
function getConfig() {
const conf = [];
const keys = Object.keys(process.env);
for (const k of keys) {
if (k.startsWith("GENIEACS_")) {
const name = k.slice(9);
const value = process.env[k];
const def = config.getDefault(name);
if (def == null || value !== `${def}`) conf.push(`${name}=${value}`);
}
}
return conf.join(",");
}
function restartWorker(worker, code, signal) {
const msg = {
message: "Worker died",
@@ -96,24 +62,12 @@ function restartWorker(worker, code, signal) {
}, respawnTimestamp - now);
}
function start(service) {
logger.init(service, VERSION);
const SERVICE_ADDRESS = config.get(`${service.toUpperCase()}_INTERFACE`);
const SERVICE_PORT = config.get(`${service.toUpperCase()}_PORT`);
logger.info({
message: `GenieACS (genieacs-${service}) starting`,
pid: process.pid,
version: VERSION,
dependencies: getDependencyVersions(),
config: getConfig()
});
function start(workerCount, servicePort, serviceAddress) {
cluster.on("listening", (worker, address) => {
if (
(address.addressType === 4 || address.addressType === 6) &&
address.address === SERVICE_ADDRESS &&
address.port === SERVICE_PORT
address.address === serviceAddress &&
address.port === servicePort
) {
logger.info({
message: "Worker listening",
@@ -126,46 +80,16 @@ function start(service) {
cluster.on("exit", restartWorker);
process.on("SIGINT", () => {
logger.info({
message: "Received signal SIGINT, exiting",
pid: process.pid
});
cluster.removeListener("exit", restartWorker);
});
process.on("SIGTERM", () => {
logger.info({
message: "Received signal SIGTERM, exiting",
pid: process.pid
});
cluster.removeListener("exit", restartWorker);
for (const pid in cluster.workers) cluster.workers[pid].kill();
});
cluster.setupMaster({
exec: "lib/server",
args: [service]
});
let workerCount = config.get(`${service.toUpperCase()}_WORKER_PROCESSES`);
if (!workerCount) workerCount = Math.max(2, require("os").cpus().length);
for (let i = 0; i < workerCount; ++i) cluster.fork();
}
process.on("uncaughtException", err => {
logger.error({
message: "Uncaught exception in master process, exiting",
exception: err,
pid: process.pid
});
process.exitCode = 1;
function stop() {
cluster.removeListener("exit", restartWorker);
for (const pid in cluster.workers) cluster.workers[pid].kill();
});
}
exports.start = start;
exports.stop = stop;
exports.worker = cluster.worker;