Unitech/pm2 — pull request #321 by DookTibs (2014-02-24 18:39:32+00:00)
Title: -added a "getValOrDefault" convenience function and calls for optionally...

Made two main changes in this fork. Hopefully I am following proper github etiquette here; if not I apologize, this is my first pull request:

1. I think I found a bug in God.js where clu.pm2_env.min_uptime configuration value was never being used; code was doing something like "if (1000 || min_uptime)" instead of "if (min_uptime || 1000)", so the 1000 was always being used. Fixed that, added a new convenience method for using default config values, and made a few other changes to make the unstable restart monitor period (previously hardcoded at 15000 ms) and the max number of allowable unstable restarts (previously hardcoded at 15) so that they could be specified in the json configuration file.

2. added a way to specify some customized code that will execute when pm2 detects that an app has crashed/exited. The motivation here was so that you could do satisfy a requirement like "email site admins if the app fatally crashes, but do something else if it crashed but pm2 did a restart." I didn't see an existing facility for this type of behavior but if it exists then ignore this pull!

Anyway way it works is, you can optionally specify the path to a Node script in the pm2 configuration file. pm2 will attempt to require that module and will look for an exported function "appExited" within it. When pm2 detects a crash/exit, it will call that function and pass in the details (path, id, exit code, numRestarts, is the app being stopped for good) as arguments, and the custom code in the user's script can do whatever is appropriate. I think this is ok from a security standpoint but see what you think. I also readily admit that this may not be a good fit for every pm2 use case, like using it to monitor multiple several different node scripts or using clustering, so it's possible this introduces problems in one of those areas. I think it's useful functionality but may not be the direction you want to take.

Hopefully some of this is helpful. Thx.