@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,300 @@
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 Reload functions related
10
+ * @author Alexandre Strzelewicz <as@unitech.io>
11
+ * @project PM2
12
+ */
13
+
14
+ var cst = require('../../constants.js');
15
+ var Utility = require('../Utility.js');
16
+
17
+ /**
18
+ * Put a parked worker back under its original id when the reload could not
19
+ * spawn its replacement — otherwise the _old_ slot would stay occupied and
20
+ * block any further reload of this process
21
+ * @method unparkOldWorker
22
+ */
23
+ function unparkOldWorker(God, id, t_key, old_worker) {
24
+ if (God.clusters_db[t_key] === old_worker && !(id in God.clusters_db)) {
25
+ old_worker.pm2_env.pm_id = id;
26
+ old_worker.pm_id = id;
27
+ God.clusters_db[id] = old_worker;
28
+ delete God.clusters_db[t_key];
29
+ }
30
+ }
31
+
32
+ /**
33
+ * softReload will wait permission from process to exit
34
+ * @method softReload
35
+ * @param {} God
36
+ * @param {} id
37
+ * @param {} cb
38
+ * @return Literal
39
+ */
40
+ function softReload(God, id, cb) {
41
+ var t_key = '_old_' + id;
42
+
43
+ // Move old worker to tmp id
44
+ God.clusters_db[t_key] = God.clusters_db[id];
45
+
46
+ delete God.clusters_db[id];
47
+
48
+ var old_worker = God.clusters_db[t_key];
49
+
50
+ // Deep copy
51
+ var new_env = Utility.clone(old_worker.pm2_env);
52
+
53
+ // Reset created_at and unstable_restarts
54
+ God.resetState(new_env);
55
+
56
+ new_env.restart_time += 1;
57
+
58
+ old_worker.pm2_env.pm_id = t_key;
59
+ old_worker.pm_id = t_key;
60
+
61
+ var timer = null;
62
+ var new_worker = null;
63
+ var started = false;
64
+
65
+ // Remove old worker properly; gated so that only the first of onListen /
66
+ // backstop timer starts the old worker shutdown sequence
67
+ var softCleanDeleteProcess = function () {
68
+ if (started) return false;
69
+ started = true;
70
+
71
+ var cleanUp = function () {
72
+ clearTimeout(timer);
73
+ console.log('-softReload- Old worker disconnected');
74
+ return God.deleteProcessId(t_key, cb);
75
+ };
76
+
77
+ old_worker.once('disconnect', cleanUp);
78
+
79
+ try {
80
+ if (old_worker.state != 'dead' && old_worker.state != 'disconnected')
81
+ old_worker.send && old_worker.send('shutdown');
82
+ else {
83
+ clearTimeout(timer);
84
+ console.error('Worker %d is already disconnected', old_worker.pm2_env.pm_id);
85
+ return God.deleteProcessId(t_key, cb);
86
+ }
87
+ } catch(e) {
88
+ clearTimeout(timer);
89
+ console.error('Worker %d is already disconnected', old_worker.pm2_env.pm_id);
90
+ return God.deleteProcessId(t_key, cb);
91
+ }
92
+
93
+ timer = setTimeout(function () {
94
+ old_worker.removeListener('disconnect', cleanUp);
95
+ return God.deleteProcessId(t_key, cb);
96
+ }, cst.GRACEFUL_TIMEOUT);
97
+ return false;
98
+ };
99
+
100
+ var onListen = function () {
101
+ // The backstop timer already concluded and `timer` now holds the graceful
102
+ // shutdown timer: a late 'listening' must not cancel it
103
+ if (started) return false;
104
+ clearTimeout(timer);
105
+ softCleanDeleteProcess();
106
+ console.log('-softReload- New worker listening');
107
+ };
108
+
109
+ // Backstop armed BEFORE spawning the replacement: even if executeApp's
110
+ // callback never fires (worker dying before its 'online' event), the parked
111
+ // worker is stopped and the _old_ slot released after listen_timeout (#6129)
112
+ timer = setTimeout(function() {
113
+ if (new_worker)
114
+ new_worker.removeListener('listening', onListen);
115
+ softCleanDeleteProcess();
116
+ }, new_env.listen_timeout || cst.GRACEFUL_LISTEN_TIMEOUT);
117
+
118
+ God.executeApp(new_env, function(err, worker) {
119
+ if (err) {
120
+ // `timer` may only be cleared while it still holds the listen backstop,
121
+ // i.e. before softCleanDeleteProcess reassigned it to the graceful timer
122
+ if (started) return false;
123
+ started = true;
124
+ clearTimeout(timer);
125
+ unparkOldWorker(God, id, t_key, old_worker);
126
+ return cb(err);
127
+ }
128
+ new_worker = worker;
129
+
130
+ // Bind to know when the new process is up
131
+ new_worker.once('listening', onListen);
132
+ return false;
133
+ });
134
+ return false;
135
+ };
136
+
137
+ /**
138
+ * hardReload will reload without waiting permission from process
139
+ * @method hardReload
140
+ * @param {} God
141
+ * @param {} id
142
+ * @param {} cb
143
+ * @return Literal
144
+ */
145
+ function hardReload(God, id, wait_msg, cb) {
146
+ var t_key = '_old_' + id;
147
+
148
+ // Move old worker to tmp id
149
+ God.clusters_db[t_key] = God.clusters_db[id];
150
+ delete God.clusters_db[id];
151
+
152
+ var old_worker = God.clusters_db[t_key];
153
+ // Deep copy
154
+ var new_env = Utility.clone(old_worker.pm2_env);
155
+ new_env.restart_time += 1;
156
+
157
+ // Reset created_at and unstable_restarts
158
+ God.resetState(new_env);
159
+
160
+ old_worker.pm2_env.pm_id = t_key;
161
+ old_worker.pm_id = t_key;
162
+ var timer = null;
163
+ var new_worker = null;
164
+ var finished = false;
165
+
166
+ // Single gate concluding the reload: only the first of onListen / backstop
167
+ // timer / spawn-error path runs the cleanup and fires cb
168
+ var finish = function () {
169
+ if (finished) return false;
170
+ finished = true;
171
+ God.bus.removeListener('process:msg', listener);
172
+ return God.deleteProcessId(t_key, cb);
173
+ };
174
+
175
+ var onListen = function () {
176
+ clearTimeout(timer);
177
+ console.log('-reload- New worker listening');
178
+ return finish();
179
+ };
180
+
181
+ var listener = function (packet) {
182
+ if (packet.raw === 'ready' &&
183
+ packet.process.name === old_worker.pm2_env.name &&
184
+ packet.process.pm_id === id) {
185
+ return onListen();
186
+ }
187
+ };
188
+
189
+ if (wait_msg !== 'listening') {
190
+ God.bus.on('process:msg', listener);
191
+ }
192
+
193
+ // Backstop armed BEFORE spawning the replacement: even if executeApp's
194
+ // callback never fires (worker dying before its 'online' event), the parked
195
+ // worker is stopped and the _old_ slot released after listen_timeout (#6129)
196
+ timer = setTimeout(function() {
197
+ if (new_worker && wait_msg === 'listening')
198
+ new_worker.removeListener(wait_msg, onListen);
199
+ return finish();
200
+ }, new_env.listen_timeout || cst.GRACEFUL_LISTEN_TIMEOUT);
201
+
202
+ God.executeApp(new_env, function(err, worker) {
203
+ if (err) {
204
+ clearTimeout(timer);
205
+ God.bus.removeListener('process:msg', listener);
206
+ if (finished) return false;
207
+ finished = true;
208
+ unparkOldWorker(God, id, t_key, old_worker);
209
+ return cb(err);
210
+ }
211
+ new_worker = worker;
212
+
213
+ // Bind to know when the new process is up
214
+ if (wait_msg === 'listening') {
215
+ new_worker.once('listening', onListen);
216
+ }
217
+ return false;
218
+ });
219
+ return false;
220
+ };
221
+
222
+ /**
223
+ * Description
224
+ * @method exports
225
+ * @param {} God
226
+ * @return
227
+ */
228
+ module.exports = function(God) {
229
+
230
+ /**
231
+ * Reload
232
+ * @method softReloadProcessId
233
+ * @param {} id
234
+ * @param {} cb
235
+ * @return CallExpression
236
+ */
237
+ God.softReloadProcessId = function(opts, cb) {
238
+ var id = opts.id;
239
+ var env = opts.env || {};
240
+
241
+ if (!(id in God.clusters_db))
242
+ return cb(new Error(`pm_id ${id} not available in ${id}`));
243
+
244
+ // The previous reload of this process is not finished yet (its outgoing
245
+ // worker is still parked under the _old_ key): refuse instead of
246
+ // overwriting the slot and orphaning that worker (#6129)
247
+ if (('_old_' + id) in God.clusters_db)
248
+ return cb(new Error(`Reload already in progress for process id ${id}, retry later`));
249
+
250
+ if (God.clusters_db[id].pm2_env.status == cst.ONLINE_STATUS &&
251
+ God.clusters_db[id].pm2_env.exec_mode == 'cluster_mode' &&
252
+ !God.clusters_db[id].pm2_env.wait_ready) {
253
+
254
+ Utility.extend(God.clusters_db[id].pm2_env.env, opts.env);
255
+ Utility.extendExtraConfig(God.clusters_db[id], opts);
256
+
257
+ return softReload(God, id, cb);
258
+ }
259
+ else {
260
+ console.log('Process %s in a stopped status, starting it', id);
261
+ return God.restartProcessId(opts, cb);
262
+ }
263
+ };
264
+
265
+ /**
266
+ * Reload
267
+ * @method reloadProcessId
268
+ * @param {} id
269
+ * @param {} cb
270
+ * @return CallExpression
271
+ */
272
+ God.reloadProcessId = function(opts, cb) {
273
+ var id = opts.id;
274
+ var env = opts.env || {};
275
+
276
+ if (!(id in God.clusters_db))
277
+ return cb(new Error('PM2 ID unknown'));
278
+
279
+ // The previous reload of this process is not finished yet (its outgoing
280
+ // worker is still parked under the _old_ key): refuse instead of
281
+ // overwriting the slot and orphaning that worker (#6129)
282
+ if (('_old_' + id) in God.clusters_db)
283
+ return cb(new Error(`Reload already in progress for process id ${id}, retry later`));
284
+
285
+ if (God.clusters_db[id].pm2_env.status == cst.ONLINE_STATUS &&
286
+ God.clusters_db[id].pm2_env.exec_mode == 'cluster_mode') {
287
+
288
+ Utility.extend(God.clusters_db[id].pm2_env.env, opts.env);
289
+ Utility.extendExtraConfig(God.clusters_db[id], opts);
290
+
291
+ var wait_msg = God.clusters_db[id].pm2_env.wait_ready ? 'ready' : 'listening';
292
+ return hardReload(God, id, wait_msg, cb);
293
+ }
294
+ else {
295
+ console.log('Process %s in a stopped status, starting it', id);
296
+ return God.restartProcessId(opts, cb);
297
+ }
298
+ };
299
+
300
+ };
@@ -0,0 +1,239 @@
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 Daemon-side stdout/stderr -> log files + bus, shared by fork and
10
+ * cluster mode. In cluster mode the worker already intercepts
11
+ * process.stdout.write in ProcessContainer.js, so what reaches the pipe
12
+ * here is only what bypassed it: direct fd writes (pino/sonic-boom),
13
+ * children spawned with stdio 'inherit' (npm start), native addons...
14
+ */
15
+ var fs = require('fs');
16
+ var dayjs = require('dayjs');
17
+ var Utility = require('../Utility.js');
18
+
19
+ /**
20
+ * Bind `data` handlers on proc.stdout / proc.stderr
21
+ * @param {Object} God
22
+ * @param {Object} proc ChildProcess-like object exposing stdout/stderr streams
23
+ * @param {Object} pm2_env
24
+ * @param {Object} stds { out, err, std? } write streams (or raw paths when NULL)
25
+ * @return {Object} { flush() } flush the pending partial line buffer
26
+ */
27
+ function attach(God, proc, pm2_env, stds) {
28
+ function transformLogToJson(type, data) {
29
+ return JSON.stringify({
30
+ message : data.toString(),
31
+ timestamp : pm2_env.log_date_format ? dayjs().format(pm2_env.log_date_format) : new Date().toISOString(),
32
+ type : type,
33
+ process_id : pm2_env.pm_id,
34
+ app_name : pm2_env.name
35
+ }) + '\n';
36
+ }
37
+
38
+ // Pipe chunks are not line-aligned: keep the trailing partial line
39
+ // per stream until the next chunk (or exit) completes it (#6125)
40
+ var MAX_LINE_BUFFER = 1024 * 1024;
41
+ var line_buf = { out: '', err: '' };
42
+
43
+ function prefixLogWithDate(data, type) {
44
+ var text = line_buf[type] + data.toString();
45
+ var idx = text.lastIndexOf('\n');
46
+ if (idx === -1) {
47
+ if (text.length < MAX_LINE_BUFFER) {
48
+ line_buf[type] = text;
49
+ return '';
50
+ }
51
+ // no newline in sight: force flush to bound memory
52
+ line_buf[type] = '';
53
+ return `${dayjs().format(pm2_env.log_date_format)}: ${text}\n`;
54
+ }
55
+ line_buf[type] = text.slice(idx + 1);
56
+ var ts = dayjs().format(pm2_env.log_date_format);
57
+ return text.slice(0, idx).split('\n').map(line => `${ts}: ${line}\n`).join('');
58
+ }
59
+
60
+ function flushLineBuffer(type) {
61
+ if (!line_buf[type]) return;
62
+ var log_data = `${dayjs().format(pm2_env.log_date_format)}: ${line_buf[type]}\n`;
63
+ line_buf[type] = '';
64
+ var std = type === 'err' ? stds.err : stds.out;
65
+ stds.std && stds.std.write && stds.std.write(log_data);
66
+ std && std.write && std.write(log_data);
67
+ }
68
+
69
+ function onData(type) {
70
+ var log_path = type === 'err' ? pm2_env.pm_err_log_path : pm2_env.pm_out_log_path;
71
+
72
+ return function(data) {
73
+ var log_data = null;
74
+
75
+ // via --out /dev/null --err /dev/null
76
+ if (pm2_env.disable_logs === true) return false;
77
+
78
+ if (pm2_env.log_type && pm2_env.log_type === 'json')
79
+ log_data = transformLogToJson(type, data);
80
+ else if (pm2_env.log_date_format)
81
+ log_data = prefixLogWithDate(data, type);
82
+ else
83
+ log_data = data.toString();
84
+
85
+ if (log_data === '') return false;
86
+
87
+ God.bus.emit('log:' + type, {
88
+ process : {
89
+ pm_id : pm2_env.pm_id,
90
+ name : pm2_env.name,
91
+ rev : (pm2_env.versioning && pm2_env.versioning.revision) ? pm2_env.versioning.revision : null,
92
+ namespace : pm2_env.namespace
93
+ },
94
+ at : Utility.getDate(),
95
+ data : log_data
96
+ });
97
+
98
+ if (Utility.checkPathIsNull(log_path) &&
99
+ (!pm2_env.pm_log_path || Utility.checkPathIsNull(pm2_env.pm_log_path)))
100
+ return false;
101
+
102
+ stds.std && stds.std.write && stds.std.write(log_data);
103
+ stds[type] && stds[type].write && stds[type].write(log_data);
104
+ };
105
+ }
106
+
107
+ proc.stderr && proc.stderr.on('data', onData('err'));
108
+ proc.stdout && proc.stdout.on('data', onData('out'));
109
+
110
+ return {
111
+ flush: function() {
112
+ if (pm2_env.log_date_format && !(pm2_env.log_type === 'json')) {
113
+ flushLineBuffer('out');
114
+ flushLineBuffer('err');
115
+ }
116
+ }
117
+ };
118
+ }
119
+
120
+ /**
121
+ * Call cb once the child's stdout/stderr pipes are closed: all their data
122
+ * has been read, the log streams can be closed.
123
+ * Not the ChildProcess 'close': it also waits for the IPC channel, whose
124
+ * close is sometimes never emitted after the disconnect() done at exit
125
+ * (Linux) -> the log streams were never closed, 2 fds leaked per app stop
126
+ * @param {Object} proc ChildProcess exposing stdout/stderr
127
+ * @param {Function} cb
128
+ */
129
+ function onPipesClosed(proc, cb) {
130
+ var pipes = [proc.stdout, proc.stderr].filter(Boolean);
131
+ var pending = pipes.length;
132
+
133
+ function closed() {
134
+ if (--pending === 0) cb();
135
+ }
136
+
137
+ if (pending === 0) return cb();
138
+ pipes.forEach(function(pipe) {
139
+ if (pipe.destroyed) return closed();
140
+ pipe.once('close', closed);
141
+ });
142
+ }
143
+
144
+ /**
145
+ * Prefer end() so buffered data is flushed before the fd is closed
146
+ */
147
+ function close(stds) {
148
+ for (var k in stds) {
149
+ if (stds[k] && stds[k].end) stds[k].end();
150
+ else if (stds[k] && stds[k].destroy) stds[k].destroy();
151
+ else if (stds[k] && stds[k].close) stds[k].close();
152
+ // NULL out/err never get a stream: keep the raw path (#5509)
153
+ if (stds[k] && stds[k]._file) stds[k] = stds[k]._file;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Build the { out, err, std? } path map from pm2_env
159
+ */
160
+ function stdsFromEnv(pm2_env) {
161
+ var stds = {
162
+ out: pm2_env.pm_out_log_path,
163
+ err: pm2_env.pm_err_log_path
164
+ };
165
+ if ('pm_log_path' in pm2_env)
166
+ stds.std = pm2_env.pm_log_path;
167
+ return stds;
168
+ }
169
+
170
+ /**
171
+ * Resolve a uid/gid (number or name) like process.setuid/setgid do
172
+ * @return {Number} id, -1 when unset/unknown (= leave unchanged for chown)
173
+ */
174
+ function resolveId(id, kind) {
175
+ if (id === undefined || id === null || id === '') return -1;
176
+ if (!isNaN(parseInt(id, 10)) && String(parseInt(id, 10)) === String(id).trim())
177
+ return parseInt(id, 10);
178
+ try {
179
+ var passwd = require('../tools/passwd.js');
180
+ var db = kind === 'group' ? passwd.getGroups() : passwd.getUsers();
181
+ var entry = db && db[id];
182
+ if (entry) return parseInt(kind === 'group' ? entry.id : entry.userId, 10);
183
+ } catch(e) {}
184
+ return -1;
185
+ }
186
+
187
+ /**
188
+ * Owner the daemon must give to the log files it creates, null when
189
+ * nothing to do (daemon not root, no uid/gid, Windows)
190
+ */
191
+ function logOwner(pm2_env) {
192
+ if (!process.getuid || process.getuid() !== 0) return null;
193
+ if (!pm2_env) return null;
194
+ // resolveId and not a falsy check: uid/gid 0 is a valid target
195
+ var owner = { uid: resolveId(pm2_env.uid, 'user'), gid: resolveId(pm2_env.gid, 'group') };
196
+ if (owner.uid === -1 && owner.gid === -1) return null;
197
+ return owner;
198
+ }
199
+
200
+ /**
201
+ * Utility.startLogging, then hand the files it created to the app uid/gid:
202
+ * a cluster worker drops privileges (ProcessContainer.js) and reopens the
203
+ * same paths itself, root:root 0644 files made it crash loop on EACCES.
204
+ * Only files created here are chowned, existing ones are left untouched
205
+ */
206
+ function open(stds, pm2_env, cb) {
207
+ var owner = logOwner(pm2_env);
208
+ var created = [];
209
+
210
+ if (owner) {
211
+ for (var k in stds) {
212
+ var file = stds[k];
213
+ // same skip rule as Utility.startLogging
214
+ if (typeof file !== 'string' || file.indexOf('NULL') > -1 ||
215
+ file.indexOf('/dev/null') > -1) continue;
216
+ if (!fs.existsSync(file) && created.indexOf(file) === -1) created.push(file);
217
+ }
218
+ }
219
+
220
+ Utility.startLogging(stds, function(err) {
221
+ created.forEach(function(file) {
222
+ try {
223
+ fs.chownSync(file, owner.uid, owner.gid);
224
+ } catch(e) {
225
+ console.error('Cannot chown %s to %s:%s (%s)', file, owner.uid, owner.gid, e.message);
226
+ }
227
+ });
228
+ return cb(err);
229
+ });
230
+ }
231
+
232
+ module.exports = {
233
+ attach : attach,
234
+ close : close,
235
+ open : open,
236
+ onPipesClosed : onPipesClosed,
237
+ logOwner : logOwner,
238
+ stdsFromEnv : stdsFromEnv
239
+ };