forked from kobotoolbox/enketo-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (30 loc) · 1.25 KB
/
app.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
#!/usr/bin/env node
/* eslint no-console: ["error", { allow: ["log", "error"] }] */
const cluster = require( 'cluster' );
const numCPUs = require( 'os' ).cpus().length;
if ( cluster.isMaster ) {
// Fork workers.
for ( let i = 0; i < numCPUs; i++ ) {
cluster.fork();
}
cluster.on( 'exit', function( worker ) {
console.log( 'Worker ' + worker.process.pid + ' sadly passed away. It will be reincarnated.' );
cluster.fork();
} );
} else {
const app = require( './config/express' );
const server = app.listen( app.get( 'port' ), () => {
const worker = ( cluster.worker ) ? cluster.worker.id : 'Master';
const msg = 'Worker ' + worker + ' ready for duty at port ' + server.address().port + '! (environment: ' + app.get( 'env' ) + ')';
console.log( msg );
} );
/**
* The goal of this timeout is to time out AFTER the client (browser request) times out.
* This avoids nasty issues where a proxied submission is still ongoing but Enketo
* drops the connection, potentially resulting in the browser queue not emptying,
* despite submitting successfully.
*
* https://github.com/kobotoolbox/enketo-express/issues/564
*/
server.timeout = app.get( 'timeout' ) + 1000;
}