@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.
- package/CHANGELOG.md +2607 -0
- package/CONTRIBUTING.md +124 -0
- package/GNU-AGPL-3.0.txt +665 -0
- package/LICENSE +1 -0
- package/README.md +255 -2
- package/SECURITY.md +41 -0
- package/bin/pm2 +3 -0
- package/bin/pm2-dev +3 -0
- package/bin/pm2-docker +3 -0
- package/bin/pm2-runtime +3 -0
- package/bin/pm2.ps1 +3 -0
- package/constants.js +113 -0
- package/index.js +12 -0
- package/lib/API/Configuration.js +212 -0
- package/lib/API/Containerizer.js +335 -0
- package/lib/API/Dashboard.js +726 -0
- package/lib/API/Deploy.js +117 -0
- package/lib/API/Extra.js +798 -0
- package/lib/API/ExtraMgmt/Docker.js +30 -0
- package/lib/API/Log.js +388 -0
- package/lib/API/LogManagement.js +351 -0
- package/lib/API/Modules/LOCAL.js +122 -0
- package/lib/API/Modules/Modularizer.js +148 -0
- package/lib/API/Modules/NPM.js +443 -0
- package/lib/API/Modules/TAR.js +368 -0
- package/lib/API/Modules/flagExt.js +46 -0
- package/lib/API/Modules/index.js +120 -0
- package/lib/API/Monit.js +247 -0
- package/lib/API/Serve.js +497 -0
- package/lib/API/Startup.js +630 -0
- package/lib/API/UX/helpers.js +213 -0
- package/lib/API/UX/index.js +9 -0
- package/lib/API/UX/pm2-describe.js +197 -0
- package/lib/API/UX/pm2-ls-minimal.js +31 -0
- package/lib/API/UX/pm2-ls.js +581 -0
- package/lib/API/Version.js +383 -0
- package/lib/API/interpreter.json +12 -0
- package/lib/API/pm2-plus/PM2IO.js +381 -0
- package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
- package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
- package/lib/API/pm2-plus/helpers.js +97 -0
- package/lib/API/pm2-plus/link.js +126 -0
- package/lib/API/pm2-plus/pres/motd +16 -0
- package/lib/API/pm2-plus/pres/motd.update +26 -0
- package/lib/API/pm2-plus/pres/welcome +28 -0
- package/lib/API/pm2-plus/process-selector.js +52 -0
- package/lib/API/schema.json +375 -0
- package/lib/API.js +1980 -0
- package/lib/Client.js +793 -0
- package/lib/Common.js +949 -0
- package/lib/Configuration.js +309 -0
- package/lib/Daemon.js +468 -0
- package/lib/Event.js +37 -0
- package/lib/God/ActionMethods.js +955 -0
- package/lib/God/ClusterMode.js +192 -0
- package/lib/God/ForkMode.js +205 -0
- package/lib/God/Methods.js +292 -0
- package/lib/God/Reload.js +300 -0
- package/lib/God/StdioLogger.js +239 -0
- package/lib/God.js +653 -0
- package/lib/HttpInterface.js +75 -0
- package/lib/OtelManager.js +74 -0
- package/lib/ProcessContainer.js +321 -0
- package/lib/ProcessContainerBun.js +355 -0
- package/lib/ProcessContainerFork.js +42 -0
- package/lib/ProcessContainerForkBun.js +31 -0
- package/lib/ProcessUtils.js +74 -0
- package/lib/TreeKill.js +111 -0
- package/lib/Utility.js +304 -0
- package/lib/VersionCheck.js +46 -0
- package/lib/Watcher.js +117 -0
- package/lib/Worker.js +200 -0
- package/lib/binaries/CLI.js +1092 -0
- package/lib/binaries/DevCLI.js +183 -0
- package/lib/binaries/Runtime.js +101 -0
- package/lib/binaries/Runtime4Docker.js +192 -0
- package/lib/completion.js +229 -0
- package/lib/completion.sh +40 -0
- package/lib/motd +36 -0
- package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
- package/lib/templates/ecosystem-es.tpl +24 -0
- package/lib/templates/ecosystem-simple-es.tpl +8 -0
- package/lib/templates/ecosystem-simple.tpl +6 -0
- package/lib/templates/ecosystem.tpl +22 -0
- package/lib/templates/init-scripts/launchd.tpl +35 -0
- package/lib/templates/init-scripts/openrc.tpl +52 -0
- package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
- package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
- package/lib/templates/init-scripts/rcd.tpl +44 -0
- package/lib/templates/init-scripts/smf.tpl +43 -0
- package/lib/templates/init-scripts/systemd-online.tpl +22 -0
- package/lib/templates/init-scripts/systemd.tpl +22 -0
- package/lib/templates/init-scripts/upstart.tpl +103 -0
- package/lib/templates/logrotate.d/pm2 +10 -0
- package/lib/templates/sample-apps/http-server/README.md +14 -0
- package/lib/templates/sample-apps/http-server/api.js +9 -0
- package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
- package/lib/templates/sample-apps/http-server/package.json +11 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
- package/lib/templates/sample-apps/python-app/README.md +4 -0
- package/lib/templates/sample-apps/python-app/echo.py +7 -0
- package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/python-app/package.json +11 -0
- package/lib/tools/Config.js +251 -0
- package/lib/tools/SysMetrics.js +375 -0
- package/lib/tools/copydirSync.js +101 -0
- package/lib/tools/deleteFolderRecursive.js +19 -0
- package/lib/tools/find-package-json.js +74 -0
- package/lib/tools/fmt.js +72 -0
- package/lib/tools/interpreter.js +88 -0
- package/lib/tools/isbinaryfile.js +94 -0
- package/lib/tools/json5.js +752 -0
- package/lib/tools/multimeter/bar.js +111 -0
- package/lib/tools/multimeter/charm/encode.js +18 -0
- package/lib/tools/multimeter/charm/index.js +305 -0
- package/lib/tools/multimeter/index.js +76 -0
- package/lib/tools/open.js +56 -0
- package/lib/tools/passwd.js +58 -0
- package/lib/tools/pm2-config-keys.js +165 -0
- package/lib/tools/prompt.js +76 -0
- package/lib/tools/serviceStop.js +149 -0
- package/lib/tools/sexec.js +55 -0
- package/lib/tools/treeify.js +113 -0
- package/lib/tools/typestrip.js +55 -0
- package/lib/tools/which.js +120 -0
- package/lib/tools/xdg-open +861 -0
- package/modules/blessed/LICENSE +20 -0
- package/modules/blessed/README.md +34 -0
- package/modules/blessed/index.js +1 -0
- package/modules/blessed/lib/alias.js +526 -0
- package/modules/blessed/lib/blessed.js +32 -0
- package/modules/blessed/lib/colors.js +530 -0
- package/modules/blessed/lib/events.js +189 -0
- package/modules/blessed/lib/gpmclient.js +221 -0
- package/modules/blessed/lib/helpers.js +165 -0
- package/modules/blessed/lib/keys.js +339 -0
- package/modules/blessed/lib/program.js +4297 -0
- package/modules/blessed/lib/tput.js +3022 -0
- package/modules/blessed/lib/unicode.js +885 -0
- package/modules/blessed/lib/widget.js +32 -0
- package/modules/blessed/lib/widgets/box.js +34 -0
- package/modules/blessed/lib/widgets/element.js +2570 -0
- package/modules/blessed/lib/widgets/list.js +599 -0
- package/modules/blessed/lib/widgets/log.js +83 -0
- package/modules/blessed/lib/widgets/node.js +282 -0
- package/modules/blessed/lib/widgets/screen.js +2298 -0
- package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
- package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
- package/modules/blessed/lib/widgets/text.js +35 -0
- package/modules/blessed/usr/linux +0 -0
- package/modules/blessed/usr/windows-ansi +0 -0
- package/modules/blessed/usr/xterm +0 -0
- package/modules/blessed/usr/xterm-256color +0 -0
- package/modules/blessed/usr/xterm.termcap +243 -0
- package/modules/chokidar/LICENSE +21 -0
- package/modules/chokidar/VENDORED.md +76 -0
- package/modules/chokidar/index.js +979 -0
- package/modules/chokidar/lib/brace-expand.js +180 -0
- package/modules/chokidar/lib/constants.js +66 -0
- package/modules/chokidar/lib/fsevents-handler.js +526 -0
- package/modules/chokidar/lib/nodefs-handler.js +654 -0
- package/modules/fclone.js +49 -0
- package/modules/pm2-axon/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/History.md +5 -0
- package/modules/pm2-axon/lib/configurable/Makefile +7 -0
- package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
- package/modules/pm2-axon/lib/configurable/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
- package/modules/pm2-axon/lib/configurable/package.json +68 -0
- package/modules/pm2-axon/lib/index.js +44 -0
- package/modules/pm2-axon/lib/plugins/queue.js +71 -0
- package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
- package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
- package/modules/pm2-axon/lib/sockets/pub.js +77 -0
- package/modules/pm2-axon/lib/sockets/pull.js +37 -0
- package/modules/pm2-axon/lib/sockets/push.js +32 -0
- package/modules/pm2-axon/lib/sockets/rep.js +73 -0
- package/modules/pm2-axon/lib/sockets/req.js +101 -0
- package/modules/pm2-axon/lib/sockets/sock.js +430 -0
- package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
- package/modules/pm2-axon/lib/sockets/sub.js +147 -0
- package/modules/pm2-axon/lib/utils.js +19 -0
- package/modules/pm2-axon-rpc/index.js +3 -0
- package/modules/pm2-axon-rpc/lib/client.js +63 -0
- package/modules/pm2-axon-rpc/lib/server.js +139 -0
- package/modules/pm2-io-agent/LICENSE +661 -0
- package/modules/pm2-io-agent/README.md +62 -0
- package/modules/pm2-io-agent/config.js +30 -0
- package/modules/pm2-io-agent/constants.js +118 -0
- package/modules/pm2-io-agent/index.js +2 -0
- package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
- package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
- package/modules/pm2-io-agent/src/PM2Client.js +112 -0
- package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
- package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
- package/modules/pm2-io-agent/src/Utility.js +392 -0
- package/modules/pm2-io-agent/src/WatchDog.js +70 -0
- package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
- package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
- package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
- package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
- package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
- package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
- package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
- package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
- package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
- package/modules/pm2-io-agent/src/utils/units.js +10 -0
- package/modules/pm2-io-bpm/configuration.js +141 -0
- package/modules/pm2-io-bpm/constants.js +10 -0
- package/modules/pm2-io-bpm/featureManager.js +110 -0
- package/modules/pm2-io-bpm/features/dependencies.js +48 -0
- package/modules/pm2-io-bpm/features/events.js +50 -0
- package/modules/pm2-io-bpm/features/metrics.js +102 -0
- package/modules/pm2-io-bpm/features/notify.js +238 -0
- package/modules/pm2-io-bpm/features/profiling.js +70 -0
- package/modules/pm2-io-bpm/features/tracing.js +174 -0
- package/modules/pm2-io-bpm/index.js +11 -0
- package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
- package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
- package/modules/pm2-io-bpm/metrics/network.js +132 -0
- package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
- package/modules/pm2-io-bpm/metrics/v8.js +104 -0
- package/modules/pm2-io-bpm/otel/constants.js +7 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
- package/modules/pm2-io-bpm/pmx.js +380 -0
- package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
- package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
- package/modules/pm2-io-bpm/serviceManager.js +21 -0
- package/modules/pm2-io-bpm/services/actions.js +159 -0
- package/modules/pm2-io-bpm/services/inspector.js +43 -0
- package/modules/pm2-io-bpm/services/metrics.js +186 -0
- package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
- package/modules/pm2-io-bpm/services/transport.js +11 -0
- package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
- package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
- package/modules/pm2-io-bpm/utils/EDS.js +113 -0
- package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
- package/modules/pm2-io-bpm/utils/autocast.js +61 -0
- package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
- package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
- package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
- package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
- package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
- package/modules/pm2-io-bpm/utils/module.js +67 -0
- package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
- package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
- package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
- package/modules/pm2-io-bpm/utils/units.js +16 -0
- package/modules/vizion/LICENSE +13 -0
- package/modules/vizion/README.md +133 -0
- package/modules/vizion/index.js +1 -0
- package/modules/vizion/lib/git/git.js +247 -0
- package/modules/vizion/lib/vizion.js +52 -0
- package/package.json +170 -4
- package/paths.js +121 -0
- package/pm2 +11 -0
- package/types/index.d.ts +732 -0
- package/types/tsconfig.json +14 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const fs = require('fs')
|
|
4
|
+
const rpc = require('../../pm2-axon-rpc')
|
|
5
|
+
const axon = require('../../pm2-axon')
|
|
6
|
+
const log = require('debug')('interactor:daemon')
|
|
7
|
+
const os = require('os')
|
|
8
|
+
const cst = require('../constants.js')
|
|
9
|
+
const ReverseInteractor = require('./reverse/ReverseInteractor.js')
|
|
10
|
+
const PushInteractor = require('./push/PushInteractor.js')
|
|
11
|
+
const Utility = require('./Utility.js')
|
|
12
|
+
const PM2Client = require('./PM2Client.js')
|
|
13
|
+
const TransporterInterface = require('./TransporterInterface.js')
|
|
14
|
+
const domain = require('domain') // eslint-disable-line
|
|
15
|
+
const WatchDog = require('./WatchDog')
|
|
16
|
+
const InteractorClient = require('./InteractorClient')
|
|
17
|
+
const path = require('path')
|
|
18
|
+
const pkg = require('../../../package.json')
|
|
19
|
+
|
|
20
|
+
global._logs = false
|
|
21
|
+
|
|
22
|
+
const InteractorDaemon = module.exports = class InteractorDaemon {
|
|
23
|
+
constructor () {
|
|
24
|
+
this.opts = this.retrieveConf()
|
|
25
|
+
|
|
26
|
+
log(`MACHINE_NAME=${this.opts.MACHINE_NAME}`)
|
|
27
|
+
log(`PUBLIC_KEY=${this.opts.PUBLIC_KEY}`)
|
|
28
|
+
log(`ROOT_URL=${cst.KEYMETRICS_ROOT_URL}`)
|
|
29
|
+
|
|
30
|
+
this.DAEMON_ACTIVE = false
|
|
31
|
+
this.transport = new TransporterInterface(this.opts, this)
|
|
32
|
+
.bind('websocket')
|
|
33
|
+
this.transport.on('error', (err) => {
|
|
34
|
+
return console.error('[NETWORK] Error : ' + err.message || err)
|
|
35
|
+
})
|
|
36
|
+
this.httpClient = new Utility.HTTPClient()
|
|
37
|
+
this._online = true
|
|
38
|
+
|
|
39
|
+
if (cst.IS_BUN === false)
|
|
40
|
+
this._internalDebugger()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Use process.send() if connected
|
|
45
|
+
* @param {Object} data
|
|
46
|
+
*/
|
|
47
|
+
sendToParent (data) {
|
|
48
|
+
if (!process.connected || !process.send || cst.IS_BUN == true)
|
|
49
|
+
return console.log('Could not send data to parent')
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
process.send(data)
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.trace('Parent process disconnected')
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get an interface for communicating with PM2 daemon
|
|
60
|
+
* @private
|
|
61
|
+
* @return {PM2Client}
|
|
62
|
+
*/
|
|
63
|
+
getPM2Client () {
|
|
64
|
+
if (!this._ipm2) {
|
|
65
|
+
this._ipm2 = new PM2Client()
|
|
66
|
+
}
|
|
67
|
+
return this._ipm2
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Terminate aconnections and exit
|
|
72
|
+
* @param {cb} callback called at the end
|
|
73
|
+
*/
|
|
74
|
+
exit (err, cb) {
|
|
75
|
+
log('Exiting Interactor')
|
|
76
|
+
// remove signal handlers to avoid listener leaks
|
|
77
|
+
if (this._sigusr1Handler) process.removeListener('SIGUSR1', this._sigusr1Handler)
|
|
78
|
+
if (this._sigusr2Handler) process.removeListener('SIGUSR2', this._sigusr2Handler)
|
|
79
|
+
// clear workers
|
|
80
|
+
if (this._workerEndpoint) clearInterval(this._workerEndpoint)
|
|
81
|
+
|
|
82
|
+
// stop interactors
|
|
83
|
+
if (this.reverse) this.reverse.stop()
|
|
84
|
+
if (this.push) this.push.stop()
|
|
85
|
+
|
|
86
|
+
if (this._ipm2) this._ipm2.disconnect()
|
|
87
|
+
if (this.watchDog) this.watchDog.stop()
|
|
88
|
+
// stop transport
|
|
89
|
+
if (this.transport) this.transport.disconnect()
|
|
90
|
+
|
|
91
|
+
if (!err) {
|
|
92
|
+
try {
|
|
93
|
+
fs.unlinkSync(cst.INTERACTOR_RPC_PORT)
|
|
94
|
+
fs.unlinkSync(cst.INTERACTOR_PID_PATH)
|
|
95
|
+
} catch (err) {}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!this._rpc || !this._rpc.sock) {
|
|
99
|
+
return process.exit(cst.ERROR_EXIT)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (typeof cb === 'function') {
|
|
103
|
+
cb()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
setTimeout(() => {
|
|
107
|
+
this._rpc.sock.close(() => {
|
|
108
|
+
log('RPC server closed')
|
|
109
|
+
process.exit(err ? cst.ERROR_EXIT : cst.SUCCESS_EXIT)
|
|
110
|
+
})
|
|
111
|
+
}, 10)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Start a RPC server and expose it throught a socket file
|
|
116
|
+
*/
|
|
117
|
+
startRPC (opts) {
|
|
118
|
+
log('Launching Interactor RPC server (bind to %s)', cst.INTERACTOR_RPC_PORT)
|
|
119
|
+
const rep = axon.socket('rep')
|
|
120
|
+
const rpcServer = new rpc.Server(rep)
|
|
121
|
+
const self = this
|
|
122
|
+
rep.bind(cst.INTERACTOR_RPC_PORT)
|
|
123
|
+
|
|
124
|
+
rpcServer.expose({
|
|
125
|
+
kill: function (cb) {
|
|
126
|
+
log('Shutdown request received via RPC')
|
|
127
|
+
return self.exit(null, cb)
|
|
128
|
+
},
|
|
129
|
+
getInfos: function (cb) {
|
|
130
|
+
if (self.opts && self.DAEMON_ACTIVE === true) {
|
|
131
|
+
return cb(null, {
|
|
132
|
+
machine_name: self.opts.MACHINE_NAME,
|
|
133
|
+
public_key: self.opts.PUBLIC_KEY,
|
|
134
|
+
secret_key: self.opts.SECRET_KEY,
|
|
135
|
+
remote_host: self.km_data.endpoints.web,
|
|
136
|
+
connected: self.transport.isConnected(),
|
|
137
|
+
transporters: self.transport.getActiveTransporters(),
|
|
138
|
+
socket_path: cst.INTERACTOR_RPC_PORT,
|
|
139
|
+
pm2_home_monitored: cst.PM2_HOME
|
|
140
|
+
})
|
|
141
|
+
} else {
|
|
142
|
+
return cb(null)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
return rpcServer
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Handle specific signals to launch memory / cpu profiling
|
|
151
|
+
* if available in node
|
|
152
|
+
*/
|
|
153
|
+
_internalDebugger () {
|
|
154
|
+
const inspector = require('inspector')
|
|
155
|
+
const state = {
|
|
156
|
+
heap: false,
|
|
157
|
+
cpu: false,
|
|
158
|
+
session: null
|
|
159
|
+
}
|
|
160
|
+
const commands = {
|
|
161
|
+
heap: {
|
|
162
|
+
start: 'HeapProfiler.startSampling',
|
|
163
|
+
stop: 'HeapProfiler.stopSampling'
|
|
164
|
+
},
|
|
165
|
+
cpu: {
|
|
166
|
+
start: 'Profiler.start',
|
|
167
|
+
stop: 'Profiler.stop'
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const handleSignal = type => {
|
|
172
|
+
return _ => {
|
|
173
|
+
if (state.session === null) {
|
|
174
|
+
state.session = new inspector.Session()
|
|
175
|
+
state.session.connect()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const isAlreadyEnabled = state[type]
|
|
179
|
+
const debuggerCommands = commands[type]
|
|
180
|
+
const profilerDomain = type === 'cpu' ? 'Profiler' : 'HeapProfiler'
|
|
181
|
+
const fileExt = type === 'heap' ? '.heapprofile' : '.cpuprofile'
|
|
182
|
+
|
|
183
|
+
if (isAlreadyEnabled) {
|
|
184
|
+
// stopping the profiling and writting it to disk if its running
|
|
185
|
+
console.log(`[DEBUG] Stopping ${type.toUpperCase()} Profiling`)
|
|
186
|
+
state.session.post(debuggerCommands.stop, (err, data) => {
|
|
187
|
+
const profile = data.profile
|
|
188
|
+
if (err) return console.error(err)
|
|
189
|
+
const randomId = Math.random().toString(36)
|
|
190
|
+
const profilePath = path.resolve(os.tmpdir(), `${type}-${randomId}${fileExt}`)
|
|
191
|
+
|
|
192
|
+
fs.writeFileSync(profilePath, JSON.stringify(profile))
|
|
193
|
+
console.log(`[DEBUG] Writing file in ${profilePath}`)
|
|
194
|
+
state[type] = false
|
|
195
|
+
state.session.post(`${profilerDomain}.disable`)
|
|
196
|
+
})
|
|
197
|
+
} else {
|
|
198
|
+
// start the profiling otherwise
|
|
199
|
+
console.log(`[DEBUG] Starting ${type.toUpperCase()} Profiling`)
|
|
200
|
+
state.session.post(`${profilerDomain}.enable`, _ => {
|
|
201
|
+
state.session.post(debuggerCommands.start)
|
|
202
|
+
state[type] = true
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// use hook - store handlers for cleanup and avoid listener leaks
|
|
209
|
+
this._sigusr1Handler = handleSignal('cpu')
|
|
210
|
+
this._sigusr2Handler = handleSignal('heap')
|
|
211
|
+
process.on('SIGUSR1', this._sigusr1Handler)
|
|
212
|
+
process.on('SIGUSR2', this._sigusr2Handler)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Retrieve metadata about the system
|
|
217
|
+
*/
|
|
218
|
+
getSystemMetadata () {
|
|
219
|
+
return {
|
|
220
|
+
MACHINE_NAME: this.opts.MACHINE_NAME,
|
|
221
|
+
PUBLIC_KEY: this.opts.PUBLIC_KEY,
|
|
222
|
+
RECYCLE: this.opts.RECYCLE || false,
|
|
223
|
+
PM2_VERSION: process.env.PM2_VERSION,
|
|
224
|
+
MEMORY: os.totalmem() / 1000 / 1000,
|
|
225
|
+
HOSTNAME: os.hostname(),
|
|
226
|
+
CPUS: os.cpus()
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Ping root url to retrieve node info
|
|
232
|
+
* @private
|
|
233
|
+
* @param {Function} cb invoked with <Error, Object> where Object is the response sended by the server
|
|
234
|
+
*/
|
|
235
|
+
_pingRoot (cb) {
|
|
236
|
+
const data = this.getSystemMetadata()
|
|
237
|
+
|
|
238
|
+
this.httpClient.open({
|
|
239
|
+
url: this.opts.ROOT_URL + '/api/node/verifyPM2',
|
|
240
|
+
method: 'POST',
|
|
241
|
+
data: {
|
|
242
|
+
public_id: this.opts.PUBLIC_KEY,
|
|
243
|
+
private_id: this.opts.SECRET_KEY,
|
|
244
|
+
data: data
|
|
245
|
+
},
|
|
246
|
+
headers: {
|
|
247
|
+
'User-Agent': `PM2 Agent v${pkg.version}`
|
|
248
|
+
}
|
|
249
|
+
}, cb)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Ping root to verify retrieve and connect to the km endpoint
|
|
254
|
+
* @private
|
|
255
|
+
* @param {Function} cb invoked with <Error, Boolean>
|
|
256
|
+
*/
|
|
257
|
+
_verifyEndpoint (cb) {
|
|
258
|
+
if (typeof cb !== 'function') cb = function () {}
|
|
259
|
+
|
|
260
|
+
this._pingRoot((err, data) => {
|
|
261
|
+
if (err) {
|
|
262
|
+
log('Got an a error on ping root', err)
|
|
263
|
+
return cb(err)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
this.km_data = data
|
|
267
|
+
|
|
268
|
+
// Verify data integrity
|
|
269
|
+
if (data.disabled === true || data.pending === true) {
|
|
270
|
+
log('Interactor is disabled by admins')
|
|
271
|
+
return cb(new Error('Connection refused, you might have hit the limit of agents you can connect (send email at contact@keymetrics.io for more infos)'))
|
|
272
|
+
}
|
|
273
|
+
if (data.active === false) {
|
|
274
|
+
log('Interactor not active: %s', data.msg || 'no message')
|
|
275
|
+
return cb(null, data)
|
|
276
|
+
}
|
|
277
|
+
if (!data.endpoints) {
|
|
278
|
+
return cb(new Error(`Endpoints field not present (${JSON.stringify(data)})`))
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
this.DAEMON_ACTIVE = true
|
|
282
|
+
this.transport.connect(data.endpoints, cb)
|
|
283
|
+
})
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Retrieve configuration from environnement
|
|
288
|
+
*/
|
|
289
|
+
retrieveConf () {
|
|
290
|
+
let opts = {}
|
|
291
|
+
|
|
292
|
+
opts.MACHINE_NAME = process.env.PM2_MACHINE_NAME
|
|
293
|
+
opts.PUBLIC_KEY = process.env.PM2_PUBLIC_KEY
|
|
294
|
+
opts.PM2_BINARY_PATH = process.env.PM2_BINARY_PATH
|
|
295
|
+
opts.SECRET_KEY = process.env.PM2_SECRET_KEY
|
|
296
|
+
opts.RECYCLE = process.env.KM_RECYCLE ? JSON.parse(process.env.KM_RECYCLE) : false
|
|
297
|
+
opts.PM2_VERSION = process.env.PM2_VERSION || '0.0.0'
|
|
298
|
+
opts.AGENT_TRANSPORT_WEBSOCKET = process.env.AGENT_TRANSPORT_WEBSOCKET
|
|
299
|
+
opts.internal_ip = Utility.network.v4
|
|
300
|
+
|
|
301
|
+
opts.PM2_REMOTE_METHOD_ALLOWED = [
|
|
302
|
+
'restart',
|
|
303
|
+
'reload',
|
|
304
|
+
'reset',
|
|
305
|
+
'scale',
|
|
306
|
+
'startLogging',
|
|
307
|
+
'stopLogging',
|
|
308
|
+
'ping',
|
|
309
|
+
'launchSysMonitoring',
|
|
310
|
+
'deepUpdate'
|
|
311
|
+
]
|
|
312
|
+
|
|
313
|
+
if (!opts.MACHINE_NAME) {
|
|
314
|
+
console.error('You must provide a PM2_MACHINE_NAME environment variable')
|
|
315
|
+
process.exit(cst.ERROR_EXIT)
|
|
316
|
+
} else if (!opts.PUBLIC_KEY) {
|
|
317
|
+
console.error('You must provide a PM2_PUBLIC_KEY environment variable')
|
|
318
|
+
process.exit(cst.ERROR_EXIT)
|
|
319
|
+
} else if (!opts.SECRET_KEY) {
|
|
320
|
+
console.error('You must provide a PM2_SECRET_KEY environment variable')
|
|
321
|
+
process.exit(cst.ERROR_EXIT)
|
|
322
|
+
}
|
|
323
|
+
return opts
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Ping root url to retrieve node info
|
|
328
|
+
* @private
|
|
329
|
+
* @param {Function} cb invoked with <Error> [optional]
|
|
330
|
+
*/
|
|
331
|
+
start (cb) {
|
|
332
|
+
let retries = 0
|
|
333
|
+
this._rpc = this.startRPC()
|
|
334
|
+
this.opts.ROOT_URL = cst.KEYMETRICS_ROOT_URL
|
|
335
|
+
|
|
336
|
+
const verifyEndpointCallback = (err, result) => {
|
|
337
|
+
if (err) {
|
|
338
|
+
log('Error while trying to retrieve endpoints : ' + (err.message || err))
|
|
339
|
+
if (retries++ < 30 && process.env.NODE_ENV !== 'test') {
|
|
340
|
+
log('Retrying to retrieve endpoints...')
|
|
341
|
+
return setTimeout(_ => {
|
|
342
|
+
return this._verifyEndpoint(verifyEndpointCallback)
|
|
343
|
+
}, 200 * retries)
|
|
344
|
+
}
|
|
345
|
+
this.sendToParent({ error: true, msg: err.message || err })
|
|
346
|
+
return this.exit(new Error('Error retrieving endpoints'))
|
|
347
|
+
}
|
|
348
|
+
if (result === false) {
|
|
349
|
+
log('False returned while trying to retrieve endpoints')
|
|
350
|
+
return this.exit(new Error('Error retrieving endpoints'))
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// send data over IPC for CLI feedback
|
|
354
|
+
this.sendToParent({
|
|
355
|
+
error: false,
|
|
356
|
+
km_data: this.km_data,
|
|
357
|
+
online: true,
|
|
358
|
+
pid: process.pid,
|
|
359
|
+
machine_name: this.opts.MACHINE_NAME,
|
|
360
|
+
public_key: this.opts.PUBLIC_KEY,
|
|
361
|
+
secret_key: this.opts.SECRET_KEY,
|
|
362
|
+
reverse_interaction: this.opts.REVERSE_INTERACT
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
if (result && typeof result === 'object' &&
|
|
366
|
+
result.error === true && result.active === false) {
|
|
367
|
+
log(`Error when connecting: ${result.msg}`)
|
|
368
|
+
return this.exit(new Error(`Error when connecting: ${result.msg}`))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// start workers
|
|
372
|
+
this._workerEndpoint = setInterval(this._verifyEndpoint.bind(this, (err, result) => {
|
|
373
|
+
if (err) return
|
|
374
|
+
// We need to exit agent if bucket is disabled (trialing)
|
|
375
|
+
if (result && typeof result === 'object' && result.error === true && result.active === false) {
|
|
376
|
+
log(`Error when connecting: ${result.msg}, disconnecting transporters`)
|
|
377
|
+
return this.transport.disconnect()
|
|
378
|
+
}
|
|
379
|
+
}), 60000)
|
|
380
|
+
// start interactors
|
|
381
|
+
this.watchDog = WatchDog
|
|
382
|
+
|
|
383
|
+
setTimeout(() => {
|
|
384
|
+
log('>> PM2 Watchdog started')
|
|
385
|
+
this.watchDog.start({
|
|
386
|
+
pm2_binary_path: this.opts.PM2_BINARY_PATH,
|
|
387
|
+
conf: {
|
|
388
|
+
ipm2: this.getPM2Client()
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
}, 30 * 1000)
|
|
392
|
+
|
|
393
|
+
this.push = new PushInteractor(this.opts, this.getPM2Client(), this.transport)
|
|
394
|
+
this.reverse = new ReverseInteractor(this.opts, this.getPM2Client(), this.transport)
|
|
395
|
+
this.push.start()
|
|
396
|
+
this.reverse.start()
|
|
397
|
+
log('Interactor daemon started')
|
|
398
|
+
if (cb) {
|
|
399
|
+
setTimeout(cb, 20)
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return this._verifyEndpoint(verifyEndpointCallback)
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// If its the entry file launch the daemon
|
|
407
|
+
// otherwise we just required it to use a function
|
|
408
|
+
if (require.main === module) {
|
|
409
|
+
const d = domain.create()
|
|
410
|
+
let daemon = null
|
|
411
|
+
|
|
412
|
+
d.on('error', function (err) {
|
|
413
|
+
console.error('-- FATAL EXCEPTION happened --')
|
|
414
|
+
console.error(new Date())
|
|
415
|
+
console.error(err.stack)
|
|
416
|
+
console.log('Re-initiating Agent')
|
|
417
|
+
|
|
418
|
+
InteractorClient.getOrSetConf(cst, null, (err, infos) => {
|
|
419
|
+
if (err || !infos) {
|
|
420
|
+
if (err) {
|
|
421
|
+
console.error('[PM2 Agent] Failed to rescue agent :')
|
|
422
|
+
console.error(err || new Error(`Cannot find configuration to connect to backend`))
|
|
423
|
+
return process.exit(1)
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
console.log(`[PM2 Agent] Using (Public key: ${infos.public_key}) (Private key: ${infos.secret_key}) (Info node: ${infos.info_node})`)
|
|
427
|
+
|
|
428
|
+
// Exit anyway the errored agent
|
|
429
|
+
var timeout = setTimeout(_ => {
|
|
430
|
+
console.error('Daemonization of failsafe agent did not worked')
|
|
431
|
+
daemon.exit(err)
|
|
432
|
+
}, 2000)
|
|
433
|
+
|
|
434
|
+
InteractorClient.daemonize(cst, infos, (err) => {
|
|
435
|
+
if (err) {
|
|
436
|
+
log('[PM2 Agent] Failed to rescue agent :')
|
|
437
|
+
log(err)
|
|
438
|
+
} else {
|
|
439
|
+
log(`Succesfully launched new agent`)
|
|
440
|
+
}
|
|
441
|
+
clearTimeout(timeout)
|
|
442
|
+
daemon.exit(err)
|
|
443
|
+
})
|
|
444
|
+
})
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
d.run(_ => {
|
|
448
|
+
daemon = new InteractorDaemon()
|
|
449
|
+
|
|
450
|
+
process.title = `PM2 Agent v${pkg.version}: (${cst.PM2_HOME})`
|
|
451
|
+
|
|
452
|
+
log('[PM2 Agent] Launching agent')
|
|
453
|
+
daemon.start()
|
|
454
|
+
})
|
|
455
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const axon = require('../../pm2-axon')
|
|
4
|
+
const cst = require('../constants.js')
|
|
5
|
+
const rpc = require('../../pm2-axon-rpc')
|
|
6
|
+
const log = require('debug')('interactor:pm2:client')
|
|
7
|
+
const EventEmitter = require('events').EventEmitter
|
|
8
|
+
const PM2Interface = require('./PM2Interface')
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* PM2 API Wrapper used to setup connection with the daemon
|
|
12
|
+
* @param {Object} opts options
|
|
13
|
+
* @param {String} opts.sub_port socket file of the PM2 bus [optionnal]
|
|
14
|
+
* @param {String} opts.rpc_port socket file of the PM2 RPC server [optionnal]
|
|
15
|
+
*/
|
|
16
|
+
module.exports = class PM2Client extends EventEmitter {
|
|
17
|
+
constructor (opts) {
|
|
18
|
+
super()
|
|
19
|
+
const subSocket = (opts && opts.sub_port) || cst.DAEMON_PUB_PORT
|
|
20
|
+
const rpcSocket = (opts && opts.rpc_port) || cst.DAEMON_RPC_PORT
|
|
21
|
+
|
|
22
|
+
const sub = axon.socket('sub-emitter')
|
|
23
|
+
this.sub_sock = sub.connect(subSocket)
|
|
24
|
+
this.bus = sub
|
|
25
|
+
|
|
26
|
+
const req = axon.socket('req')
|
|
27
|
+
this.rpc_sock = req.connect(rpcSocket)
|
|
28
|
+
this.rpc_client = new rpc.Client(req)
|
|
29
|
+
|
|
30
|
+
this.rpc = {}
|
|
31
|
+
|
|
32
|
+
this.rpc_sock.on('connect', _ => {
|
|
33
|
+
log('PM2 API Wrapper connected to PM2 Daemon via RPC')
|
|
34
|
+
this.generateMethods(_ => {
|
|
35
|
+
this.pm2Interface = new PM2Interface(this.rpc)
|
|
36
|
+
this.emit('ready')
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
this.rpc_sock.on('close', _ => {
|
|
41
|
+
log('pm2 rpc closed')
|
|
42
|
+
this.emit('closed')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
this.rpc_sock.on('reconnect attempt', _ => {
|
|
46
|
+
log('pm2 rpc reconnecting')
|
|
47
|
+
this.emit('reconnecting')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
this.sub_sock.on('connect', _ => {
|
|
51
|
+
log('bus ready')
|
|
52
|
+
this.emit('bus:ready')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
this.sub_sock.on('close', _ => {
|
|
56
|
+
log('bus closed')
|
|
57
|
+
this.emit('bus:closed')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
this.sub_sock.on('reconnect attempt', _ => {
|
|
61
|
+
log('bus reconnecting')
|
|
62
|
+
this.emit('bus:reconnecting')
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Disconnect socket connections. This will allow Node to exit automatically.
|
|
68
|
+
* Further calls to PM2 from this object will throw an error.
|
|
69
|
+
*/
|
|
70
|
+
disconnect () {
|
|
71
|
+
this.sub_sock.close()
|
|
72
|
+
this.rpc_sock.close()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Generate method by requesting exposed methods by PM2
|
|
77
|
+
* You can now control/interact with PM2
|
|
78
|
+
*/
|
|
79
|
+
generateMethods (cb) {
|
|
80
|
+
log('Requesting and generating RPC methods')
|
|
81
|
+
this.rpc_client.methods((err, methods) => {
|
|
82
|
+
if (err) return cb(err)
|
|
83
|
+
Object.keys(methods).forEach((key) => {
|
|
84
|
+
let method = methods[key]
|
|
85
|
+
|
|
86
|
+
log('+-- Creating %s method', method.name);
|
|
87
|
+
|
|
88
|
+
((name) => {
|
|
89
|
+
const self = this
|
|
90
|
+
this.rpc[name] = function () {
|
|
91
|
+
let args = Array.prototype.slice.call(arguments)
|
|
92
|
+
args.unshift(name)
|
|
93
|
+
self.rpc_client.call.apply(self.rpc_client, args)
|
|
94
|
+
}
|
|
95
|
+
})(method.name)
|
|
96
|
+
})
|
|
97
|
+
return cb()
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
remote (method, parameters, cb) {
|
|
102
|
+
log('remote send %s', method, parameters)
|
|
103
|
+
if (typeof this.pm2Interface[method] === 'undefined') {
|
|
104
|
+
return cb(new Error('Deprecated or invalid method'))
|
|
105
|
+
}
|
|
106
|
+
this.pm2Interface[method](parameters, cb)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
msgProcess (data, cb) {
|
|
110
|
+
this.rpc.msgProcess(data, cb)
|
|
111
|
+
}
|
|
112
|
+
}
|