This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
163 lines (136 loc) · 4.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
module.exports = function module(config) {
const domain = require('domain');
const primary = domain.create();
const cluster = require('cluster');
const _ = require('lodash');
const validateConfigs = require('./lib/validate_configs');
const { loggerClient } = require('./lib/logger_utils');
const api = require('./lib/api');
const name = config.name ? config.name : 'terafoundation';
const { argv } = require('yargs')
.usage('Usage: $0 [options]')
.alias('c', 'configfile')
.describe('c', `Configuration file to load.
If not specified, the envorinment TERAFOUNDATION_CONFIG can be used.`)
.alias('b', 'bootstrap')
.describe('b', 'Perform initial setup')
.help('h')
.alias('h', 'help');
const configFile = require('./lib/sysconfig')({
configfile: argv.configfile
});
// allows top level function to declare ops_directory, so not hard baked in
// TODO verify why we need this
if (typeof config.ops_directory === 'function') {
config.ops_directory = config.ops_directory(configFile);
}
let logger;
const sysconfig = validateConfigs(cluster, config, configFile);
// set by initAPI
function errorHandler(err) {
// eslint-disable-next-line no-console
const logErr = logger ? logger.error.bind(logger) : console.log;
if (cluster.isMaster) {
logErr(`Error in master with pid: ${process.pid}`);
} else {
logErr(`Error in worker: ${cluster.worker.id} pid: ${process.pid}`);
}
if (err.message) {
logErr(err.message);
} else {
logErr(err);
}
if (err.stack) {
logErr(err.stack);
}
// log saving to disk is async, using hack to give time to flush
setTimeout(() => {
process.exit(-1);
}, 600);
}
function findWorkerCode(context) {
let keyFound = false;
if (config.descriptors) {
_.forOwn(config.descriptors, (value, key) => {
if (process.env.assignment === key) {
keyFound = true;
config[key](context);
}
});
// if no key was explicitly set then default to worker
if (!keyFound) {
config.worker(context);
}
} else {
config.worker(context);
}
}
// Domain emits 'error' when it's given an unhandled error
primary.on('error', errorHandler);
process.on('uncaughtException', errorHandler);
process.on('unhandledRejection', errorHandler);
// See https://github.com/trentm/node-bunyan/issues/246
function handleStdError(err) {
if (err.code === 'EPIPE' || err.code === 'ERR_STREAM_DESTROYED') {
// ignore
} else {
throw err;
}
}
process.stdout.on('error', handleStdError);
process.stderr.on('error', handleStdError);
primary.run(() => {
/*
* Service configuration context
*/
const context = {};
context.sysconfig = sysconfig;
context.cluster = cluster;
context.name = name;
context.arch = process.arch;
context.platform = process.platform;
if (typeof config.cluster_name === 'function') {
context.cluster_name = config.cluster_name(context.sysconfig);
}
// Initialize the API
api(context);
// Bootstrap the top level logger
logger = context.apis.foundation.makeLogger(context.name, context.name);
context.logger = logger;
// FIXME: this should probably be refactored to actually create the
// logger as it stands this function is very confusing
loggerClient(context);
if (config.script) {
config.script(context);
/**
* Use cluster to start multiple workers
*/
} else if (context.cluster.isMaster) {
/**
* If the bootstrap option is provided we run the bootstrap function to
* do any initial application setup.
* */
// TODO verify we need this
if (argv.bootstrap) {
if (config.bootstrap && typeof config.bootstrap === 'function') {
config.bootstrap(context, () => {
// process.exit(0);
});
} else {
logger.error('No bootstrap function provided. Nothing to do.');
// process.exit(0);
}
}
require('./lib/master')(context, config);
// If there's a master plugin defined, pass it on.
if (config.master) {
// TODO reexamine this code here
context.master_plugin = config.master(context, config);
}
} else {
findWorkerCode(context);
}
});
return primary;
};