Methods.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * Copyright 2013-2022 the PM2 project authors. All rights reserved.
  3. * Use of this source code is governed by a license that
  4. * can be found in the LICENSE file.
  5. */
  6. 'use strict';
  7. /**
  8. * @file Utilities for PM2
  9. * @author Alexandre Strzelewicz <as@unitech.io>
  10. * @project PM2
  11. */
  12. var p = require('path');
  13. var treekill = require('../TreeKill');
  14. var cst = require('../../constants.js');
  15. /**
  16. * Description
  17. * @method exports
  18. * @param {} God
  19. * @return
  20. */
  21. module.exports = function(God) {
  22. /**
  23. * Description
  24. * @method logAndGenerateError
  25. * @param {} err
  26. * @return NewExpression
  27. */
  28. God.logAndGenerateError = function(err) {
  29. // Is an Error object
  30. if (err instanceof Error) {
  31. console.trace(err);
  32. return err;
  33. }
  34. // Is a JSON or simple string
  35. console.error(err);
  36. return new Error(err);
  37. };
  38. /**
  39. * Utility functions
  40. * @method getProcesses
  41. * @return MemberExpression
  42. */
  43. God.getProcesses = function() {
  44. return God.clusters_db;
  45. };
  46. God.getFormatedProcess = function getFormatedProcesses(id) {
  47. if (God.clusters_db[id])
  48. return {
  49. pid : God.clusters_db[id].process.pid,
  50. name : God.clusters_db[id].pm2_env.name,
  51. pm2_env : God.clusters_db[id].pm2_env,
  52. pm_id : God.clusters_db[id].pm2_env.pm_id
  53. };
  54. return {};
  55. };
  56. /**
  57. * Get formated processes
  58. * @method getFormatedProcesses
  59. * @return {Array} formated processes
  60. */
  61. God.getFormatedProcesses = function getFormatedProcesses() {
  62. var keys = Object.keys(God.clusters_db);
  63. var arr = new Array();
  64. var kl = keys.length;
  65. for (var i = 0; i < kl; i++) {
  66. var key = keys[i];
  67. if (!God.clusters_db[key]) continue;
  68. // Avoid _old type pm_ids
  69. if (isNaN(God.clusters_db[key].pm2_env.pm_id)) continue;
  70. arr.push({
  71. pid : God.clusters_db[key].process.pid,
  72. name : God.clusters_db[key].pm2_env.name,
  73. pm2_env : God.clusters_db[key].pm2_env,
  74. pm_id : God.clusters_db[key].pm2_env.pm_id
  75. })
  76. }
  77. return arr;
  78. };
  79. /**
  80. * Description
  81. * @method findProcessById
  82. * @param {} id
  83. * @return ConditionalExpression
  84. */
  85. God.findProcessById = function findProcessById(id) {
  86. return God.clusters_db[id] ? God.clusters_db[id] : null;
  87. };
  88. /**
  89. * Description
  90. * @method findByName
  91. * @param {} name
  92. * @return arr
  93. */
  94. God.findByName = function(name) {
  95. var db = God.clusters_db;
  96. var arr = [];
  97. if (name == 'all') {
  98. for (var key in db) {
  99. // Avoid _old_proc process style
  100. if (typeof(God.clusters_db[key].pm2_env.pm_id) === 'number')
  101. arr.push(db[key]);
  102. }
  103. return arr;
  104. }
  105. for (var key in db) {
  106. if (God.clusters_db[key].pm2_env.name == name ||
  107. God.clusters_db[key].pm2_env.pm_exec_path == p.resolve(name)) {
  108. arr.push(db[key]);
  109. }
  110. }
  111. return arr;
  112. };
  113. /**
  114. * Check if a process is alive in system processes
  115. * Return TRUE if process online
  116. * @method checkProcess
  117. * @param {} pid
  118. * @return
  119. */
  120. God.checkProcess = function(pid) {
  121. if (!pid) return false;
  122. try {
  123. // Sending 0 signal do not kill the process
  124. process.kill(pid, 0);
  125. return true;
  126. }
  127. catch (err) {
  128. return false;
  129. }
  130. };
  131. /**
  132. * Description
  133. * @method processIsDead
  134. * @param {} pid
  135. * @param {} cb
  136. * @return Literal
  137. */
  138. God.processIsDead = function(pid, pm2_env, cb, sigkill) {
  139. if (!pid) return cb({type : 'param:missing', msg : 'no pid passed'});
  140. var timeout = null;
  141. var kill_timeout = (pm2_env && pm2_env.kill_timeout) ? pm2_env.kill_timeout : cst.KILL_TIMEOUT;
  142. var mode = pm2_env.exec_mode;
  143. var timer = setInterval(function() {
  144. if (God.checkProcess(pid) === false) {
  145. console.log('pid=%d msg=process killed', pid);
  146. clearTimeout(timeout);
  147. clearInterval(timer);
  148. return cb(null, true);
  149. }
  150. console.log('pid=%d msg=failed to kill - retrying in %dms', pid, pm2_env.kill_retry_time);
  151. return false;
  152. }, pm2_env.kill_retry_time);
  153. timeout = setTimeout(function() {
  154. clearInterval(timer);
  155. if (sigkill) {
  156. console.log('Process with pid %d could not be killed', pid);
  157. return cb({type : 'timeout', msg : 'timeout'});
  158. }
  159. else {
  160. console.log('Process with pid %d still alive after %sms, sending it SIGKILL now...', pid, kill_timeout);
  161. if (pm2_env.treekill !== true) {
  162. try {
  163. process.kill(parseInt(pid), 'SIGKILL');
  164. } catch(e) {
  165. console.error('[SimpleKill][SIGKILL] %s pid can not be killed', pid, e.stack, e.message);
  166. }
  167. return God.processIsDead(pid, pm2_env, cb, true);
  168. }
  169. else {
  170. treekill(parseInt(pid), 'SIGKILL', function(err) {
  171. return God.processIsDead(pid, pm2_env, cb, true);
  172. });
  173. }
  174. }
  175. }, kill_timeout);
  176. return false;
  177. };
  178. /**
  179. * Description
  180. * @method killProcess
  181. * @param int pid
  182. * @param Object pm2_env
  183. * @param function cb
  184. * @return CallExpression
  185. */
  186. God.killProcess = function(pid, pm2_env, cb) {
  187. if (!pid) return cb({msg : 'no pid passed or null'});
  188. if (typeof(pm2_env.pm_id) === 'number' &&
  189. (cst.KILL_USE_MESSAGE || pm2_env.shutdown_with_message == true)) {
  190. var proc = God.clusters_db[pm2_env.pm_id];
  191. if (proc && proc.send) {
  192. try {
  193. proc.send('shutdown');
  194. } catch (e) {
  195. console.error(`[AppKill] Cannot send "shutdown" message to ${pid}`)
  196. console.error(e.stack, e.message)
  197. }
  198. return God.processIsDead(pid, pm2_env, cb);
  199. }
  200. else {
  201. console.log(`[AppKill] ${pid} pid cannot be notified with send()`)
  202. }
  203. }
  204. if (pm2_env.treekill !== true) {
  205. try {
  206. process.kill(parseInt(pid), cst.KILL_SIGNAL);
  207. } catch(e) {
  208. console.error('[SimpleKill] %s pid can not be killed', pid, e.stack, e.message);
  209. }
  210. return God.processIsDead(pid, pm2_env, cb);
  211. }
  212. else {
  213. treekill(parseInt(pid), cst.KILL_SIGNAL, function(err) {
  214. return God.processIsDead(pid, pm2_env, cb);
  215. });
  216. }
  217. };
  218. /**
  219. * Description
  220. * @method getNewId
  221. * @return UpdateExpression
  222. */
  223. God.getNewId = function() {
  224. return God.next_id++;
  225. };
  226. /**
  227. * When a process is restarted or reloaded reset fields
  228. * to monitor unstable starts
  229. * @method resetState
  230. * @param {} pm2_env
  231. * @return
  232. */
  233. God.resetState = function(pm2_env) {
  234. pm2_env.created_at = Date.now();
  235. pm2_env.unstable_restarts = 0;
  236. pm2_env.prev_restart_delay = 0;
  237. };
  238. };