1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 const cluster = require('cluster');
\r
18 const os = require('os');
\r
21 const logger = require('./lib/logger');
\r
23 const jobWorkers = [];
\r
24 const expressWorkers = [];
\r
26 process.env.NODE_CONFIG_DIR = './server/config';
\r
28 // TODO: Do we need this
\r
29 process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
\r
31 const env = process.env['ENV'];
\r
32 const validEnvs = ['production', 'development', 'system_test', 'local'];
\r
34 if (env === undefined) {
\r
35 logger.error('The process environment is not set, so the application will not start.\nPlease set the variable ' +
\r
36 '\'%s\' to one of the following values: %s', 'env', validEnvs);
\r
38 } else if (!validEnvs.includes(env)) {
\r
39 logger.error('%s is not a valid value.\nPlease set the environment variable \'%s\' to one of the following ' +
\r
40 'values: %s', env, 'env', validEnvs);
\r
44 // Workers can only be spawned on the master node.
\r
45 if (cluster.isMaster) {
\r
46 // Use 8 CPU's on non-local environments, otherwise get the number of CPUs
\r
47 const numWorkers = (env === 'local') ? 1 : 8;
\r
49 logger.info('Master node is creating %d workers on the %s environment.', numWorkers, env);
\r
51 // Spawns a worker process for every CPU
\r
52 for (let i = 0; i < numWorkers; i++) {
\r
54 // Don't add job workers on local environments
\r
55 if (env === 'local') continue;
\r
59 // Listener for a spawned worker
\r
60 cluster.on('exit', (worker, code, signal) => {
\r
61 logger.info('Worker %d is online.', worker.process.pid);
\r
63 if (jobWorkers.indexOf(worker.id) !== -1) {
\r
64 console.log(`job worker ${worker.process.pid} exited (signal: ${signal}). Trying to respawn...`);
\r
65 removeJobWorker(worker.id);
\r
69 if (expressWorkers.indexOf(worker.id) !== -1) {
\r
70 console.log(`express worker ${worker.process.pid} exited (signal: ${signal}). Trying to respawn...`);
\r
71 removeExpressWorker(worker.id);
\r
76 if (process.env.express) {
\r
77 logger.info('Created express server process.');
\r
78 require('./feathers/index');
\r
81 if (process.env.job) {
\r
82 logger.info('Created agenda job server process.');
\r
83 require('./agenda/agenda').initializeAgenda();
\r
87 function addExpressWorker () {
\r
88 expressWorkers.push(cluster.fork({ express: 1 }).id);
\r
91 function addJobWorker () {
\r
92 jobWorkers.push(cluster.fork({ job: 1 }).id);
\r
95 function removeExpressWorker (id) {
\r
96 expressWorkers.splice(expressWorkers.indexOf(id), 1);
\r
99 function removeJobWorker (id) {
\r
100 jobWorkers.splice(jobWorkers.indexOf(id), 1);
\r