-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
101 lines (85 loc) · 2.66 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
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* ./index.js
*
* Main module to start the web process. To just start the web process use
* "node index.js". To start both the web and the clock process use "heroku local"
*/
/* eslint-disable global-require */
/* eslint-disable no-process-env */
const logger = require('@salesforce/refocus-logging-client');
const throng = require('throng');
const featureToggles = require('feature-toggles');
const DEFAULT_WEB_CONCURRENCY = 1;
const WORKERS = process.env.WEB_CONCURRENCY || DEFAULT_WEB_CONCURRENCY;
const conf = require('./config');
const logEnvVars = require('./utils/logEnvVars');
/**
* Entry point for each clustered process.
*
* @param {Number} clusterProcessId - process id if called from throng,
* otherwise 0
*/
function start(clusterProcessId = 0) { // eslint-disable-line max-statements
logger.info(`Started node process ${clusterProcessId}`);
/*
* Express app
*/
const app = require('./express').app;
/*
* Sample store
*/
require('./cache/sampleStoreInit').init();
/*
* Clock jobs
* If the clock dyno is NOT enabled, schedule all the scheduled jobs right
* from here.
*/
if (!featureToggles.isFeatureEnabled('enableClockProcess')) {
require('./clock/index'); // eslint-disable-line global-require
}
/*
* Logging
*/
const processName = (process.env.DYNO ? process.env.DYNO + ':' : '') +
clusterProcessId;
if (conf.newRelicKey) {
require('newrelic');
}
if (featureToggles.isFeatureEnabled('enablePubsubStatsLogs')) {
const logPubSubStats = require('./realtime/pubSubStats').log;
setInterval(() => logPubSubStats(processName),
conf.pubSubStatsLoggingInterval);
}
// Log env vars. Only log once when there are multiple processes.
// Process id is 0 for a single process, 1-n for multiple throng workers.
if (clusterProcessId < 2) logEnvVars.log(process.env);
// Custom middleware to add process info to the request (for use in logging)
app.use((req, res, next) => {
// process id (0 for a single process, 1-n for multiple throng workers)
req.clusterProcessId = clusterProcessId;
// process name (dyno + process id)
req.process = processName;
next();
});
}
function startMaster() {
logger.info('Started node cluster master');
} // startMaster
const isProd = (process.env.NODE_ENV === 'production');
if (isProd) {
throng({
lifetime: Infinity,
master: startMaster,
start,
workers: WORKERS,
});
} else {
start();
}