@ohos-ports/pm2 0.0.0-stage → 7.1.2-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/CHANGELOG.md +2607 -0
  2. package/CONTRIBUTING.md +124 -0
  3. package/GNU-AGPL-3.0.txt +665 -0
  4. package/LICENSE +1 -0
  5. package/README.md +255 -2
  6. package/SECURITY.md +41 -0
  7. package/bin/pm2 +3 -0
  8. package/bin/pm2-dev +3 -0
  9. package/bin/pm2-docker +3 -0
  10. package/bin/pm2-runtime +3 -0
  11. package/bin/pm2.ps1 +3 -0
  12. package/constants.js +113 -0
  13. package/index.js +12 -0
  14. package/lib/API/Configuration.js +212 -0
  15. package/lib/API/Containerizer.js +335 -0
  16. package/lib/API/Dashboard.js +726 -0
  17. package/lib/API/Deploy.js +117 -0
  18. package/lib/API/Extra.js +798 -0
  19. package/lib/API/ExtraMgmt/Docker.js +30 -0
  20. package/lib/API/Log.js +388 -0
  21. package/lib/API/LogManagement.js +351 -0
  22. package/lib/API/Modules/LOCAL.js +122 -0
  23. package/lib/API/Modules/Modularizer.js +148 -0
  24. package/lib/API/Modules/NPM.js +443 -0
  25. package/lib/API/Modules/TAR.js +368 -0
  26. package/lib/API/Modules/flagExt.js +46 -0
  27. package/lib/API/Modules/index.js +120 -0
  28. package/lib/API/Monit.js +247 -0
  29. package/lib/API/Serve.js +497 -0
  30. package/lib/API/Startup.js +630 -0
  31. package/lib/API/UX/helpers.js +213 -0
  32. package/lib/API/UX/index.js +9 -0
  33. package/lib/API/UX/pm2-describe.js +197 -0
  34. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  35. package/lib/API/UX/pm2-ls.js +581 -0
  36. package/lib/API/Version.js +383 -0
  37. package/lib/API/interpreter.json +12 -0
  38. package/lib/API/pm2-plus/PM2IO.js +381 -0
  39. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
  40. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
  41. package/lib/API/pm2-plus/helpers.js +97 -0
  42. package/lib/API/pm2-plus/link.js +126 -0
  43. package/lib/API/pm2-plus/pres/motd +16 -0
  44. package/lib/API/pm2-plus/pres/motd.update +26 -0
  45. package/lib/API/pm2-plus/pres/welcome +28 -0
  46. package/lib/API/pm2-plus/process-selector.js +52 -0
  47. package/lib/API/schema.json +375 -0
  48. package/lib/API.js +1980 -0
  49. package/lib/Client.js +793 -0
  50. package/lib/Common.js +949 -0
  51. package/lib/Configuration.js +309 -0
  52. package/lib/Daemon.js +468 -0
  53. package/lib/Event.js +37 -0
  54. package/lib/God/ActionMethods.js +955 -0
  55. package/lib/God/ClusterMode.js +192 -0
  56. package/lib/God/ForkMode.js +205 -0
  57. package/lib/God/Methods.js +292 -0
  58. package/lib/God/Reload.js +300 -0
  59. package/lib/God/StdioLogger.js +239 -0
  60. package/lib/God.js +653 -0
  61. package/lib/HttpInterface.js +75 -0
  62. package/lib/OtelManager.js +74 -0
  63. package/lib/ProcessContainer.js +321 -0
  64. package/lib/ProcessContainerBun.js +355 -0
  65. package/lib/ProcessContainerFork.js +42 -0
  66. package/lib/ProcessContainerForkBun.js +31 -0
  67. package/lib/ProcessUtils.js +74 -0
  68. package/lib/TreeKill.js +111 -0
  69. package/lib/Utility.js +304 -0
  70. package/lib/VersionCheck.js +46 -0
  71. package/lib/Watcher.js +117 -0
  72. package/lib/Worker.js +200 -0
  73. package/lib/binaries/CLI.js +1092 -0
  74. package/lib/binaries/DevCLI.js +183 -0
  75. package/lib/binaries/Runtime.js +101 -0
  76. package/lib/binaries/Runtime4Docker.js +192 -0
  77. package/lib/completion.js +229 -0
  78. package/lib/completion.sh +40 -0
  79. package/lib/motd +36 -0
  80. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  81. package/lib/templates/ecosystem-es.tpl +24 -0
  82. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  83. package/lib/templates/ecosystem-simple.tpl +6 -0
  84. package/lib/templates/ecosystem.tpl +22 -0
  85. package/lib/templates/init-scripts/launchd.tpl +35 -0
  86. package/lib/templates/init-scripts/openrc.tpl +52 -0
  87. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  88. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  89. package/lib/templates/init-scripts/rcd.tpl +44 -0
  90. package/lib/templates/init-scripts/smf.tpl +43 -0
  91. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  92. package/lib/templates/init-scripts/systemd.tpl +22 -0
  93. package/lib/templates/init-scripts/upstart.tpl +103 -0
  94. package/lib/templates/logrotate.d/pm2 +10 -0
  95. package/lib/templates/sample-apps/http-server/README.md +14 -0
  96. package/lib/templates/sample-apps/http-server/api.js +9 -0
  97. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  98. package/lib/templates/sample-apps/http-server/package.json +11 -0
  99. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  100. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  101. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  102. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  103. package/lib/templates/sample-apps/python-app/README.md +4 -0
  104. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  105. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  106. package/lib/templates/sample-apps/python-app/package.json +11 -0
  107. package/lib/tools/Config.js +251 -0
  108. package/lib/tools/SysMetrics.js +375 -0
  109. package/lib/tools/copydirSync.js +101 -0
  110. package/lib/tools/deleteFolderRecursive.js +19 -0
  111. package/lib/tools/find-package-json.js +74 -0
  112. package/lib/tools/fmt.js +72 -0
  113. package/lib/tools/interpreter.js +88 -0
  114. package/lib/tools/isbinaryfile.js +94 -0
  115. package/lib/tools/json5.js +752 -0
  116. package/lib/tools/multimeter/bar.js +111 -0
  117. package/lib/tools/multimeter/charm/encode.js +18 -0
  118. package/lib/tools/multimeter/charm/index.js +305 -0
  119. package/lib/tools/multimeter/index.js +76 -0
  120. package/lib/tools/open.js +56 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/pm2-config-keys.js +165 -0
  123. package/lib/tools/prompt.js +76 -0
  124. package/lib/tools/serviceStop.js +149 -0
  125. package/lib/tools/sexec.js +55 -0
  126. package/lib/tools/treeify.js +113 -0
  127. package/lib/tools/typestrip.js +55 -0
  128. package/lib/tools/which.js +120 -0
  129. package/lib/tools/xdg-open +861 -0
  130. package/modules/blessed/LICENSE +20 -0
  131. package/modules/blessed/README.md +34 -0
  132. package/modules/blessed/index.js +1 -0
  133. package/modules/blessed/lib/alias.js +526 -0
  134. package/modules/blessed/lib/blessed.js +32 -0
  135. package/modules/blessed/lib/colors.js +530 -0
  136. package/modules/blessed/lib/events.js +189 -0
  137. package/modules/blessed/lib/gpmclient.js +221 -0
  138. package/modules/blessed/lib/helpers.js +165 -0
  139. package/modules/blessed/lib/keys.js +339 -0
  140. package/modules/blessed/lib/program.js +4297 -0
  141. package/modules/blessed/lib/tput.js +3022 -0
  142. package/modules/blessed/lib/unicode.js +885 -0
  143. package/modules/blessed/lib/widget.js +32 -0
  144. package/modules/blessed/lib/widgets/box.js +34 -0
  145. package/modules/blessed/lib/widgets/element.js +2570 -0
  146. package/modules/blessed/lib/widgets/list.js +599 -0
  147. package/modules/blessed/lib/widgets/log.js +83 -0
  148. package/modules/blessed/lib/widgets/node.js +282 -0
  149. package/modules/blessed/lib/widgets/screen.js +2298 -0
  150. package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
  151. package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
  152. package/modules/blessed/lib/widgets/text.js +35 -0
  153. package/modules/blessed/usr/linux +0 -0
  154. package/modules/blessed/usr/windows-ansi +0 -0
  155. package/modules/blessed/usr/xterm +0 -0
  156. package/modules/blessed/usr/xterm-256color +0 -0
  157. package/modules/blessed/usr/xterm.termcap +243 -0
  158. package/modules/chokidar/LICENSE +21 -0
  159. package/modules/chokidar/VENDORED.md +76 -0
  160. package/modules/chokidar/index.js +979 -0
  161. package/modules/chokidar/lib/brace-expand.js +180 -0
  162. package/modules/chokidar/lib/constants.js +66 -0
  163. package/modules/chokidar/lib/fsevents-handler.js +526 -0
  164. package/modules/chokidar/lib/nodefs-handler.js +654 -0
  165. package/modules/fclone.js +49 -0
  166. package/modules/pm2-axon/index.js +2 -0
  167. package/modules/pm2-axon/lib/configurable/History.md +5 -0
  168. package/modules/pm2-axon/lib/configurable/Makefile +7 -0
  169. package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
  170. package/modules/pm2-axon/lib/configurable/index.js +2 -0
  171. package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
  172. package/modules/pm2-axon/lib/configurable/package.json +68 -0
  173. package/modules/pm2-axon/lib/index.js +44 -0
  174. package/modules/pm2-axon/lib/plugins/queue.js +71 -0
  175. package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
  176. package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
  177. package/modules/pm2-axon/lib/sockets/pub.js +77 -0
  178. package/modules/pm2-axon/lib/sockets/pull.js +37 -0
  179. package/modules/pm2-axon/lib/sockets/push.js +32 -0
  180. package/modules/pm2-axon/lib/sockets/rep.js +73 -0
  181. package/modules/pm2-axon/lib/sockets/req.js +101 -0
  182. package/modules/pm2-axon/lib/sockets/sock.js +430 -0
  183. package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
  184. package/modules/pm2-axon/lib/sockets/sub.js +147 -0
  185. package/modules/pm2-axon/lib/utils.js +19 -0
  186. package/modules/pm2-axon-rpc/index.js +3 -0
  187. package/modules/pm2-axon-rpc/lib/client.js +63 -0
  188. package/modules/pm2-axon-rpc/lib/server.js +139 -0
  189. package/modules/pm2-io-agent/LICENSE +661 -0
  190. package/modules/pm2-io-agent/README.md +62 -0
  191. package/modules/pm2-io-agent/config.js +30 -0
  192. package/modules/pm2-io-agent/constants.js +118 -0
  193. package/modules/pm2-io-agent/index.js +2 -0
  194. package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
  195. package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
  196. package/modules/pm2-io-agent/src/PM2Client.js +112 -0
  197. package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
  198. package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
  199. package/modules/pm2-io-agent/src/Utility.js +392 -0
  200. package/modules/pm2-io-agent/src/WatchDog.js +70 -0
  201. package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
  202. package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
  203. package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
  204. package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
  205. package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
  206. package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
  207. package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
  208. package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
  209. package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
  210. package/modules/pm2-io-agent/src/utils/units.js +10 -0
  211. package/modules/pm2-io-bpm/configuration.js +141 -0
  212. package/modules/pm2-io-bpm/constants.js +10 -0
  213. package/modules/pm2-io-bpm/featureManager.js +110 -0
  214. package/modules/pm2-io-bpm/features/dependencies.js +48 -0
  215. package/modules/pm2-io-bpm/features/events.js +50 -0
  216. package/modules/pm2-io-bpm/features/metrics.js +102 -0
  217. package/modules/pm2-io-bpm/features/notify.js +238 -0
  218. package/modules/pm2-io-bpm/features/profiling.js +70 -0
  219. package/modules/pm2-io-bpm/features/tracing.js +174 -0
  220. package/modules/pm2-io-bpm/index.js +11 -0
  221. package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
  222. package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
  223. package/modules/pm2-io-bpm/metrics/network.js +132 -0
  224. package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
  225. package/modules/pm2-io-bpm/metrics/v8.js +104 -0
  226. package/modules/pm2-io-bpm/otel/constants.js +7 -0
  227. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
  228. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
  229. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
  230. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
  231. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
  232. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
  233. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
  234. package/modules/pm2-io-bpm/pmx.js +380 -0
  235. package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
  236. package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
  237. package/modules/pm2-io-bpm/serviceManager.js +21 -0
  238. package/modules/pm2-io-bpm/services/actions.js +159 -0
  239. package/modules/pm2-io-bpm/services/inspector.js +43 -0
  240. package/modules/pm2-io-bpm/services/metrics.js +186 -0
  241. package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
  242. package/modules/pm2-io-bpm/services/transport.js +11 -0
  243. package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
  244. package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
  245. package/modules/pm2-io-bpm/utils/EDS.js +113 -0
  246. package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
  247. package/modules/pm2-io-bpm/utils/autocast.js +61 -0
  248. package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
  249. package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
  250. package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
  251. package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
  252. package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
  253. package/modules/pm2-io-bpm/utils/module.js +67 -0
  254. package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
  255. package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
  256. package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
  257. package/modules/pm2-io-bpm/utils/units.js +16 -0
  258. package/modules/vizion/LICENSE +13 -0
  259. package/modules/vizion/README.md +133 -0
  260. package/modules/vizion/index.js +1 -0
  261. package/modules/vizion/lib/git/git.js +247 -0
  262. package/modules/vizion/lib/vizion.js +52 -0
  263. package/package.json +170 -4
  264. package/paths.js +121 -0
  265. package/pm2 +11 -0
  266. package/types/index.d.ts +732 -0
  267. package/types/tsconfig.json +14 -0
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Copyright 2013-present 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
+ /**
9
+ * @file Cluster execution functions related
10
+ * @author Alexandre Strzelewicz <as@unitech.io>
11
+ * @project PM2
12
+ */
13
+ var cluster = require('cluster');
14
+ var path = require('path');
15
+ var Utility = require('../Utility.js');
16
+ var pkg = require('../../package.json');
17
+ var typestrip = require('../tools/typestrip.js');
18
+ var StdioLogger = require('./StdioLogger.js');
19
+ var pm2Keys = require('../tools/pm2-config-keys.js');
20
+
21
+ /**
22
+ * Description
23
+ * @method exports
24
+ * @param {} God
25
+ * @return
26
+ */
27
+ module.exports = function ClusterMode(God) {
28
+
29
+ /**
30
+ * For Node apps - Cluster mode
31
+ * It will wrap the code and enable load-balancing mode
32
+ * @method nodeApp
33
+ * @param {} env_copy
34
+ * @param {} cb
35
+ * @return Literal
36
+ */
37
+ God.nodeApp = function nodeApp(env_copy, cb){
38
+ var clu = null;
39
+
40
+ console.log(`App [${env_copy.name}:${env_copy.pm_id}] starting in -cluster mode-`)
41
+ if (env_copy.node_args && Array.isArray(env_copy.node_args)) {
42
+ cluster.settings.execArgv = env_copy.node_args;
43
+ }
44
+
45
+ // Cluster workers run on the daemon's node, whose version can differ
46
+ // from the CLI that resolved the interpreter: ensure type stripping
47
+ // is active for .ts apps when this node needs the flag
48
+ if (path.extname(env_copy.pm_exec_path || '') === '.ts' &&
49
+ typestrip.supportLevel(process.versions.node) === 'flag' &&
50
+ cluster.settings.execArgv.indexOf('--experimental-strip-types') === -1) {
51
+ cluster.settings.execArgv = cluster.settings.execArgv.concat('--experimental-strip-types');
52
+ }
53
+
54
+ env_copy._pm2_version = pkg.version;
55
+
56
+ // The worker's stdout/stderr are piped to the daemon (cluster silent
57
+ // mode) instead of inheriting the daemon's fds: writes that bypass the
58
+ // process.stdout.write hook of ProcessContainer.js (pino/sonic-boom,
59
+ // children spawned with stdio 'inherit' like `npm start`...) used to
60
+ // land in pm2.log (#3675 #4719 #4872 #4916 #4953 #5633)
61
+ var stds = StdioLogger.stdsFromEnv(env_copy);
62
+
63
+ // open() and not Utility.startLogging: files created here (as root) are
64
+ // chowned to the app uid/gid the worker switches to before reopening them
65
+ StdioLogger.open(stds, env_copy, function(err) {
66
+ if (err) {
67
+ God.logAndGenerateError(err);
68
+ return cb(err);
69
+ }
70
+
71
+ try {
72
+ // node.js cluster clients can not receive deep-level objects or arrays in the forked process, e.g.:
73
+ // { "args": ["foo", "bar"], "env": { "foo1": "bar1" }} will be parsed to
74
+ // { "args": "foo, bar", "env": "[object Object]"}
75
+ // So we passing a stringified JSON here.
76
+ var fork_env = {};
77
+
78
+ // cluster.fork() layers the daemon env under the worker's: drop what
79
+ // filter_env excludes here, so it is not even in the worker's initial
80
+ // environment (/proc/<pid>/environ, preloaded modules). "undefined
81
+ // values in env will be ignored" (child_process, node >= 10) (#6176)
82
+ var kept = pm2Keys.filterShellEnv(process.env, env_copy.filter_env);
83
+
84
+ // same object: no filter (false, [] stored by the 7.1.1 CLI...)
85
+ if (kept !== process.env) {
86
+ Object.keys(process.env).forEach(function(key) {
87
+ if (!(key in kept)) fork_env[key] = undefined;
88
+ });
89
+
90
+ // variables read at startup (NODE_OPTIONS...) only act from the
91
+ // initial env: start the worker with the app's own, as fork mode
92
+ var app_env = env_copy.env || {};
93
+ Object.keys(app_env).forEach(function(key) {
94
+ if (typeof app_env[key] === 'string' || typeof app_env[key] === 'number')
95
+ fork_env[key] = String(app_env[key]);
96
+ });
97
+ }
98
+
99
+ // after the overlay: a daemon started from a managed app has its own
100
+ // pm2_env, which must not mask the worker's
101
+ fork_env.pm2_env = JSON.stringify(env_copy);
102
+ fork_env.windowsHide = true;
103
+
104
+ clu = cluster.fork(fork_env);
105
+ } catch(e) {
106
+ God.logAndGenerateError(e);
107
+ StdioLogger.close(stds);
108
+ return cb(e);
109
+ }
110
+
111
+ clu.pm2_env = env_copy;
112
+
113
+ var stdio = StdioLogger.attach(God, clu.process, env_copy, stds);
114
+
115
+ // Pipes closed and not the Worker 'exit': exit is relayed from ChildProcess
116
+ // exit, which fires before the stdio pipes are drained. Data still in
117
+ // flight (or written by an stdio-inherit child outliving the worker)
118
+ // would hit closed streams and be dropped from the log files
119
+ var closed = false;
120
+
121
+ StdioLogger.onPipesClosed(clu.process, function cluCloseLogs() {
122
+ closed = true;
123
+ try {
124
+ stdio.flush();
125
+ StdioLogger.close(stds);
126
+ } catch(e) { God.logAndGenerateError(e); }
127
+ });
128
+
129
+ // A stopped app stays in clusters_db: reloading its logs must not
130
+ // reopen streams nobody will ever close (fd leak)
131
+ clu._reloadLogs = function(cb) {
132
+ if (closed) return cb();
133
+ try {
134
+ StdioLogger.close(stds);
135
+ } catch(e) { God.logAndGenerateError(e); }
136
+ StdioLogger.open(stds, env_copy, function(err) {
137
+ // worker closed while reopening
138
+ if (closed) {
139
+ try { StdioLogger.close(stds); } catch(e) {}
140
+ }
141
+ return cb(err);
142
+ });
143
+ };
144
+
145
+ bindMessages(clu);
146
+
147
+ return cb(null, clu);
148
+ });
149
+ };
150
+
151
+ function bindMessages(clu) {
152
+
153
+ /**
154
+ * Broadcast message to God
155
+ */
156
+ clu.on('message', function cluMessage(msg) {
157
+ /*********************************
158
+ * If you edit this function
159
+ * Do the same in ForkMode.js !
160
+ *********************************/
161
+ if (msg.data && msg.type) {
162
+ return God.bus.emit(msg.type ? msg.type : 'process:msg', {
163
+ at : Utility.getDate(),
164
+ data : msg.data,
165
+ process : {
166
+ pm_id : clu.pm2_env.pm_id,
167
+ name : clu.pm2_env.name,
168
+ rev : (clu.pm2_env.versioning && clu.pm2_env.versioning.revision) ? clu.pm2_env.versioning.revision : null,
169
+ namespace : clu.pm2_env.namespace
170
+ }
171
+ });
172
+ }
173
+ else {
174
+
175
+ if (typeof msg == 'object' && 'node_version' in msg) {
176
+ clu.pm2_env.node_version = msg.node_version;
177
+ return false;
178
+ }
179
+
180
+ return God.bus.emit('process:msg', {
181
+ at : Utility.getDate(),
182
+ raw : msg,
183
+ process : {
184
+ pm_id : clu.pm2_env.pm_id,
185
+ name : clu.pm2_env.name,
186
+ namespace : clu.pm2_env.namespace
187
+ }
188
+ });
189
+ }
190
+ });
191
+ }
192
+ };
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Copyright 2013-present 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
+ /**
9
+ * @file Fork execution related functions
10
+ * @author Alexandre Strzelewicz <as@unitech.io>
11
+ * @project PM2
12
+ */
13
+ var log = require('debug')('pm2:fork_mode');
14
+ var fs = require('fs');
15
+ var Utility = require('../Utility.js');
16
+ var path = require('path');
17
+ var cst = require('../../constants.js');
18
+ var StdioLogger = require('./StdioLogger.js');
19
+ var runtimeOf = require('../tools/interpreter.js').runtimeOf;
20
+
21
+ /**
22
+ * Description
23
+ * @method exports
24
+ * @param {} God
25
+ * @return
26
+ */
27
+ module.exports = function ForkMode(God) {
28
+ /**
29
+ * For all apps - FORK MODE
30
+ * fork the app
31
+ * @method forkMode
32
+ * @param {} pm2_env
33
+ * @param {} cb
34
+ * @return
35
+ */
36
+ God.forkMode = function forkMode(pm2_env, cb) {
37
+ var command = '';
38
+ var args = [];
39
+
40
+ console.log(`App [${pm2_env.name}:${pm2_env.pm_id}] starting in -fork mode-`)
41
+ var spawn = require('child_process').spawn;
42
+
43
+ var interpreter = pm2_env.exec_interpreter || process.execPath;
44
+ var pidFile = pm2_env.pm_pid_path;
45
+
46
+ if (interpreter !== 'none') {
47
+ command = interpreter;
48
+
49
+ if (pm2_env.node_args && Array.isArray(pm2_env.node_args)) {
50
+ args = args.concat(pm2_env.node_args);
51
+ }
52
+
53
+ // Deprecated - to remove at some point
54
+ if (process.env.PM2_NODE_OPTIONS) {
55
+ args = args.concat(process.env.PM2_NODE_OPTIONS.split(' '));
56
+ }
57
+
58
+ var runtime = runtimeOf(interpreter);
59
+ if (runtime === 'node') {
60
+ args.push(path.resolve(path.dirname(module.filename), '..', 'ProcessContainerFork.js'));
61
+ }
62
+ else if (runtime === 'bun') {
63
+ args.push(path.resolve(path.dirname(module.filename), '..', 'ProcessContainerForkBun.js'));
64
+ }
65
+ else
66
+ args.push(pm2_env.pm_exec_path);
67
+ }
68
+ else {
69
+ command = pm2_env.pm_exec_path;
70
+ args = [ ];
71
+ }
72
+
73
+ if (pm2_env.args) {
74
+ args = args.concat(pm2_env.args);
75
+ }
76
+
77
+ // piping stream o file
78
+ var stds = StdioLogger.stdsFromEnv(pm2_env);
79
+
80
+ log("stds: %j", stds);
81
+
82
+ Utility.startLogging(stds, function(err, result) {
83
+ if (err) {
84
+ God.logAndGenerateError(err);
85
+ return cb(err);
86
+ };
87
+
88
+ try {
89
+ var spawn_env = {};
90
+ for (var k in pm2_env) {
91
+ if (pm2_env[k] !== null && pm2_env[k] !== undefined && typeof pm2_env[k] !== 'object')
92
+ spawn_env[k] = pm2_env[k];
93
+ }
94
+
95
+ var options = {
96
+ env : spawn_env,
97
+ detached : true,
98
+ cwd : pm2_env.pm_cwd || process.cwd(),
99
+ stdio : ['pipe', 'pipe', 'pipe', 'ipc'] //Same as fork() in node core
100
+ }
101
+
102
+ if (typeof(pm2_env.windowsHide) === "boolean") {
103
+ options.windowsHide = pm2_env.windowsHide;
104
+ } else {
105
+ options.windowsHide = true;
106
+ }
107
+
108
+ if (pm2_env.uid) {
109
+ options.uid = pm2_env.uid
110
+ }
111
+
112
+ if (pm2_env.gid) {
113
+ options.gid = pm2_env.gid
114
+ }
115
+
116
+ var cspr = spawn(command, args, options);
117
+ } catch(e) {
118
+ God.logAndGenerateError(e);
119
+ return cb(e);
120
+ }
121
+
122
+ if (!cspr || !cspr.stderr || !cspr.stdout) {
123
+ var fatalError = new Error('Process could not be forked properly, check your system health')
124
+ God.logAndGenerateError(fatalError);
125
+ return cb(fatalError);
126
+ }
127
+
128
+ cspr.process = {};
129
+ cspr.process.pid = cspr.pid;
130
+ cspr.pm2_env = pm2_env;
131
+
132
+ var stdio = StdioLogger.attach(God, cspr, pm2_env, stds);
133
+
134
+ /**
135
+ * Broadcast message to God
136
+ */
137
+ cspr.on('message', function forkMessage(msg) {
138
+ /*********************************
139
+ * If you edit this function
140
+ * Do the same in ClusterMode.js !
141
+ *********************************/
142
+ if (msg.data && msg.type) {
143
+ process.nextTick(function() {
144
+ return God.bus.emit(msg.type ? msg.type : 'process:msg', {
145
+ at : Utility.getDate(),
146
+ data : msg.data,
147
+ process : {
148
+ pm_id : cspr.pm2_env.pm_id,
149
+ name : cspr.pm2_env.name,
150
+ versioning : cspr.pm2_env.versioning,
151
+ namespace : cspr.pm2_env.namespace
152
+ }
153
+ });
154
+ });
155
+ }
156
+ else {
157
+
158
+ if (typeof msg == 'object' && 'node_version' in msg) {
159
+ cspr.pm2_env.node_version = msg.node_version;
160
+ return false;
161
+ }
162
+
163
+ return God.bus.emit('process:msg', {
164
+ at : Utility.getDate(),
165
+ raw : msg,
166
+ process : {
167
+ pm_id : cspr.pm2_env.pm_id,
168
+ name : cspr.pm2_env.name,
169
+ namespace : cspr.pm2_env.namespace
170
+ }
171
+ });
172
+ }
173
+ });
174
+
175
+ try {
176
+ var pid = cspr.pid
177
+ if (typeof(pid) !== 'undefined')
178
+ fs.writeFileSync(pidFile, pid.toString());
179
+ } catch (e) {
180
+ console.error(e.stack || e);
181
+ }
182
+
183
+ // once stdout/stderr are drained, see StdioLogger.onPipesClosed
184
+ StdioLogger.onPipesClosed(cspr, function forkClose() {
185
+ try {
186
+ stdio.flush();
187
+ StdioLogger.close(stds);
188
+ } catch(e) { God.logAndGenerateError(e);}
189
+ });
190
+
191
+ cspr._reloadLogs = function(cb) {
192
+ try {
193
+ StdioLogger.close(stds);
194
+ } catch(e) { God.logAndGenerateError(e);}
195
+ //cspr.removeAllListeners();
196
+ Utility.startLogging(stds, cb);
197
+ };
198
+
199
+ cspr.unref();
200
+
201
+ return cb(null, cspr);
202
+ });
203
+
204
+ };
205
+ };
@@ -0,0 +1,292 @@
1
+ /**
2
+ * Copyright 2013-present 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
+ /**
9
+ * @file Utilities for PM2
10
+ * @author Alexandre Strzelewicz <as@unitech.io>
11
+ * @project PM2
12
+ */
13
+ var p = require('path');
14
+ var treekill = require('../TreeKill');
15
+ var cst = require('../../constants.js');
16
+
17
+ /**
18
+ * Description
19
+ * @method exports
20
+ * @param {} God
21
+ * @return
22
+ */
23
+ module.exports = function(God) {
24
+
25
+ /**
26
+ * Description
27
+ * @method logAndGenerateError
28
+ * @param {} err
29
+ * @return NewExpression
30
+ */
31
+ God.logAndGenerateError = function(err) {
32
+ // Is an Error object
33
+ if (err instanceof Error) {
34
+ console.trace(err);
35
+ return err;
36
+ }
37
+ // Is a JSON or simple string
38
+ console.error(err);
39
+ return new Error(err);
40
+ };
41
+
42
+ /**
43
+ * Utility functions
44
+ * @method getProcesses
45
+ * @return MemberExpression
46
+ */
47
+ God.getProcesses = function() {
48
+ return God.clusters_db;
49
+ };
50
+
51
+ God.getFormatedProcess = function getFormatedProcesses(id) {
52
+ if (God.clusters_db[id])
53
+ return {
54
+ pid : God.clusters_db[id].process.pid,
55
+ name : God.clusters_db[id].pm2_env.name,
56
+ pm2_env : God.clusters_db[id].pm2_env,
57
+ pm_id : God.clusters_db[id].pm2_env.pm_id
58
+ };
59
+ return {};
60
+ };
61
+
62
+ /**
63
+ * Get formated processes
64
+ * @method getFormatedProcesses
65
+ * @return {Array} formated processes
66
+ */
67
+ God.getFormatedProcesses = function getFormatedProcesses() {
68
+ var keys = Object.keys(God.clusters_db);
69
+ var arr = new Array();
70
+ var kl = keys.length;
71
+
72
+ for (var i = 0; i < kl; i++) {
73
+ var key = keys[i];
74
+
75
+ if (!God.clusters_db[key]) continue;
76
+ // Avoid _old type pm_ids
77
+ if (isNaN(God.clusters_db[key].pm2_env.pm_id)) continue;
78
+
79
+ arr.push({
80
+ pid : God.clusters_db[key].process.pid,
81
+ name : God.clusters_db[key].pm2_env.name,
82
+ pm2_env : God.clusters_db[key].pm2_env,
83
+ pm_id : God.clusters_db[key].pm2_env.pm_id
84
+ })
85
+ }
86
+ return arr;
87
+ };
88
+
89
+ /**
90
+ * Description
91
+ * @method findProcessById
92
+ * @param {} id
93
+ * @return ConditionalExpression
94
+ */
95
+ God.findProcessById = function findProcessById(id) {
96
+ return God.clusters_db[id] ? God.clusters_db[id] : null;
97
+ };
98
+
99
+ /**
100
+ * Description
101
+ * @method findByName
102
+ * @param {} name
103
+ * @return arr
104
+ */
105
+ God.findByName = function(name) {
106
+ var db = God.clusters_db;
107
+ var arr = [];
108
+
109
+ if (name == 'all') {
110
+ for (var key in db) {
111
+ // Avoid _old_proc process style
112
+ if (typeof(God.clusters_db[key].pm2_env.pm_id) === 'number')
113
+ arr.push(db[key]);
114
+ }
115
+ return arr;
116
+ }
117
+
118
+ for (var key in db) {
119
+ if (God.clusters_db[key].pm2_env.name == name ||
120
+ God.clusters_db[key].pm2_env.pm_exec_path == p.resolve(name)) {
121
+ arr.push(db[key]);
122
+ }
123
+ }
124
+ return arr;
125
+ };
126
+
127
+ /**
128
+ * Check if a process is alive in system processes
129
+ * Return TRUE if process online
130
+ * @method checkProcess
131
+ * @param {} pid
132
+ * @return
133
+ */
134
+ God.checkProcess = function(pid) {
135
+ if (!pid) return false;
136
+
137
+ try {
138
+ // Sending 0 signal do not kill the process
139
+ process.kill(pid, 0);
140
+ return true;
141
+ }
142
+ catch (err) {
143
+ return false;
144
+ }
145
+ };
146
+
147
+ /**
148
+ * Description
149
+ * @method processIsDead
150
+ * @param {} pid
151
+ * @param {} cb
152
+ * @return Literal
153
+ */
154
+ God.processIsDead = function(pid, pm2_env, cb, sigkill) {
155
+ if (!pid) return cb({type : 'param:missing', msg : 'no pid passed'});
156
+
157
+ var timeout = null;
158
+ var kill_timeout = (pm2_env && pm2_env.kill_timeout) ? pm2_env.kill_timeout : cst.KILL_TIMEOUT;
159
+ var kill_retry_time = (pm2_env && pm2_env.kill_retry_time) ? pm2_env.kill_retry_time : cst.KILL_RETRY_TIME;
160
+ var treePids = pm2_env._tree_pids || [pid];
161
+
162
+ function allDead() {
163
+ for (var i = 0; i < treePids.length; i++) {
164
+ if (God.checkProcess(treePids[i]) !== false)
165
+ return false;
166
+ }
167
+ return true;
168
+ }
169
+
170
+ function survivors() {
171
+ return treePids.filter(function(p) {
172
+ return God.checkProcess(p) !== false;
173
+ });
174
+ }
175
+
176
+ // _tree_pids only has to outlive this kill sequence. Left on pm2_env it
177
+ // would follow the process into its next incarnation: exported to the
178
+ // cluster worker's process.env by ProcessContainer.js, and reused as the
179
+ // pid list of a later kill that skips treekill (shutdown_with_message).
180
+ function done(err, res) {
181
+ delete pm2_env._tree_pids;
182
+ return cb(err, res);
183
+ }
184
+
185
+ var timer = setInterval(function() {
186
+ if (allDead()) {
187
+ console.log('pid=%d msg=process tree killed (%d pids)', pid, treePids.length);
188
+ clearTimeout(timeout);
189
+ clearInterval(timer);
190
+ return done(null, true);
191
+ }
192
+ console.log('pid=%d msg=failed to kill - retrying in %dms', pid, kill_retry_time);
193
+ return false;
194
+ }, kill_retry_time);
195
+
196
+ timeout = setTimeout(function() {
197
+ clearInterval(timer);
198
+ if (sigkill) {
199
+ console.log('Process with pid %d could not be killed', pid);
200
+ return done({type : 'timeout', msg : 'timeout'});
201
+ }
202
+ else {
203
+ var alive = survivors();
204
+ console.log('Process with pid %d still alive after %sms, sending SIGKILL to %d remaining pids...', pid, kill_timeout, alive.length);
205
+
206
+ alive.forEach(function(p) {
207
+ try {
208
+ process.kill(parseInt(p), 'SIGKILL');
209
+ } catch(e) {
210
+ if (e.code !== 'ESRCH')
211
+ console.error('[SIGKILL] %s pid can not be killed', p, e.stack, e.message);
212
+ }
213
+ });
214
+ return God.processIsDead(pid, pm2_env, cb, true);
215
+ }
216
+ }, kill_timeout);
217
+ return false;
218
+ };
219
+
220
+ /**
221
+ * Description
222
+ * @method killProcess
223
+ * @param int pid
224
+ * @param Object pm2_env
225
+ * @param function cb
226
+ * @return CallExpression
227
+ */
228
+ God.killProcess = function(pid, pm2_env, cb) {
229
+ if (!pid) return cb({msg : 'no pid passed or null'});
230
+
231
+ // Only the treekill branch below may provide the pid list processIsDead
232
+ // waits on: any earlier one belongs to another kill, its pids may be recycled
233
+ delete pm2_env._tree_pids;
234
+
235
+ if (typeof(pm2_env.pm_id) === 'number' &&
236
+ (cst.KILL_USE_MESSAGE || pm2_env.shutdown_with_message == true)) {
237
+ var proc = God.clusters_db[pm2_env.pm_id];
238
+
239
+ if (proc && proc.send) {
240
+ try {
241
+ proc.send('shutdown');
242
+ } catch (e) {
243
+ console.error(`[AppKill] Cannot send "shutdown" message to ${pid}`)
244
+ console.error(e.stack, e.message)
245
+ }
246
+ return God.processIsDead(pid, pm2_env, cb);
247
+ }
248
+ else {
249
+ console.log(`[AppKill] ${pid} pid cannot be notified with send()`)
250
+ }
251
+ }
252
+
253
+ if (pm2_env.treekill !== true) {
254
+ try {
255
+ process.kill(parseInt(pid), cst.KILL_SIGNAL);
256
+ } catch(e) {
257
+ console.error('[SimpleKill] %s pid can not be killed', pid, e.stack, e.message);
258
+ }
259
+ return God.processIsDead(pid, pm2_env, cb);
260
+ }
261
+ else {
262
+ treekill(parseInt(pid), cst.KILL_SIGNAL, function(err, killedPids) {
263
+ if (Array.isArray(killedPids) && killedPids.length > 0)
264
+ pm2_env._tree_pids = killedPids;
265
+ return God.processIsDead(pid, pm2_env, cb);
266
+ });
267
+ }
268
+ };
269
+
270
+ /**
271
+ * Description
272
+ * @method getNewId
273
+ * @return UpdateExpression
274
+ */
275
+ God.getNewId = function() {
276
+ return God.next_id++;
277
+ };
278
+
279
+ /**
280
+ * When a process is restarted or reloaded reset fields
281
+ * to monitor unstable starts
282
+ * @method resetState
283
+ * @param {} pm2_env
284
+ * @return
285
+ */
286
+ God.resetState = function(pm2_env) {
287
+ pm2_env.created_at = Date.now();
288
+ pm2_env.unstable_restarts = 0;
289
+ pm2_env.prev_restart_delay = 0;
290
+ };
291
+
292
+ };