--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+const cluster = require('cluster');\r
+const os = require('os');\r
+\r
+// Winston\r
+const logger = require('./lib/logger');\r
+\r
+const jobWorkers = [];\r
+const expressWorkers = [];\r
+\r
+process.env.NODE_CONFIG_DIR = './server/config';\r
+\r
+// TODO: Do we need this\r
+process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;\r
+\r
+const env = process.env['ENV'];\r
+const validEnvs = ['production', 'development', 'system_test', 'local'];\r
+\r
+if (env === undefined) {\r
+ logger.error('The process environment is not set, so the application will not start.\nPlease set the variable ' +\r
+ '\'%s\' to one of the following values: %s', 'env', validEnvs);\r
+ process.exit(1);\r
+} else if (!validEnvs.includes(env)) {\r
+ logger.error('%s is not a valid value.\nPlease set the environment variable \'%s\' to one of the following ' +\r
+ 'values: %s', env, 'env', validEnvs);\r
+ process.exit(1);\r
+}\r
+\r
+// Workers can only be spawned on the master node.\r
+if (cluster.isMaster) {\r
+ // Use 8 CPU's on non-local environments, otherwise get the number of CPUs\r
+ const numWorkers = (env === 'local') ? 1 : 8;\r
+\r
+ logger.info('Master node is creating %d workers on the %s environment.', numWorkers, env);\r
+\r
+ // Spawns a worker process for every CPU\r
+ for (let i = 0; i < numWorkers; i++) {\r
+ addExpressWorker();\r
+ // Don't add job workers on local environments\r
+ if (env === 'local') continue;\r
+ addJobWorker();\r
+ }\r
+\r
+ // Listener for a spawned worker\r
+ cluster.on('exit', (worker, code, signal) => {\r
+ logger.info('Worker %d is online.', worker.process.pid);\r
+\r
+ if (jobWorkers.indexOf(worker.id) !== -1) {\r
+ console.log(`job worker ${worker.process.pid} exited (signal: ${signal}). Trying to respawn...`);\r
+ removeJobWorker(worker.id);\r
+ addJobWorker();\r
+ }\r
+\r
+ if (expressWorkers.indexOf(worker.id) !== -1) {\r
+ console.log(`express worker ${worker.process.pid} exited (signal: ${signal}). Trying to respawn...`);\r
+ removeExpressWorker(worker.id);\r
+ addExpressWorker();\r
+ }\r
+ });\r
+} else {\r
+ if (process.env.express) {\r
+ logger.info('Created express server process.');\r
+ require('./feathers/index');\r
+ }\r
+\r
+ if (process.env.job) {\r
+ logger.info('Created agenda job server process.');\r
+ require('./agenda/agenda').initializeAgenda();\r
+ }\r
+}\r
+\r
+function addExpressWorker () {\r
+ expressWorkers.push(cluster.fork({ express: 1 }).id);\r
+}\r
+\r
+function addJobWorker () {\r
+ jobWorkers.push(cluster.fork({ job: 1 }).id);\r
+}\r
+\r
+function removeExpressWorker (id) {\r
+ expressWorkers.splice(expressWorkers.indexOf(id), 1);\r
+}\r
+\r
+function removeJobWorker (id) {\r
+ jobWorkers.splice(jobWorkers.indexOf(id), 1);\r
+}\r