@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
package/lib/Client.js ADDED
@@ -0,0 +1,793 @@
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
+
7
+ var debug = require('debug')('pm2:client');
8
+ var Common = require('./Common.js');
9
+ var KMDaemon = require('../modules/pm2-io-agent/src/InteractorClient');
10
+ var rpc = require('../modules/pm2-axon-rpc');
11
+ var forEach = require('async/forEach');
12
+ var axon = require('../modules/pm2-axon');
13
+ var util = require('util');
14
+ var fs = require('fs');
15
+ var path = require('path');
16
+ var pkg = require('../package.json');
17
+ var which = require('./tools/which.js');
18
+
19
+ function noop() {}
20
+
21
+ var Client = module.exports = function(opts) {
22
+ if (!opts) opts = {};
23
+
24
+ if (!opts.conf)
25
+ this.conf = require('../constants.js');
26
+ else {
27
+ this.conf = opts.conf;
28
+ }
29
+
30
+ this.daemon_mode = typeof(opts.daemon_mode) === 'undefined' ? true : opts.daemon_mode;
31
+ this.pm2_home = this.conf.PM2_ROOT_PATH;
32
+ this.secret_key = opts.secret_key;
33
+ this.public_key = opts.public_key;
34
+ this.machine_name = opts.machine_name;
35
+
36
+ // Create all folders and files needed
37
+ // Client depends to that to interact with PM2 properly
38
+ this.initFileStructure(this.conf);
39
+
40
+ debug('Using RPC file %s', this.conf.DAEMON_RPC_PORT);
41
+ debug('Using PUB file %s', this.conf.DAEMON_PUB_PORT);
42
+ this.rpc_socket_file = this.conf.DAEMON_RPC_PORT;
43
+ this.pub_socket_file = this.conf.DAEMON_PUB_PORT;
44
+ };
45
+
46
+ // @breaking change (noDaemonMode has been drop)
47
+ // @todo ret err
48
+ Client.prototype.start = function(cb) {
49
+ var that = this;
50
+
51
+ this.pingDaemon(function(daemonAlive) {
52
+ if (daemonAlive === true)
53
+ return that.launchRPC(function(err, meta) {
54
+ return cb(null, {
55
+ daemon_mode : that.conf.daemon_mode,
56
+ new_pm2_instance : false,
57
+ rpc_socket_file : that.rpc_socket_file,
58
+ pub_socket_file : that.pub_socket_file,
59
+ pm2_home : that.pm2_home
60
+ });
61
+ });
62
+
63
+ /**
64
+ * No Daemon mode
65
+ */
66
+ if (that.daemon_mode === false) {
67
+ var Daemon = require('./Daemon.js');
68
+
69
+ var daemon = new Daemon({
70
+ pub_socket_file : that.conf.DAEMON_PUB_PORT,
71
+ rpc_socket_file : that.conf.DAEMON_RPC_PORT,
72
+ pid_file : that.conf.PM2_PID_FILE_PATH,
73
+ ignore_signals : true
74
+ });
75
+
76
+ console.log('Launching in no daemon mode');
77
+
78
+ daemon.innerStart(function() {
79
+ KMDaemon.launchAndInteract(that.conf, {
80
+ machine_name : that.machine_name,
81
+ public_key : that.public_key,
82
+ secret_key : that.secret_key,
83
+ pm2_version : pkg.version
84
+ }, function(err, data, interactor_proc) {
85
+ that.interactor_process = interactor_proc;
86
+ });
87
+
88
+ that.launchRPC(function(err, meta) {
89
+ return cb(null, {
90
+ daemon_mode : that.conf.daemon_mode,
91
+ new_pm2_instance : true,
92
+ rpc_socket_file : that.rpc_socket_file,
93
+ pub_socket_file : that.pub_socket_file,
94
+ pm2_home : that.pm2_home
95
+ });
96
+ });
97
+ });
98
+ return false;
99
+ }
100
+
101
+ /**
102
+ * Daemon mode
103
+ */
104
+ that.launchDaemon(function(err, child) {
105
+ if (err) {
106
+ Common.printError(err);
107
+ return cb ? cb(err) : process.exit(that.conf.ERROR_EXIT);
108
+ }
109
+
110
+ if (!process.env.PM2_DISCRETE_MODE)
111
+ Common.printOut(that.conf.PREFIX_MSG + 'PM2 Successfully daemonized');
112
+
113
+ that.launchRPC(function(err, meta) {
114
+ return cb(null, {
115
+ daemon_mode : that.conf.daemon_mode,
116
+ new_pm2_instance : true,
117
+ rpc_socket_file : that.rpc_socket_file,
118
+ pub_socket_file : that.pub_socket_file,
119
+ pm2_home : that.pm2_home
120
+ });
121
+ });
122
+ });
123
+ });
124
+ };
125
+
126
+ // Init file structure of pm2_home
127
+ // This includes
128
+ // - pm2 pid and log path
129
+ // - rpc and pub socket for command execution
130
+ Client.prototype.initFileStructure = function (opts) {
131
+ if (!fs.existsSync(opts.DEFAULT_LOG_PATH)) {
132
+ try {
133
+ fs.mkdirSync(opts.DEFAULT_LOG_PATH, { recursive: true });
134
+ } catch (e) {
135
+ console.error(e.stack || e);
136
+ }
137
+ }
138
+
139
+ if (!fs.existsSync(opts.DEFAULT_PID_PATH)) {
140
+ try {
141
+ fs.mkdirSync(opts.DEFAULT_PID_PATH, { recursive: true });
142
+ } catch (e) {
143
+ console.error(e.stack || e);
144
+ }
145
+ }
146
+
147
+ if (!fs.existsSync(opts.PM2_MODULE_CONF_FILE)) {
148
+ try {
149
+ fs.writeFileSync(opts.PM2_MODULE_CONF_FILE, "{}");
150
+ } catch (e) {
151
+ console.error(e.stack || e);
152
+ }
153
+ }
154
+
155
+ if (!fs.existsSync(opts.DEFAULT_MODULE_PATH)) {
156
+ try {
157
+ fs.mkdirSync(opts.DEFAULT_MODULE_PATH, { recursive: true });
158
+ } catch (e) {
159
+ console.error(e.stack || e);
160
+ }
161
+ }
162
+
163
+ if (process.env.PM2_DISCRETE_MODE) {
164
+ try {
165
+ fs.writeFileSync(path.join(opts.PM2_HOME, 'touch'), Date.now().toString());
166
+ } catch(e) {
167
+ debug(e.stack || e);
168
+ }
169
+ }
170
+
171
+ if (!process.env.PM2_PROGRAMMATIC && !fs.existsSync(path.join(opts.PM2_HOME, 'touch'))) {
172
+
173
+ var vCheck = require('./VersionCheck.js')
174
+
175
+ vCheck({
176
+ state: 'install',
177
+ version: pkg.version
178
+ })
179
+
180
+ var dt = fs.readFileSync(path.join(__dirname, opts.PM2_BANNER));
181
+ console.log(dt.toString());
182
+ try {
183
+ fs.writeFileSync(path.join(opts.PM2_HOME, 'touch'), Date.now().toString());
184
+ } catch(e) {
185
+ debug(e.stack || e);
186
+ }
187
+ }
188
+ };
189
+
190
+ Client.prototype.close = function(cb) {
191
+ var that = this;
192
+
193
+ forEach([
194
+ that.disconnectRPC.bind(that),
195
+ that.disconnectBus.bind(that)
196
+ ], function(fn, next) {
197
+ fn(next)
198
+ }, cb);
199
+ };
200
+
201
+ /**
202
+ * Launch the Daemon by forking this same file
203
+ * The method Client.remoteWrapper will be called
204
+ *
205
+ * @method launchDaemon
206
+ * @param {Object} opts
207
+ * @param {Object} [opts.interactor=true] allow to disable interaction on launch
208
+ */
209
+ Client.prototype.launchDaemon = function(opts, cb) {
210
+ if (typeof(opts) == 'function') {
211
+ cb = opts;
212
+ opts = {
213
+ interactor : true
214
+ };
215
+ }
216
+
217
+ var that = this
218
+ var ClientJS = path.resolve(path.dirname(module.filename), 'Daemon.js');
219
+ var node_args = [];
220
+ var out, err;
221
+
222
+ // if (process.env.TRAVIS) {
223
+ // // Redirect PM2 internal err and out to STDERR STDOUT when running with Travis
224
+ // out = 1;
225
+ // err = 2;
226
+ // }
227
+ // else {
228
+ out = fs.openSync(that.conf.PM2_LOG_FILE_PATH, 'a'),
229
+ err = fs.openSync(that.conf.PM2_LOG_FILE_PATH, 'a');
230
+ //}
231
+
232
+ if (this.conf.LOW_MEMORY_ENVIRONMENT) {
233
+ var os = require('os');
234
+ node_args.push('--gc-global'); // Does full GC (smaller memory footprint)
235
+ node_args.push('--max-old-space-size=' + Math.floor(os.totalmem() / 1024 / 1024));
236
+ }
237
+
238
+ // Node.js tuning for better performance
239
+ //node_args.push('--expose-gc'); // Allows manual GC in the code
240
+
241
+ /**
242
+ * Add node [arguments] depending on PM2_NODE_OPTIONS env variable
243
+ */
244
+ if (process.env.PM2_NODE_OPTIONS)
245
+ node_args = node_args.concat(process.env.PM2_NODE_OPTIONS.split(' '));
246
+ node_args.push(ClientJS);
247
+
248
+ if (!process.env.PM2_DISCRETE_MODE)
249
+ Common.printOut(that.conf.PREFIX_MSG + 'Spawning PM2 daemon with pm2_home=' + this.pm2_home);
250
+
251
+ var interpreter = process.execPath;
252
+
253
+ var child = require('child_process').spawn(interpreter, node_args, {
254
+ detached : true,
255
+ cwd : that.conf.cwd || process.cwd(),
256
+ windowsHide: true,
257
+ env : Object.assign({
258
+ 'SILENT' : that.conf.DEBUG ? !that.conf.DEBUG : true,
259
+ 'PM2_HOME' : that.pm2_home
260
+ }, process.env),
261
+ stdio : [null, out, err, 'ipc']
262
+ });
263
+
264
+ function onError(e) {
265
+ console.error(e.message || e);
266
+ return cb ? cb(e.message || e) : false;
267
+ }
268
+
269
+ child.once('error', onError);
270
+
271
+ if (this.conf.IS_BUN === false)
272
+ child.unref();
273
+
274
+ child.once('message', function(msg) {
275
+ debug('PM2 daemon launched with return message: ', msg);
276
+ child.removeListener('error', onError);
277
+ child.disconnect();
278
+ if (opts && opts.interactor == false)
279
+ return cb(null, child);
280
+
281
+ if (process.env.PM2_NO_INTERACTION == 'true')
282
+ return cb(null, child);
283
+
284
+ /**
285
+ * Here the Keymetrics agent is launched automaticcaly if
286
+ * it has been already configured before (via pm2 link)
287
+ */
288
+ KMDaemon.launchAndInteract(that.conf, {
289
+ machine_name : that.machine_name,
290
+ public_key : that.public_key,
291
+ secret_key : that.secret_key,
292
+ pm2_version : pkg.version
293
+ }, function(err, data, interactor_proc) {
294
+ that.interactor_process = interactor_proc;
295
+ return cb(null, child);
296
+ });
297
+ });
298
+ };
299
+
300
+ /**
301
+ * Ping the daemon to know if it alive or not
302
+ * @api public
303
+ * @method pingDaemon
304
+ * @param {} cb
305
+ * @return
306
+ */
307
+ Client.prototype.pingDaemon = function pingDaemon(cb) {
308
+ var req = axon.socket('req');
309
+ var client = new rpc.Client(req);
310
+ var that = this;
311
+
312
+ debug('[PING PM2] Trying to connect to server');
313
+
314
+ client.sock.once('reconnect attempt', function() {
315
+ client.sock.close();
316
+ debug('Daemon not launched');
317
+ process.nextTick(function() {
318
+ return cb(false);
319
+ });
320
+ });
321
+
322
+ client.sock.once('error', function(e) {
323
+ if (e.code === 'EACCES') {
324
+ fs.stat(that.conf.DAEMON_RPC_PORT, function(e, stats) {
325
+ if (stats.uid === 0) {
326
+ console.error(that.conf.PREFIX_MSG_ERR + 'Permission denied, to give access to current user:');
327
+ console.log('$ sudo chown ' + process.env.USER + ':' + process.env.USER + ' ' + that.conf.DAEMON_RPC_PORT + ' ' + that.conf.DAEMON_PUB_PORT);
328
+ }
329
+ else
330
+ console.error(that.conf.PREFIX_MSG_ERR + 'Permission denied, check permissions on ' + that.conf.DAEMON_RPC_PORT);
331
+
332
+ process.exit(1);
333
+ });
334
+ }
335
+ else
336
+ console.error(e.message || e);
337
+ });
338
+
339
+ client.sock.once('connect', function() {
340
+ client.sock.once('close', function() {
341
+ return cb(true);
342
+ });
343
+ client.sock.close();
344
+ debug('Daemon alive');
345
+ });
346
+
347
+ req.connect(this.rpc_socket_file);
348
+ };
349
+
350
+ /**
351
+ * Methods to interact with the Daemon via RPC
352
+ * This method wait to be connected to the Daemon
353
+ * Once he's connected it trigger the command parsing (on ./bin/pm2 file, at the end)
354
+ * @method launchRPC
355
+ * @params {function} [cb]
356
+ * @return
357
+ */
358
+ Client.prototype.launchRPC = function launchRPC(cb) {
359
+ debug('Launching RPC client on socket file %s', this.rpc_socket_file);
360
+ var req = axon.socket('req');
361
+ this.client = new rpc.Client(req);
362
+
363
+ // Use `req` rather than `this.client.sock`: `this.client` can be replaced
364
+ // or cleared by a previous connection that finishes closing (#6002)
365
+ var connectHandler = function() {
366
+ req.removeListener('error', errorHandler);
367
+ debug('RPC Connected to Daemon');
368
+ if (cb) {
369
+ setTimeout(function() {
370
+ cb(null);
371
+ }, 4);
372
+ }
373
+ };
374
+
375
+ var errorHandler = function(e) {
376
+ req.removeListener('connect', connectHandler);
377
+ if (cb) {
378
+ return cb(e);
379
+ }
380
+ };
381
+
382
+ this.client.sock.once('connect', connectHandler);
383
+ this.client.sock.once('error', errorHandler);
384
+ this.client_sock = req.connect(this.rpc_socket_file);
385
+ };
386
+
387
+ /**
388
+ * Methods to close the RPC connection
389
+ * @callback cb
390
+ */
391
+ Client.prototype.disconnectRPC = function disconnectRPC(cb) {
392
+ var that = this;
393
+ if (!cb) cb = noop;
394
+
395
+ if (!this.client_sock || !this.client_sock.close) {
396
+ this.client = null;
397
+ return process.nextTick(function() {
398
+ cb(new Error('SUB connection to PM2 is not launched'));
399
+ });
400
+ }
401
+
402
+ if (this.client_sock.connected === false ||
403
+ this.client_sock.closing === true) {
404
+ this.client = null;
405
+ return process.nextTick(function() {
406
+ cb(new Error('RPC already being closed'));
407
+ });
408
+ }
409
+
410
+ try {
411
+ var timer;
412
+ var done = false;
413
+ // launchRPC() may open a new connection before this one has closed,
414
+ // so only clean up the client and socket being closed here (#6002)
415
+ var client = that.client;
416
+ var client_sock = that.client_sock;
417
+
418
+ client_sock.once('close', function() {
419
+ clearTimeout(timer);
420
+ if (that.client === client)
421
+ that.client = null;
422
+ debug('PM2 RPC cleanly closed');
423
+ // a close after the timeout (Bun) was already reported
424
+ if (done) return;
425
+ done = true;
426
+ return cb(null, { msg : 'RPC Successfully closed' });
427
+ });
428
+
429
+ timer = setTimeout(function() {
430
+ done = true;
431
+ if (client_sock.destroy)
432
+ client_sock.destroy();
433
+ if (that.client === client)
434
+ that.client = null;
435
+ return cb(null, { msg : 'RPC Successfully closed via timeout' });
436
+ }, 200);
437
+
438
+ client_sock.close();
439
+ } catch(e) {
440
+ debug('Error while disconnecting RPC PM2', e.stack || e);
441
+ return cb(e);
442
+ }
443
+ return false;
444
+ };
445
+
446
+ Client.prototype.launchBus = function launchEventSystem(cb) {
447
+ var self = this;
448
+ this.sub = axon.socket('sub-emitter');
449
+ this.sub_sock = this.sub.connect(this.pub_socket_file);
450
+
451
+ this.sub_sock.once('connect', function() {
452
+ return cb(null, self.sub, self.sub_sock);
453
+ });
454
+ };
455
+
456
+ Client.prototype.disconnectBus = function disconnectBus(cb) {
457
+ if (!cb) cb = noop;
458
+
459
+ var that = this;
460
+
461
+ if (!this.sub_sock || !this.sub_sock.close) {
462
+ that.sub = null;
463
+ return process.nextTick(function() {
464
+ cb(null, { msg : 'bus was not connected'});
465
+ });
466
+ }
467
+
468
+ if (this.sub_sock.connected === false ||
469
+ this.sub_sock.closing === true) {
470
+ that.sub = null;
471
+ return process.nextTick(function() {
472
+ cb(new Error('SUB connection is already being closed'));
473
+ });
474
+ }
475
+
476
+ try {
477
+ var timer;
478
+ var done = false;
479
+
480
+ that.sub_sock.once('close', function() {
481
+ that.sub = null;
482
+ clearTimeout(timer);
483
+ debug('PM2 PUB cleanly closed');
484
+ // a close after the timeout (Bun) was already reported
485
+ if (done) return;
486
+ done = true;
487
+ return cb();
488
+ });
489
+
490
+ timer = setTimeout(function() {
491
+ done = true;
492
+ if (that.sub_sock.destroy)
493
+ that.sub_sock.destroy();
494
+ return cb();
495
+ }, 200);
496
+
497
+ this.sub_sock.close();
498
+ } catch(e) {
499
+ return cb(e);
500
+ }
501
+ };
502
+
503
+ /**
504
+ * Description
505
+ * @method gestExposedMethods
506
+ * @param {} cb
507
+ * @return
508
+ */
509
+ Client.prototype.getExposedMethods = function getExposedMethods(cb) {
510
+ this.client.methods(cb);
511
+ };
512
+
513
+ /**
514
+ * Description
515
+ * @method executeRemote
516
+ * @param {} method
517
+ * @param {} env
518
+ * @param {} fn
519
+ * @return
520
+ */
521
+ Client.prototype.executeRemote = function executeRemote(method, app_conf, fn) {
522
+ var self = this;
523
+
524
+ // stop watch on stop | env is the process id
525
+ if (method.indexOf('stop') !== -1) {
526
+ this.stopWatch(method, app_conf);
527
+ }
528
+ // stop watching when process is deleted
529
+ else if (method.indexOf('delete') !== -1) {
530
+ this.stopWatch(method, app_conf);
531
+ }
532
+ // stop everything on kill
533
+ else if (method.indexOf('kill') !== -1) {
534
+ this.stopWatch('deleteAll', app_conf);
535
+ }
536
+ else if (method.indexOf('restartProcessId') !== -1 && process.argv.indexOf('--watch') > -1) {
537
+ delete app_conf.env.current_conf.watch;
538
+ this.toggleWatch(method, app_conf);
539
+ }
540
+
541
+ if (!this.client || !this.client.call) {
542
+ this.start(function(error) {
543
+ if (error) {
544
+ if (fn)
545
+ return fn(error);
546
+ console.error(error);
547
+ return process.exit(0);
548
+ }
549
+ if (self.client) {
550
+ return self.client.call(method, app_conf, fn);
551
+ }
552
+ });
553
+ return false;
554
+ }
555
+
556
+ debug('Calling daemon method pm2:%s on rpc socket:%s', method, this.rpc_socket_file);
557
+ return this.client.call(method, app_conf, fn);
558
+ };
559
+
560
+ Client.prototype.notifyGod = function(action_name, id, cb) {
561
+ this.executeRemote('notifyByProcessId', {
562
+ id : id,
563
+ action_name : action_name,
564
+ manually : true
565
+ }, function() {
566
+ debug('God notified');
567
+ return cb ? cb() : false;
568
+ });
569
+ };
570
+
571
+ Client.prototype.killDaemon = function killDaemon(fn) {
572
+ var timeout;
573
+ var that = this;
574
+
575
+ function quit() {
576
+ that.close(function() {
577
+ return fn ? fn(null, {success:true}) : false;
578
+ });
579
+ }
580
+
581
+ // under unix, we listen for signal (that is send by daemon to notify us that its shuting down)
582
+ if (process.platform !== 'win32') {
583
+ process.once('SIGQUIT', function() {
584
+ debug('Received SIGQUIT from pm2 daemon');
585
+ clearTimeout(timeout);
586
+ quit();
587
+ });
588
+ }
589
+ else {
590
+ // if under windows, try to ping the daemon to see if it still here
591
+ setTimeout(function() {
592
+ that.pingDaemon(function(alive) {
593
+ if (!alive) {
594
+ clearTimeout(timeout);
595
+ return quit();
596
+ }
597
+ });
598
+ }, 250)
599
+ }
600
+
601
+ timeout = setTimeout(function() {
602
+ quit();
603
+ }, 3000);
604
+
605
+ // Kill daemon
606
+ this.executeRemote('killMe', {pid : process.pid});
607
+ };
608
+
609
+
610
+ /**
611
+ * Description
612
+ * @method toggleWatch
613
+ * @param {String} pm2 method name
614
+ * @param {Object} application environment, should include id
615
+ * @param {Function} callback
616
+ */
617
+ Client.prototype.toggleWatch = function toggleWatch(method, env, fn) {
618
+ debug('Calling toggleWatch');
619
+ this.client.call('toggleWatch', method, env, function() {
620
+ return fn ? fn() : false;
621
+ });
622
+ };
623
+
624
+ /**
625
+ * Description
626
+ * @method startWatch
627
+ * @param {String} pm2 method name
628
+ * @param {Object} application environment, should include id
629
+ * @param {Function} callback
630
+ */
631
+ Client.prototype.startWatch = function restartWatch(method, env, fn) {
632
+ debug('Calling startWatch');
633
+ this.client.call('startWatch', method, env, function() {
634
+ return fn ? fn() : false;
635
+ });
636
+ };
637
+
638
+ /**
639
+ * Description
640
+ * @method stopWatch
641
+ * @param {String} pm2 method name
642
+ * @param {Object} application environment, should include id
643
+ * @param {Function} callback
644
+ */
645
+ Client.prototype.stopWatch = function stopWatch(method, env, fn) {
646
+ debug('Calling stopWatch');
647
+ this.client.call('stopWatch', method, env, function() {
648
+ return fn ? fn() : false;
649
+ });
650
+ };
651
+
652
+ Client.prototype.getAllProcess = function(cb) {
653
+ var found_proc = [];
654
+
655
+ this.executeRemote('getMonitorData', {}, function(err, procs) {
656
+ if (err) {
657
+ Common.printError('Error retrieving process list: ' + err);
658
+ return cb(err);
659
+ }
660
+
661
+ return cb(null, procs);
662
+ });
663
+ };
664
+
665
+ Client.prototype.getAllProcessId = function(cb) {
666
+ var found_proc = [];
667
+
668
+ this.executeRemote('getMonitorData', {}, function(err, procs) {
669
+ if (err) {
670
+ Common.printError('Error retrieving process list: ' + err);
671
+ return cb(err);
672
+ }
673
+
674
+ return cb(null, procs.map(proc => proc.pm_id));
675
+ });
676
+ };
677
+
678
+ Client.prototype.getAllProcessIdWithoutModules = function(cb) {
679
+ var found_proc = [];
680
+
681
+ this.executeRemote('getMonitorData', {}, function(err, procs) {
682
+ if (err) {
683
+ Common.printError('Error retrieving process list: ' + err);
684
+ return cb(err);
685
+ }
686
+
687
+ var proc_ids = procs
688
+ .filter(proc => !proc.pm2_env.pmx_module)
689
+ .map(proc => proc.pm_id)
690
+
691
+ return cb(null, proc_ids);
692
+ });
693
+ };
694
+
695
+ Client.prototype.getProcessIdByName = function(name, force_all, cb) {
696
+ var found_proc = [];
697
+ var full_details = {};
698
+
699
+ if (typeof(cb) === 'undefined') {
700
+ cb = force_all;
701
+ force_all = false;
702
+ }
703
+
704
+ if (typeof(name) == 'number')
705
+ name = name.toString();
706
+
707
+ this.executeRemote('getMonitorData', {}, function(err, list) {
708
+ if (err) {
709
+ Common.printError('Error retrieving process list: ' + err);
710
+ return cb(err);
711
+ }
712
+
713
+ list.forEach(function(proc) {
714
+ if (proc.pm2_env.name == name || proc.pm2_env.pm_exec_path == path.resolve(name)) {
715
+ found_proc.push(proc.pm_id);
716
+ full_details[proc.pm_id] = proc;
717
+ }
718
+ });
719
+
720
+ return cb(null, found_proc, full_details);
721
+ });
722
+ };
723
+
724
+ Client.prototype.getProcessIdsByNamespace = function(namespace, force_all, cb) {
725
+ var found_proc = [];
726
+ var full_details = {};
727
+
728
+ if (typeof(cb) === 'undefined') {
729
+ cb = force_all;
730
+ force_all = false;
731
+ }
732
+
733
+ if (typeof(namespace) == 'number')
734
+ namespace = namespace.toString();
735
+
736
+ this.executeRemote('getMonitorData', {}, function(err, list) {
737
+ if (err) {
738
+ Common.printError('Error retrieving process list: ' + err);
739
+ return cb(err);
740
+ }
741
+
742
+ list.forEach(function(proc) {
743
+ if (proc.pm2_env.namespace == namespace) {
744
+ found_proc.push(proc.pm_id);
745
+ full_details[proc.pm_id] = proc;
746
+ }
747
+ });
748
+
749
+ return cb(null, found_proc, full_details);
750
+ });
751
+ };
752
+
753
+ Client.prototype.getProcessByName = function(name, cb) {
754
+ var found_proc = [];
755
+
756
+ this.executeRemote('getMonitorData', {}, function(err, list) {
757
+ if (err) {
758
+ Common.printError('Error retrieving process list: ' + err);
759
+ return cb(err);
760
+ }
761
+
762
+ list.forEach(function(proc) {
763
+ if (proc.pm2_env.name == name ||
764
+ proc.pm2_env.pm_exec_path == path.resolve(name)) {
765
+ found_proc.push(proc);
766
+ }
767
+ });
768
+
769
+ return cb(null, found_proc);
770
+ });
771
+ };
772
+
773
+ Client.prototype.getProcessByNameOrId = function (nameOrId, cb) {
774
+ var foundProc = [];
775
+
776
+ this.executeRemote('getMonitorData', {}, function (err, list) {
777
+ if (err) {
778
+ Common.printError('Error retrieving process list: ' + err);
779
+ return cb(err);
780
+ }
781
+
782
+ list.forEach(function (proc) {
783
+ if (proc.pm2_env.name === nameOrId ||
784
+ proc.pm2_env.pm_exec_path === path.resolve(nameOrId) ||
785
+ proc.pid === parseInt(nameOrId) ||
786
+ proc.pm2_env.pm_id === parseInt(nameOrId)) {
787
+ foundProc.push(proc);
788
+ }
789
+ });
790
+
791
+ return cb(null, foundProc);
792
+ });
793
+ };