ClusterMode.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Cluster execution functions related
  9. * @author Alexandre Strzelewicz <as@unitech.io>
  10. * @project PM2
  11. */
  12. var cluster = require('cluster');
  13. var Utility = require('../Utility.js');
  14. var pkg = require('../../package.json');
  15. /**
  16. * Description
  17. * @method exports
  18. * @param {} God
  19. * @return
  20. */
  21. module.exports = function ClusterMode(God) {
  22. /**
  23. * For Node apps - Cluster mode
  24. * It will wrap the code and enable load-balancing mode
  25. * @method nodeApp
  26. * @param {} env_copy
  27. * @param {} cb
  28. * @return Literal
  29. */
  30. God.nodeApp = function nodeApp(env_copy, cb){
  31. var clu = null;
  32. console.log(`App [${env_copy.name}:${env_copy.pm_id}] starting in -cluster mode-`)
  33. if (env_copy.node_args && Array.isArray(env_copy.node_args)) {
  34. cluster.settings.execArgv = env_copy.node_args;
  35. }
  36. env_copy._pm2_version = pkg.version;
  37. try {
  38. // node.js cluster clients can not receive deep-level objects or arrays in the forked process, e.g.:
  39. // { "args": ["foo", "bar"], "env": { "foo1": "bar1" }} will be parsed to
  40. // { "args": "foo, bar", "env": "[object Object]"}
  41. // So we passing a stringified JSON here.
  42. clu = cluster.fork({pm2_env: JSON.stringify(env_copy), windowsHide: true});
  43. } catch(e) {
  44. God.logAndGenerateError(e);
  45. return cb(e);
  46. }
  47. clu.pm2_env = env_copy;
  48. /**
  49. * Broadcast message to God
  50. */
  51. clu.on('message', function cluMessage(msg) {
  52. /*********************************
  53. * If you edit this function
  54. * Do the same in ForkMode.js !
  55. *********************************/
  56. if (msg.data && msg.type) {
  57. return God.bus.emit(msg.type ? msg.type : 'process:msg', {
  58. at : Utility.getDate(),
  59. data : msg.data,
  60. process : {
  61. pm_id : clu.pm2_env.pm_id,
  62. name : clu.pm2_env.name,
  63. rev : (clu.pm2_env.versioning && clu.pm2_env.versioning.revision) ? clu.pm2_env.versioning.revision : null,
  64. namespace : clu.pm2_env.namespace
  65. }
  66. });
  67. }
  68. else {
  69. if (typeof msg == 'object' && 'node_version' in msg) {
  70. clu.pm2_env.node_version = msg.node_version;
  71. return false;
  72. }
  73. return God.bus.emit('process:msg', {
  74. at : Utility.getDate(),
  75. raw : msg,
  76. process : {
  77. pm_id : clu.pm2_env.pm_id,
  78. name : clu.pm2_env.name,
  79. namespace : clu.pm2_env.namespace
  80. }
  81. });
  82. }
  83. });
  84. return cb(null, clu);
  85. };
  86. };