Skip to content
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

feat: add app launch in parallel limit #5799

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions lib/API/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
"docDefault": false,
"docDescription": "Make the process wait for a process.send('ready')"
},
"launch_limit": {
"type": "number",
"docDefault": 1,
"docDescription": "Maximum number of launching processes at a time"
},
"instances": {
"type": "number",
"docDefault": 1,
Expand Down
9 changes: 8 additions & 1 deletion lib/God.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,14 @@ God.prepare = function prepare (env, cb) {
env.instances = 1;
}

timesLimit(env.instances, 1, function (n, next) {
// app launch in parallel limit
if (env.launch_limit != null) {
env.launch_limit = parseInt(env.launch_limit);
}
if (!env.launch_limit || env.launch_limit < 0) {
env.launch_limit = 1;
}
timesLimit(env.instances, env.launch_limit, function (n, next) {
env.vizion_running = false;
if (env.env && env.env.vizion_running) {
env.env.vizion_running = false;
Expand Down
64 changes: 64 additions & 0 deletions test/programmatic/graceful.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,70 @@ describe('Wait ready / Graceful start / restart', function() {

});

describe('(Cluster): Launching limit feature', function () {
this.timeout(10000);

after(function(done) {
pm2.delete('all', done);
});

it('Should launch app processes in parallel', function(done) {
pm2.start({
script : './wait-ready.js',
instances : 3,
launch_limit: 2,
exec_mode : 'cluster',
name : 'launching-limit-1'
});

setTimeout(function() {
pm2.list(function(err, apps) {
should(apps[0].pm2_env.status).eql('online');
should(apps[1].pm2_env.status).eql('online');
should(apps[2].pm2_env.status).eql('online');
pm2.delete('all', done);
})
}, 500);
});

it('Should launch app processes in parallel and limit launching processes on wait ready', function(done) {
pm2.start({
script : './wait-ready.js',
listen_timeout : 2000,
wait_ready : true,
instances : 3,
launch_limit: 2,
exec_mode : 'cluster',
name : 'launching-limit-2'
});

setTimeout(function() {
pm2.list(function(err, apps) {
should(apps[0].pm2_env.status).eql('launching');
should(apps[1].pm2_env.status).eql('launching');
should(apps[2]).undefined();
});
}, 500);

setTimeout(function() {
pm2.list(function(err, apps) {
should(apps[0].pm2_env.status).eql('online');
should(apps[1].pm2_env.status).eql('online');
should(apps[2].pm2_env.status).eql('launching');
})
}, 1500);

setTimeout(function() {
pm2.list(function(err, apps) {
should(apps[0].pm2_env.status).eql('online');
should(apps[1].pm2_env.status).eql('online');
should(apps[2].pm2_env.status).eql('online');
done();
})
}, 2500);
});
});

describe('(Cluster): Wait ready feature', function () {
this.timeout(10000);

Expand Down
6 changes: 5 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ export interface StartOptions {
* when you want your application to be considered as ready.
*/
wait_ready?: boolean;
/**
* (Default: 1) Maximum number of launching processes at a time on application startup.
*/
launch_limit?: number;
/**
* (Default: 1600)
* The number of milliseconds to wait after a stop or restart command issues a SIGINT signal to kill the
Expand Down Expand Up @@ -446,7 +450,7 @@ export interface StartOptions {

interface ReloadOptions {
/**
* (Default: false) If true is passed in, pm2 will reload it’s environment from process.env
* (Default: false) If true is passed in, pm2 will reload it’s environment from process.env
* before reloading your process.
*/
updateEnv?: boolean;
Expand Down