Introduce ESLint and Prettier

This commit is contained in:
Zaid Abdulla committed 2018-08-24 05:31:22 -07:00
1 parent 9c943d0bbf
commit e494bdca8a
26 files changed
+1585 -1594

No files matched your search

+30 -36
View File
@@ -1,5 +1,5 @@
/**
* Copyright 2013-2017 Zaid Abdulla
* Copyright 2013-2018 Zaid Abdulla
*
* This file is part of GenieACS.
*
@@ -30,43 +30,43 @@ let tooManyCrashesTimestamp = 0;
function getDependencyVersions() {
let dependencies = [`node@${process.versions.node}`];
try {
let npmls = JSON.parse(require("child_process").execSync("npm ls --prod --json --depth 0"));
let npmls = JSON.parse(
require("child_process").execSync("npm ls --prod --json --depth 0")
);
for (let k in npmls.dependencies)
dependencies.push(`${k}@${npmls.dependencies[k].version}`);
} catch (err) {}
} catch (err) {
// Continue regardless of error
}
return dependencies.join(",");
}
function getConfig() {
let conf = [];
const keys = Object.keys(process.env);
for (let k of keys)
for (let 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}`);
if (def == null || value !== `${def}`) conf.push(`${name}=${value}`);
}
}
return conf.join(",");
}
function restartWorker(worker, code, signal) {
let msg = {
message: "Worker died",
pid: worker.process.pid
};
if (code != null)
msg.exitCode = code;
if (code != null) msg.exitCode = code;
if (signal != null)
msg.signal = signal;
if (signal != null) msg.signal = signal;
logger.error(msg);
@@ -75,14 +75,13 @@ function restartWorker(worker, code, signal) {
if (respawnTimestamp === now) {
tooManyCrashesTimestamp = now;
cluster.fork();
return
return;
}
if (now - tooManyCrashesTimestamp > 60000) {
process.exitCode = 1;
cluster.removeListener("exit", restartWorker);
for (let pid in cluster.workers)
cluster.workers[pid].kill();
for (let pid in cluster.workers) cluster.workers[pid].kill();
logger.error({
message: "Too many crashes, exiting",
@@ -91,18 +90,16 @@ function restartWorker(worker, code, signal) {
return;
}
setTimeout(function() {
setTimeout(() => {
if (process.exitCode) return;
cluster.fork();
}, respawnTimestamp - now);
}
function start(service) {
logger.init(service, VERSION);
const config = require("./config");
const SERVICE_ADDRESS = config.get(`${service.toUpperCase()}_INTERFACE`)
const SERVICE_PORT = config.get(`${service.toUpperCase()}_PORT`)
const SERVICE_ADDRESS = config.get(`${service.toUpperCase()}_INTERFACE`);
const SERVICE_PORT = config.get(`${service.toUpperCase()}_PORT`);
logger.info({
message: `GenieACS (genieacs-${service}) starting`,
@@ -112,9 +109,12 @@ function start(service) {
config: getConfig()
});
cluster.on("listening", function(worker, address) {
if ((address.addressType === 4 || address.addressType === 6) &&
address.address === SERVICE_ADDRESS && address.port === SERVICE_PORT) {
cluster.on("listening", (worker, address) => {
if (
(address.addressType === 4 || address.addressType === 6) &&
address.address === SERVICE_ADDRESS &&
address.port === SERVICE_PORT
) {
logger.info({
message: "Worker listening",
pid: worker.process.pid,
@@ -126,7 +126,7 @@ function start(service) {
cluster.on("exit", restartWorker);
process.on("SIGINT", function() {
process.on("SIGINT", () => {
logger.info({
message: "Received signal SIGINT, exiting",
pid: process.pid
@@ -135,15 +135,14 @@ function start(service) {
cluster.removeListener("exit", restartWorker);
});
process.on("SIGTERM", function() {
process.on("SIGTERM", () => {
logger.info({
message: "Received signal SIGTERM, exiting",
pid: process.pid
});
cluster.removeListener("exit", restartWorker);
for (let pid in cluster.workers)
cluster.workers[pid].kill();
for (let pid in cluster.workers) cluster.workers[pid].kill();
});
cluster.setupMaster({
@@ -153,15 +152,12 @@ function start(service) {
let workerCount = config.get(`${service.toUpperCase()}_WORKER_PROCESSES`);
if (!workerCount)
workerCount = Math.max(2, require("os").cpus().length);
if (!workerCount) workerCount = Math.max(2, require("os").cpus().length);
for (let i = 0; i < workerCount; ++ i)
cluster.fork();
for (let i = 0; i < workerCount; ++i) cluster.fork();
}
process.on("uncaughtException", function(err) {
process.on("uncaughtException", err => {
logger.error({
message: "Uncaught exception in master process, exiting",
exception: err,
@@ -169,9 +165,7 @@ process.on("uncaughtException", function(err) {
});
process.exitCode = 1;
cluster.removeListener("exit", restartWorker);
for (let pid in cluster.workers)
cluster.workers[pid].kill();
for (let pid in cluster.workers) cluster.workers[pid].kill();
});
exports.start = start;