added svcapi ui and camunda code
[it/otf.git] / otf-frontend / server / src / app.js
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 const cluster = require('cluster');\r
18 const os = require('os');\r
19 \r
20 // Winston\r
21 const logger = require('./lib/logger');\r
22 \r
23 const jobWorkers = [];\r
24 const expressWorkers = [];\r
25 \r
26 process.env.NODE_CONFIG_DIR = './server/config';\r
27 \r
28 // TODO: Do we need this\r
29 process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;\r
30 \r
31 const env = process.env['ENV'];\r
32 const validEnvs = ['production', 'development', 'system_test', 'local'];\r
33 \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
37         process.exit(1);\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
41         process.exit(1);\r
42 }\r
43 \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
48 \r
49         logger.info('Master node is creating %d workers on the %s environment.', numWorkers, env);\r
50 \r
51         // Spawns a worker process for every CPU\r
52         for (let i = 0; i < numWorkers; i++) {\r
53                 addExpressWorker();\r
54                 // Don't add job workers on local environments\r
55                 if (env === 'local') continue;\r
56                 addJobWorker();\r
57         }\r
58 \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
62 \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
66                         addJobWorker();\r
67                 }\r
68 \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
72                         addExpressWorker();\r
73                 }\r
74         });\r
75 } else {\r
76         if (process.env.express) {\r
77                 logger.info('Created express server process.');\r
78                 require('./feathers/index');\r
79         }\r
80 \r
81         if (process.env.job) {\r
82                 logger.info('Created agenda job server process.');\r
83                 require('./agenda/agenda').initializeAgenda();\r
84         }\r
85 }\r
86 \r
87 function addExpressWorker () {\r
88         expressWorkers.push(cluster.fork({ express: 1 }).id);\r
89 }\r
90 \r
91 function addJobWorker () {\r
92         jobWorkers.push(cluster.fork({ job: 1 }).id);\r
93 }\r
94 \r
95 function removeExpressWorker (id) {\r
96         expressWorkers.splice(expressWorkers.indexOf(id), 1);\r
97 }\r
98 \r
99 function removeJobWorker (id) {\r
100         jobWorkers.splice(jobWorkers.indexOf(id), 1);\r
101 }\r