Skip to content

Adds --cluster support #2596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cli/cli-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,9 @@ export default {
env: "PARSE_SERVER_SCHEMA_CACHE_TTL",
help: "The TTL for caching the schema for optimizing read/write operations. You should put a long TTL when your DB is in production. default to 0; disabled.",
action: numberParser("schemaCacheTTL"),
},
"cluster": {
help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
action: numberParser("cluster"),
}
};
60 changes: 44 additions & 16 deletions src/cli/parse-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ParseServer } from '../index';
import definitions from './cli-definitions';
import program from './utils/commander';
import { mergeWithOptions } from './utils/commander';
import cluster from 'cluster';
import os from 'os';

program.loadDefinitions(definitions);

Expand Down Expand Up @@ -48,28 +50,54 @@ if (!options.appId || !options.masterKey || !options.serverURL) {
process.exit(1);
}

const app = express();
const api = new ParseServer(options);
app.use(options.mountPath, api);

var server = app.listen(options.port, function() {

function logStartupOptions(options) {
for (let key in options) {
let value = options[key];
if (key == "masterKey") {
value = "***REDACTED***";
}
console.log(`${key}: ${value}`);
}
console.log('');
console.log('parse-server running on '+options.serverURL);
});
}

function startServer(options, callback) {
const app = express();
const api = new ParseServer(options);
app.use(options.mountPath, api);

var server = app.listen(options.port, callback);

var handleShutdown = function() {
console.log('Termination signal received. Shutting down.');
server.close(function () {
process.exit(0);
var handleShutdown = function() {
console.log('Termination signal received. Shutting down.');
server.close(function () {
process.exit(0);
});
};
process.on('SIGTERM', handleShutdown);
process.on('SIGINT', handleShutdown);
}

if (options.cluster) {
const numCPUs = typeof options.cluster === 'number' ? options.cluster : os.cpus().length;
if (cluster.isMaster) {
logStartupOptions(options);
for(var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died... Restarting`);
cluster.fork();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steven-supersolid I believe that will do :)

});
} else {
startServer(options, () => {
console.log('['+process.pid+'] parse-server running on '+options.serverURL);
});
}
} else {
startServer(options, () => {
logStartupOptions(options);
console.log('');
console.log('['+process.pid+'] parse-server running on '+options.serverURL);
});
};
process.on('SIGTERM', handleShutdown);
process.on('SIGINT', handleShutdown);
}