PM2 don't restart on clean exit - node.js

PM2 has an option to restart an app on error or clean exit. Unfortunately, even clean zero-code exit increases restarts counter. Is there a way to increase restarts counter only on error or crash (non-zero code exit)?

I noticed that last week in some experimenting with PM2 as I look to use it in production this summer. I was NOT able to find a way to have it discern between restart types. The only thing I found was how to reset the restart counter for an app (along with other meta data for the application), like so:
$ pm2 reset <app id>

Related

Disable PM2 logging(.pm2/pm2.log NOT .pm2/logs)

I have to run a process with PM2 on a device with very limited disk space, so I need to disable all logging otherwise the device will run out of space after several days.
I'm using:
"out_file": "/dev/null",
"error_file": "/dev/null"
It stops PM2 from creating logs for the process. However, PM2 still creates another log file in .pm2/pm2.log
The size of pm2.log can grow up to 9Mb, which is large for the device.
Is there any way to stop PM2 from creating pm2.log? Or at least some way to clean the log file automatically.
I open an issue at https://github.com/Unitech/pm2/issues/2921
Turns out that pm2.log cannot be disabled in current version of PM2(2.4.6), but the maintainer of PM2 said that this feature will be added in future update.
For now, I'm using the following code to clean log files:
var cmd_flush ='pm2 flush';
var exec = require('child_process').exec;
exec(cmd_flush ,function(error){
debugLog('error: ' + error);
});
It's slow, but at least it works.

Deploy NodeJS app with pm2 and Capistrano

I develop project based on NodeJs, pm2, Capistrano 3.
Faced with problem of downtime while deploying Node app with Capistrano.
deploy.rb:
set :linked_dirs, ['node_modules', 'logs']
set :linked_files, ['ecosystem.json']
set :npm_flags, '--silent --no-spin'
before 'deploy:updated', 'assets:upload'
after 'deploy:updated', 'assets:webpack'
after 'deploy:publishing', 'pm2:restart'
assets:upload - builds js and css files and uploads to CDN. Build performs with Webpack so it's create webpack-assets.json.
assets:webpack - uploads webpack-assets.json to prod servers. webpack-assets.json is using by node to get exact asset name because it contains hash:
task :webpack do
run_locally do
roles(:web).each do |host|
execute :rsync, '-rvzu', "themes-assets.json", "#{host.user}##{host.hostname}:#{fetch(:release_path)}"
execute :rsync, '-rvzu', "webpack-assets.json", "#{host.user}##{host.hostname}:#{fetch(:release_path)}"
end
end
end
pm2:restart - should perform zero time reload. But in fact I'm getting 1second down time. If I perform this task independently there is no downtime.
def restart_app
within current_path do
execute :pm2, :startOrRestart, fetch(:deploy_to) + '/shared/ecosystem.json'
end
end
pm2 logs show the following error
Process with pid 123169 still not killed, retrying...
Instead of
pm2 startOrRestart <app|conf>
You have to use
pm2 startOrReload <app|conf>
If you still see a downtime while using "startOrReload", have a look at: http://pm2.keymetrics.io/docs/usage/signals-clean-restart/#graceful-start

how to detect what triggered PM2 watch restart

Is there some kind of debug option to find out which file exactly was changed and caused PM2 to restart when it's used with --watch key?
At least in my version 4.5.5 it is quite simple - just show the logs. But the trick is that you should not show the log from the process itself. Therefore just execute pm2 logs without specifying the process.
What you will get is something like this:
PM2 | Change detected on path fallback-backend/logger/logfiles/info.log for app SmartProduction-Fallback - restarting

Correct way to restar/reload application for a different release

I have following folder structure:
current
releases
2192091029019/
1029012901920/
Latest release gets pushed to current folder, and I afterwards start it wiht pm2 start, however If I upload new release with different folder name and do pm2 reload from new folder it still trys to reference original release from where application was started. Is there a way to restart application respecting new code?
I have same problem with this release structure but with supervisord+Rails instead pm2 + node.
In my case i need to completely restart supervisord every deploy to fix that.
So in your case it may work like this:
pm2 stop
kill -SIGTERM {pm2_pid}
pm2 startup
It's hackish but working solution.

Jenkins nodejs pulgin long running running pm2 deamons

At the end of my Jenkins build I use the nodejs plugin to kick off pm2 to spawn a few deamons. When the build finishes I get the message:
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
Looking at this, and the corrosponding https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller it suggests that I can run something like:
BUILD_ID=dontKillMe pm2 start processes.json
But the deamons are not running after the build completes. Any ideas?
Thanks, Chris

Resources