最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js pm2 delay script launch - Stack Overflow

programmeradmin3浏览0评论

I use the awesome pm2 package to keep my node.js apps alive, but i have an issue I don't know how to fix.

One of my apps requires several scripts, a server and a few clients. Often when I restart my server, all the script are restarted, but connection isn't established because the clients have loaded before the server. Is it possible to make sure that a script starts after the other have loaded? Let me try to rephrase so it gets clearer

I have :

pm2 start server.js
pm2 start client.js

And I'd like to somehow start the client only when the server is started.

Apologies is my question seems unclear, please comment and I'll explain more!

I use the awesome pm2 package to keep my node.js apps alive, but i have an issue I don't know how to fix.

One of my apps requires several scripts, a server and a few clients. Often when I restart my server, all the script are restarted, but connection isn't established because the clients have loaded before the server. Is it possible to make sure that a script starts after the other have loaded? Let me try to rephrase so it gets clearer

I have :

pm2 start server.js
pm2 start client.js

And I'd like to somehow start the client only when the server is started.

Apologies is my question seems unclear, please comment and I'll explain more!

Share Improve this question asked Feb 1, 2014 at 17:46 xShirasexShirase 12.4k4 gold badges54 silver badges86 bronze badges 2
  • Where do you have m2 start server.js pm2 start client.js ? In a bash script ? – jillro Commented Feb 1, 2014 at 17:52
  • nope, I was trying to explain what I needed. I use a processes.json like described here : github.com/Unitech/pm2#a13 – xShirase Commented Feb 1, 2014 at 17:56
Add a comment  | 

3 Answers 3

Reset to default 12

Run Pm2 using this script

pm2 start node <start script> --restart-delay <time in milliseconds>

Rewrite your client to make several connection attempts. It is always a good thing to do, and it'll also help in this case.

Check the pm2 restart strategies

You can have either the option restart-delay

You can also use the restart_delay to set a fixed timing between restarts

Or preferably you may use exp-backoff-restart-delay

Instead of restarting your application like crazy when exceptions happens (e.g. database is down), the exponential backoff restart will increase incrementaly the time between restarts, reducing the pressure on your DB or your external provider.

You can either use it command line

pm2 start app.js --exp-backoff-restart-delay=100

or in json format on file or via ecosystem.config.js file:

module.exports = [{
  script: 'app.js',
  exp_backoff_restart_delay: 100
}]
发布评论

评论列表(0)

  1. 暂无评论