Runtime.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. var commander = require('commander');
  3. var PM2 = require('../..');
  4. var Log = require('../../lib/API/Log');
  5. var cst = require('../../constants.js');
  6. var pkg = require('../../package.json');
  7. var path = require('path');
  8. var pm2;
  9. // Do not print banner
  10. process.env.PM2_DISCRETE_MODE = true;
  11. commander.version(pkg.version)
  12. .description('pm2-runtime is an automatic pmx injection that runs in simulated no-daemon environment')
  13. .option('--auto-manage', 'keep application online after command exit')
  14. .option('--fast-boot', 'boot app faster by keeping pm2 runtime online in background (effective at second exit/start)')
  15. .option('--web [port]', 'launch process web api on [port] default to 9615')
  16. .option('--secret [key]', 'PM2 plus secret key')
  17. .option('--public [key]', 'PM2 plus public key')
  18. .option('--machine-name [name]', 'PM2 plus machine name')
  19. .option('--env [name]', 'select env_[name] env variables in process config file')
  20. .option('--watch', 'Watch and Restart')
  21. .option('-i --instances <number>', 'launch [number] instances with load-balancer')
  22. .usage('pm2-runtime app.js');
  23. commander.command('*')
  24. .action(function(cmd){
  25. pm2 = new PM2.custom({
  26. pm2_home : path.join(process.env.HOME, '.pm3'),
  27. secret_key : cst.SECRET_KEY || commander.secret,
  28. public_key : cst.PUBLIC_KEY || commander.public,
  29. machine_name : cst.MACHINE_NAME || commander.machineName
  30. });
  31. pm2.connect(function() {
  32. if (commander.web) {
  33. var port = commander.web === true ? cst.WEB_PORT : commander.web;
  34. pm2.web(port);
  35. }
  36. pm2.start(cmd, commander, function(err, obj) {
  37. if (process.env.PM2_RUNTIME_DEBUG) {
  38. return pm2.disconnect(function() {});
  39. }
  40. if (err) {
  41. console.error(err);
  42. return process.exit(1);
  43. }
  44. var pm_id = obj[0].pm2_env.pm_id;
  45. if (commander.instances == undefined) {
  46. return pm2.attach(pm_id, function() {
  47. exitPM2();
  48. });
  49. }
  50. if (commander.json === true)
  51. Log.jsonStream(pm2.Client, pm_id);
  52. else if (commander.format === true)
  53. Log.formatStream(pm2.Client, pm_id, false, 'YYYY-MM-DD-HH:mm:ssZZ');
  54. else
  55. Log.stream(pm2.Client, 'all', true);
  56. });
  57. });
  58. });
  59. if (process.argv.length == 2) {
  60. commander.outputHelp();
  61. process.exit(1);
  62. }
  63. process.on('SIGINT', function() {
  64. exitPM2();
  65. });
  66. process.on('SIGTERM', function() {
  67. exitPM2();
  68. });
  69. commander.parse(process.argv);
  70. function exitPM2() {
  71. console.log('Exited at %s', new Date());
  72. if (commander.autoManage)
  73. return process.exit(0);
  74. if (commander.fastBoot) {
  75. return pm2.delete('all', function() {
  76. process.exit(0);
  77. });
  78. }
  79. pm2.kill(function() {
  80. process.exit(0);
  81. });
  82. }