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

Cluster multiple processes but pass different arguments to one of them? #477

Closed
nathanpeck opened this issue May 21, 2014 · 11 comments
Closed

Comments

@nathanpeck
Copy link
Contributor

I'd like to be able to cluster a process, but have one of the processes in the cluster play a special role, in this case to act as a garbage collector:

[
  {
    "name"       : "server",
    "script"     : "./server.js",
    "instances"  : "3",
    "exec_mode"  : "cluster_mode",
    "port"       : 9090,
  },
  {
    "name"       : "server-gc",
    "script"     : "./server.js",
    "args"        : "['--role=gc']",
    "instances"  : "1",
    "exec_mode"  : "cluster_mode",
    "port"       : 9090,
  }
]

This does not work because PM2 refuses the start the second script. Is there any way to do something similar to this with PM2, or am I going to have to code my own special purpose solution?

@soyuka
Copy link
Collaborator

soyuka commented Jun 14, 2014

I think that you can't do this and won't be able to do it with pm2. A solution would be to clone your ./server.js and to run it as another process.
Issue will stay open in case you got a solution.

#322 may be interested by your findings.

@nathanpeck
Copy link
Contributor Author

I ended up coding my own special purpose daemon script instead of using PM2. It would be nice if this was a feature of PM2, but it turned out to be easier to write my own process manager.

@rlidwka
Copy link
Collaborator

rlidwka commented Jun 22, 2014

pm2 start -f ./path/to/your/config.json, just add "-f" flag to force execution of the same script twice

@soyuka
Copy link
Collaborator

soyuka commented Jun 22, 2014

Yup that's a nice fix thanks @rlidwka!

@soyuka soyuka closed this as completed Jun 22, 2014
@nathanpeck
Copy link
Contributor Author

Ah very nice. Just to be clear: @rlidwka does this also properly cluster the four resulting process instances, or does it just force the execution of the fourth instance without adding it to the cluster that the first 3 are running in?

@rlidwka
Copy link
Collaborator

rlidwka commented Jun 24, 2014

does this also properly cluster the four resulting process instances

Yes, from pm2's point of view all four processes are treated equally. You can even run completely different code, it would be added to cluster as well.

It happens, because pm2 manages just one single cluster, and all processes get added to it.

But there is no guarantees that 4th instance will ever receive any requests, in node 0.10 all requests could easily be sent to 1st and 2nd one (if they're responding quickly enough). I think it had been fixed in 0.11, you need to test that if it's important for you.

@nathanpeck
Copy link
Contributor Author

Okay thanks for the explanation. At this point I've already switched to using a handcoded system that uses Nginx as the loadbalancer / proxy upstream from a cluster of processes each running on a different port, but I'm sure this will help someone else who has a similar issue.

@petewarrior
Copy link

I created a symlink to the script and start that one.

@Flicksie
Copy link

Flicksie commented Oct 8, 2018

4 Years later we still don't have this natively without a -f flag?

@jimsmt
Copy link

jimsmt commented Feb 28, 2019

I created a symlink to the script and start that one.

thanks for the workaround

it's 2019 and we still don't have this feature, the "-f" flag doesn't work for me either

@danderson00
Copy link

Why is this issue closed? The suggested workaround (using the -f flag) does not appear to work, despite passing different arguments after -- and naming the process differently. PM2 decides to restartProcessId instead of starting a new instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants