Runtime4Docker.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 'use strict';
  2. /**
  3. * Specialized PM2 CLI for Containers
  4. */
  5. var commander = require('commander');
  6. var PM2 = require('../..');
  7. var Log = require('../../lib/API/Log');
  8. var cst = require('../../constants.js');
  9. var pkg = require('../../package.json');
  10. var path = require('path');
  11. var DEFAULT_FAIL_COUNT = 3;
  12. process.env.PM2_DISCRETE_MODE = true;
  13. commander.version(pkg.version)
  14. .description('pm2-runtime is a drop-in replacement Node.js binary for containers')
  15. .option('-i --instances <number>', 'launch [number] of processes automatically load-balanced. Increase overall performances and performance stability.')
  16. .option('--secret [key]', '[MONITORING] PM2 plus secret key')
  17. .option('--no-autorestart', 'start an app without automatic restart')
  18. .option('--node-args <node_args>', 'space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"')
  19. .option('-n --name <name>', 'set a <name> for script')
  20. .option('--max-memory-restart <memory>', 'specify max memory amount used to autorestart (in octet or use syntax like 100M)')
  21. .option('-c --cron <cron_pattern>', 'restart a running process based on a cron pattern')
  22. .option('--interpreter <interpreter>', 'the interpreter pm2 should use for executing app (bash, python...)')
  23. .option('--public [key]', '[MONITORING] PM2 plus public key')
  24. .option('--machine-name [name]', '[MONITORING] PM2 plus machine name')
  25. .option('--trace', 'enable transaction tracing with km')
  26. .option('--v8', 'enable v8 data collecting')
  27. .option('--format', 'output logs formated like key=val')
  28. .option('--raw', 'raw output (default mode)')
  29. .option('--formatted', 'formatted log output |id|app|log')
  30. .option('--json', 'output logs in json format')
  31. .option('--delay <seconds>', 'delay start of configuration file by <seconds>', 0)
  32. .option('--web [port]', 'launch process web api on [port] (default to 9615)')
  33. .option('--only <application-name>', 'only act on one application of configuration')
  34. .option('--no-auto-exit', 'do not exit if all processes are errored/stopped or 0 apps launched')
  35. .option('--env [name]', 'inject env_[name] env variables in process config file')
  36. .option('--watch', 'watch and restart application on file change')
  37. .option('--error <path>', 'error log file destination (default disabled)', '/dev/null')
  38. .option('--output <path>', 'output log file destination (default disabled)', '/dev/null')
  39. .option('--deep-monitoring', 'enable all monitoring tools (equivalent to --v8 --event-loop-inspector --trace)')
  40. .allowUnknownOption()
  41. .usage('app.js');
  42. commander.command('*')
  43. .action(function(cmd){
  44. Runtime.instanciate(cmd);
  45. });
  46. commander.command('start <app.js|json_file>')
  47. .description('start an application or json ecosystem file')
  48. .action(function(cmd) {
  49. Runtime.instanciate(cmd);
  50. });
  51. if (process.argv.length == 2) {
  52. commander.outputHelp();
  53. process.exit(1);
  54. }
  55. var Runtime = {
  56. pm2 : null,
  57. instanciate : function(cmd) {
  58. this.pm2 = new PM2.custom({
  59. pm2_home : process.env.PM2_HOME || path.join(process.env.HOME, '.pm2'),
  60. secret_key : cst.SECRET_KEY || commander.secret,
  61. public_key : cst.PUBLIC_KEY || commander.public,
  62. machine_name : cst.MACHINE_NAME || commander.machineName,
  63. daemon_mode : process.env.PM2_RUNTIME_DEBUG || false
  64. });
  65. this.pm2.connect(function(err, pm2_meta) {
  66. process.on('SIGINT', function() {
  67. Runtime.exit();
  68. });
  69. process.on('SIGTERM', function() {
  70. Runtime.exit();
  71. });
  72. Runtime.startLogStreaming();
  73. Runtime.startApp(cmd, function(err) {
  74. if (err) {
  75. console.error(err.message || err);
  76. return Runtime.exit();
  77. }
  78. });
  79. });
  80. },
  81. /**
  82. * Log Streaming Management
  83. */
  84. startLogStreaming : function() {
  85. if (commander.json === true)
  86. Log.jsonStream(this.pm2.Client, 'all');
  87. else if (commander.format === true)
  88. Log.formatStream(this.pm2.Client, 'all', false, 'YYYY-MM-DD-HH:mm:ssZZ');
  89. else
  90. Log.stream(this.pm2.Client, 'all', !commander.formatted, commander.timestamp, true);
  91. },
  92. /**
  93. * Application Startup
  94. */
  95. startApp : function(cmd, cb) {
  96. function exec() {
  97. this.pm2.start(cmd, commander, function(err, obj) {
  98. if (err)
  99. return cb(err);
  100. if (obj && obj.length == 0)
  101. return cb(new Error(`0 application started (no apps to run on ${cmd})`))
  102. if (commander.web) {
  103. var port = commander.web === true ? cst.WEB_PORT : commander.web;
  104. Runtime.pm2.web(port);
  105. }
  106. if (commander.autoExit) {
  107. setTimeout(function() {
  108. Runtime.autoExitWorker();
  109. }, 4000);
  110. }
  111. // For Testing purpose (allow to auto exit CLI)
  112. if (process.env.PM2_RUNTIME_DEBUG)
  113. Runtime.pm2.disconnect(function() {});
  114. return cb(null, obj);
  115. });
  116. }
  117. // via --delay <seconds> option
  118. setTimeout(exec.bind(this), commander.delay * 1000);
  119. },
  120. /**
  121. * Exit runtime mgmt
  122. */
  123. exit : function(code) {
  124. if (!this.pm2) return process.exit(1);
  125. this.pm2.kill(function() {
  126. process.exit(code || 0);
  127. });
  128. },
  129. /**
  130. * Exit current PM2 instance if 0 app is online
  131. * function activated via --auto-exit
  132. */
  133. autoExitWorker : function(fail_count) {
  134. var interval = 2000;
  135. if (typeof(fail_count) =='undefined')
  136. fail_count = DEFAULT_FAIL_COUNT;
  137. var timer = setTimeout(function () {
  138. Runtime.pm2.list(function (err, apps) {
  139. if (err) {
  140. console.error('Could not run pm2 list');
  141. return Runtime.autoExitWorker();
  142. }
  143. var appOnline = 0;
  144. apps.forEach(function (app) {
  145. if (!app.pm2_env.pmx_module &&
  146. (app.pm2_env.status === cst.ONLINE_STATUS ||
  147. app.pm2_env.status === cst.LAUNCHING_STATUS)) {
  148. appOnline++;
  149. }
  150. });
  151. if (appOnline === 0) {
  152. console.log('0 application online, retry =', fail_count);
  153. if (fail_count <= 0)
  154. return Runtime.exit(2);
  155. return Runtime.autoExitWorker(--fail_count);
  156. }
  157. Runtime.autoExitWorker();
  158. });
  159. }, interval);
  160. timer.unref();
  161. }
  162. }
  163. commander.parse(process.argv);