1

Env.: Node.js on Ubuntu, using PM2 programmatically.

I have started PM2 with 3 instances via Node on my main code. Suppose I use the PM2 command line to delete one of the instances. Can I add back another worker to the pool? Can this be done without affecting the operation of the other workers?

I suppose I should use the start method:

pm2.start({
 name : 'worker',
 script    : 'api/workers/worker.js',         // Script to be run
 exec_mode : 'cluster',        // OR FORK
 instances : 1,                // Optional: Scale your app by 4
 max_memory_restart : '100M',   // Optional: Restart your app if it reaches 100Mo
 autorestart : true
 }, function(err, apps) {
 pm2.disconnect();
 });

However, if you use pm2 monit you'll see that the 2 existing instances are restarted and no other is created. Result is still 2 running instances.

update it doesn't matter if cluster or fork -- behavior is the same.

update 2 The command line has the scale option ( https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/ ), but I don't see this method on the programmatic API documentation ( https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#programmatic-api ).

Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22
noderman
  • 1,934
  • 1
  • 20
  • 36

3 Answers3

2

I actually think this can't be done in PM2 as I have the exact same problem.

I'm sorry, but I think the solution is to use something else as PM2 is fairly limited. The lack of ability to add more workers is a deal breaker for me.

I know you can "scale" on the command line if you are using clustering but I have no idea why you can not start more instances if you are using fork. It makes no sense.

Mike
  • 1,727
  • 2
  • 19
  • 32
  • Yes. If you want to have an elastic pool of workers, scaling should be the first thing in your mind, and that should be done in programmatic way, so you can actually write code to make decisions according to server load. I'll keep the question open... hopefully this will be released soon... – noderman Jan 07 '16 at 14:55
0

As I know, all commands of PM2 can also be used programmatically, including scale. Check out CLI.js to see all available methods.

Try to use the force attribute in the application declaration. If force is true, you can start the same script several times, which is usually not allowed by PM2 (according to the Application Declaration docs)

By the way, autorestart it's true by default.

Pere Joan Martorell
  • 2,608
  • 30
  • 29
0

You can do so by use of a ecosystem.config file. Inside that file you can specify as much worker processes as you want.

E.g. we used BullJS to develop a microservice architecture of different workers that are started with the help of PM2 on multiple cores: The same worker started as named instances multiple times.

Now when jobs are run BullJS load balances the workloads for one specific worker on all available instances for that worker.

You could of course start or stop any instance via CLI and also start additional named workers via the command line to increase the amount of workers (e.g. if many jobs need to be run and you want to process more jobs at a time):

pm2 start './script/to/start.js' --name additional-worker-4
pm2 start './script/to/start.js' --name additional-worker-5
Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22