@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,128 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
const debug = require('debug')('interactor:reverse')
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ReverseInteractor is the class that handle receiving event from KM
|
|
8
|
+
* @param {Object} opts interactor options
|
|
9
|
+
* @param {PM2} pm2 pm2 api
|
|
10
|
+
* @param {WebsocketTransport} transport websocket transport used to receive data to KM
|
|
11
|
+
*/
|
|
12
|
+
module.exports = class ReverseInteractor {
|
|
13
|
+
constructor (opts, ipm2, transport) {
|
|
14
|
+
this.ipm2 = ipm2
|
|
15
|
+
this.transport = transport
|
|
16
|
+
this.opts = opts
|
|
17
|
+
this.remoteMethodAlloweds = this.opts.PM2_REMOTE_METHOD_ALLOWED
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
start () {
|
|
21
|
+
debug('Reverse interactor is listening')
|
|
22
|
+
// action that trigger custom actions inside the code
|
|
23
|
+
this.transport.on('trigger:action', this._onCustomAction.bind(this))
|
|
24
|
+
this.transport.on('trigger:scoped_action', this._onCustomAction.bind(this))
|
|
25
|
+
// action that call pm2 api
|
|
26
|
+
this.transport.on('trigger:pm2:action', this._onPM2Action.bind(this))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
stop () {
|
|
30
|
+
debug('Reverse interactor is no longer listening')
|
|
31
|
+
this.transport.removeAllListeners('trigger:action')
|
|
32
|
+
this.transport.removeAllListeners('trigger:scoped_action')
|
|
33
|
+
this.transport.removeAllListeners('trigger:pm2:action')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Listener for custom actions that can be triggered by KM, either scoped or normal
|
|
38
|
+
* @param {Object} data
|
|
39
|
+
* @param {Object} data.action_name name of the action triggered
|
|
40
|
+
* @param {Object} [data.app_name] name of the process where the action need to be run
|
|
41
|
+
* @param {Object} [data.process_id] id of the process where the action need to be run
|
|
42
|
+
* @param {Object} [data.opts] parameters used to call the method
|
|
43
|
+
* @param {Object} [data.uuid] uuid used to recognized the scoped action (scoped action only)
|
|
44
|
+
*/
|
|
45
|
+
_onCustomAction (data) {
|
|
46
|
+
const type = data.uuid ? 'SCOPED' : 'REMOTE'
|
|
47
|
+
|
|
48
|
+
data.process_id = data.process_id !== undefined ? data.process_id : data.process.pm_id
|
|
49
|
+
debug('New %s action %s triggered for process %s', type, data.action_name, data.process_id)
|
|
50
|
+
// send the request to pmx via IPC
|
|
51
|
+
this.ipm2.msgProcess({
|
|
52
|
+
name: data.app_name,
|
|
53
|
+
id: data.process_id,
|
|
54
|
+
msg: data.action_name,
|
|
55
|
+
opts: data.opts || data.options || null,
|
|
56
|
+
action_name: data.action_name,
|
|
57
|
+
uuid: data.uuid
|
|
58
|
+
}, (err, res) => {
|
|
59
|
+
if (err) {
|
|
60
|
+
return this.transport.send('trigger:action:failure', {
|
|
61
|
+
success: false,
|
|
62
|
+
err: err.message || err,
|
|
63
|
+
id: data.process_id,
|
|
64
|
+
action_name: data.action_name
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
debug('Message received from AXM for proc_id : %s and action name %s', data.process_id, data.action_name)
|
|
68
|
+
return this.transport.send('trigger:action:success', {
|
|
69
|
+
success: true,
|
|
70
|
+
id: data.process_id,
|
|
71
|
+
action_name: data.action_name
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Handle when KM call a pm2 action
|
|
78
|
+
* @param {Object} data
|
|
79
|
+
* @param {Object} data.method_name the name of the pm2 method
|
|
80
|
+
* @param {Object} data.parameters optional parameters used to call the method
|
|
81
|
+
*/
|
|
82
|
+
_onPM2Action (data) {
|
|
83
|
+
// callback when the action has been executed
|
|
84
|
+
let callback = (err, res) => {
|
|
85
|
+
debug('PM2 action ended : pm2 %s (%s)', data.method_name, !err ? 'no error' : (err.message || err))
|
|
86
|
+
this.transport.send('trigger:pm2:result', {
|
|
87
|
+
ret: { err: err, data: res },
|
|
88
|
+
meta: {
|
|
89
|
+
method_name: data.method_name,
|
|
90
|
+
app_name: data.parameters.name,
|
|
91
|
+
machine_name: this.opts.MACHINE_NAME,
|
|
92
|
+
public_key: this.opts.PUBLIC_KEY
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
if (typeof data.method_name !== 'string') {
|
|
97
|
+
return debug('New PM2 action triggered with invalid method name: ', data.method_name)
|
|
98
|
+
}
|
|
99
|
+
if (this.remoteMethodAlloweds.indexOf(data.method_name) === -1) {
|
|
100
|
+
return callback(new Error('Method not allowed'))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
debug('New PM2 action triggered : pm2 %s %j', data.method_name, data.parameters)
|
|
104
|
+
|
|
105
|
+
const method = data.method_name
|
|
106
|
+
let parameters = data.parameters
|
|
107
|
+
try {
|
|
108
|
+
parameters = JSON.parse(JSON.stringify(data.parameters))
|
|
109
|
+
} catch (err) {
|
|
110
|
+
console.error(err)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (method === 'startLogging') {
|
|
114
|
+
global._logs = true
|
|
115
|
+
// Stop streaming logs automatically after timeout
|
|
116
|
+
clearTimeout(this._loggingTimeoutId)
|
|
117
|
+
this._loggingTimeoutId = setTimeout(function () {
|
|
118
|
+
global._logs = false
|
|
119
|
+
}, process.env.NODE_ENV === 'test' ? 10 : 120000)
|
|
120
|
+
return callback(null, 'Log streaming enabled')
|
|
121
|
+
} else if (method === 'stopLogging') {
|
|
122
|
+
global._logs = false
|
|
123
|
+
return callback(null, 'Log streaming disabled')
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return this.ipm2.remote(method, parameters, callback)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const log = require('debug')('interactor:transporter')
|
|
4
|
+
const EventEmitter2 = require('eventemitter2').EventEmitter2
|
|
5
|
+
const dns = require('dns')
|
|
6
|
+
const cst = require('../../constants.js')
|
|
7
|
+
|
|
8
|
+
module.exports = class Transporter extends EventEmitter2 {
|
|
9
|
+
constructor () {
|
|
10
|
+
super({
|
|
11
|
+
delimiter: ':',
|
|
12
|
+
wildcard: true
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Disconnect and connect to a url
|
|
18
|
+
* @param {String} url where the client will connect [optionnal]
|
|
19
|
+
* @param {Function} cb invoked with <err>
|
|
20
|
+
*/
|
|
21
|
+
reconnect (url, cb) {
|
|
22
|
+
log('Reconnect transporter')
|
|
23
|
+
this.disconnect()
|
|
24
|
+
this.connect(url, cb)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Broadcast the close event from websocket connection
|
|
29
|
+
* @private
|
|
30
|
+
* @param {Integer} code
|
|
31
|
+
* @param {String} reason
|
|
32
|
+
*/
|
|
33
|
+
_onClose (code, reason) {
|
|
34
|
+
log('Closed transporter')
|
|
35
|
+
this.disconnect()
|
|
36
|
+
this._reconnect()
|
|
37
|
+
this.emit('close', code, reason)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Broadcast the error event from websocket connection
|
|
42
|
+
* and eventually close the connection if it isnt already
|
|
43
|
+
* @private
|
|
44
|
+
* @param {Error} err
|
|
45
|
+
*/
|
|
46
|
+
_onError (err) {
|
|
47
|
+
log(`Error with transporter: ${err.message}`)
|
|
48
|
+
// close connection if needed
|
|
49
|
+
this.disconnect()
|
|
50
|
+
this._reconnect()
|
|
51
|
+
this.emit('error', err)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Worker that will empty the packet queue if the connection works.
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
_emptyQueue () {
|
|
59
|
+
// create the queue if it doesn't exist
|
|
60
|
+
if (!this.queue) {
|
|
61
|
+
this.queue = []
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
if (this.queue.length === 0) return
|
|
65
|
+
if (!this.isConnected()) return
|
|
66
|
+
|
|
67
|
+
log('Emptying queue (size : %d)', this.queue.length)
|
|
68
|
+
|
|
69
|
+
// re-send all of the data
|
|
70
|
+
while (this.queue.length > 0) {
|
|
71
|
+
if (!this.isConnected()) return
|
|
72
|
+
let packet = this.queue[0]
|
|
73
|
+
this.send(packet.channel, packet.data)
|
|
74
|
+
this.queue.shift()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Is internet reachable via DNS
|
|
80
|
+
* @private
|
|
81
|
+
* @param {Function} cb invoked with <boolean>
|
|
82
|
+
*/
|
|
83
|
+
_checkInternet (cb) {
|
|
84
|
+
dns.lookup('google.com', (err) => {
|
|
85
|
+
if (err && (err.code === 'ENOTFOUND' || err.code === 'EAI_AGAIN')) {
|
|
86
|
+
if (this._online) {
|
|
87
|
+
log('Internet is unreachable (DNS)')
|
|
88
|
+
}
|
|
89
|
+
this._online = false
|
|
90
|
+
} else {
|
|
91
|
+
if (!this._online) {
|
|
92
|
+
log('Internet is reachable again')
|
|
93
|
+
}
|
|
94
|
+
this._online = true
|
|
95
|
+
}
|
|
96
|
+
return cb(this._online)
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Strategy to reconnect to remote endpoint as soon as possible
|
|
102
|
+
* -> test internet connection with dns request (if fail retry in 2 sec)
|
|
103
|
+
* -> try to connect to endpoint (if fail retry in 5 sec)
|
|
104
|
+
*/
|
|
105
|
+
_reconnect () {
|
|
106
|
+
if (this._reconnecting === true) return
|
|
107
|
+
this._reconnecting = true
|
|
108
|
+
|
|
109
|
+
log('Trying to reconnect to remote endpoint')
|
|
110
|
+
this._checkInternet((online) => {
|
|
111
|
+
if (!online && !cst.PM2_DEBUG) {
|
|
112
|
+
log('Internet down, retry in 2 seconds ..')
|
|
113
|
+
this._reconnecting = false
|
|
114
|
+
return setTimeout(this._reconnect.bind(this), process.env.NODE_ENV === 'test' ? 1 : 2000)
|
|
115
|
+
}
|
|
116
|
+
this.connect((err) => {
|
|
117
|
+
if (err || !this.isConnected()) {
|
|
118
|
+
log('Endpoint down, retry in 5 seconds ...')
|
|
119
|
+
this._reconnecting = false
|
|
120
|
+
return setTimeout(this._reconnect.bind(this), process.env.NODE_ENV === 'test' ? 1 : 5000)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
log('Connection etablished with remote endpoint')
|
|
124
|
+
this._reconnecting = false
|
|
125
|
+
this._emptyQueue()
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const WebSocket = require('ws')
|
|
4
|
+
const log = require('debug')('interactor:websocket')
|
|
5
|
+
const cst = require('../../constants.js')
|
|
6
|
+
const pkg = require('../../../../package.json')
|
|
7
|
+
const Transporter = require('./Transporter')
|
|
8
|
+
const jsonPatch = require('fast-json-patch')
|
|
9
|
+
const fs = require('fs')
|
|
10
|
+
/**
|
|
11
|
+
* Websocket Transport used to communicate with KM
|
|
12
|
+
* @param {Object} opts options
|
|
13
|
+
* @param {Daemon} daemon Interactor instance
|
|
14
|
+
*/
|
|
15
|
+
module.exports = class WebsocketTransport extends Transporter {
|
|
16
|
+
constructor (opts, daemon) {
|
|
17
|
+
super()
|
|
18
|
+
log('WebsocketTransporter constructed')
|
|
19
|
+
this.opts = opts
|
|
20
|
+
this._daemon = daemon
|
|
21
|
+
this._ws = null
|
|
22
|
+
this.queue = []
|
|
23
|
+
this._last_status = null
|
|
24
|
+
this._resend_status = false
|
|
25
|
+
this._worker = setInterval(this._emptyQueue.bind(this), process.env.NODE_ENV === 'test' ? 2 : 10000)
|
|
26
|
+
this._heartbeater = setInterval(this._heartbeat.bind(this), 5000)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Send heartbeat to websocket server (every 5 sec)
|
|
31
|
+
*/
|
|
32
|
+
_heartbeat () {
|
|
33
|
+
if (!this.isConnected()) return false
|
|
34
|
+
return this._ws.ping()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Connect the websocket client to a url
|
|
39
|
+
* @param {String} url where the client will connect
|
|
40
|
+
* @param {Function} cb invoked with <err>
|
|
41
|
+
*/
|
|
42
|
+
connect (url, cb) {
|
|
43
|
+
if (typeof url === 'function') {
|
|
44
|
+
cb = url
|
|
45
|
+
url = this.endpoint
|
|
46
|
+
}
|
|
47
|
+
this.endpoint = url
|
|
48
|
+
|
|
49
|
+
if (!url) return cb(new Error('Websocket URL is not defined!'))
|
|
50
|
+
|
|
51
|
+
log('Connecting websocket transporter to %s...', url)
|
|
52
|
+
this._ws = new WebSocket(url, {
|
|
53
|
+
perMessageDeflate: process.env.WS_GZIP || false,
|
|
54
|
+
headers: {
|
|
55
|
+
'X-KM-PUBLIC': this.opts.PUBLIC_KEY,
|
|
56
|
+
'X-KM-SECRET': this.opts.SECRET_KEY,
|
|
57
|
+
'X-KM-SERVER': this.opts.MACHINE_NAME,
|
|
58
|
+
'X-PM2-VERSION': this.opts.PM2_VERSION || '0.0.0',
|
|
59
|
+
'X-PROTOCOL-VERSION': cst.PROTOCOL_VERSION,
|
|
60
|
+
'User-Agent': `PM2 Agent v${pkg.version}`
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
let onError = (err) => {
|
|
65
|
+
log('Error on websocket connect', err)
|
|
66
|
+
return cb(err)
|
|
67
|
+
}
|
|
68
|
+
this._ws.once('error', onError)
|
|
69
|
+
this._ws.once('open', () => {
|
|
70
|
+
this._resend_status = true
|
|
71
|
+
log(`Connected to ${url}`)
|
|
72
|
+
if (!this._ws) return false // an error occurred
|
|
73
|
+
this._ws.removeListener('error', onError)
|
|
74
|
+
this._ws.on('close', this._onClose.bind(this))
|
|
75
|
+
this._ws.on('error', this._onError.bind(this))
|
|
76
|
+
return cb()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
this._ws.on('message', this._onMessage.bind(this))
|
|
80
|
+
this._ws.on('ping', (data) => {
|
|
81
|
+
this._ws.pong()
|
|
82
|
+
})
|
|
83
|
+
this._ws.on('pong', (data) => {})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Disconnect clients
|
|
88
|
+
*/
|
|
89
|
+
disconnect () {
|
|
90
|
+
log('Disconnect websocket transporter')
|
|
91
|
+
if (this.isConnected()) {
|
|
92
|
+
this._ws.close(1000, 'Disconnecting')
|
|
93
|
+
}
|
|
94
|
+
this._ws = null
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Are push and reverse connections ready
|
|
99
|
+
* @return {Boolean}
|
|
100
|
+
*/
|
|
101
|
+
isConnected () {
|
|
102
|
+
return this._ws && this._ws.readyState === 1
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Send data to endpoints
|
|
107
|
+
* @param {String} channel
|
|
108
|
+
* @param {Object} data
|
|
109
|
+
*/
|
|
110
|
+
send (channel, data) {
|
|
111
|
+
if (!channel || !data) {
|
|
112
|
+
return log('Trying to send message without all necessary fields')
|
|
113
|
+
}
|
|
114
|
+
if (!this.isConnected()) {
|
|
115
|
+
if (!this._reconnecting) this._reconnect()
|
|
116
|
+
|
|
117
|
+
// do not buffer status/monitoring packet
|
|
118
|
+
if (channel === 'status' || channel === 'monitoring') return
|
|
119
|
+
|
|
120
|
+
log('Trying to send data while not connected, buffering ...')
|
|
121
|
+
|
|
122
|
+
// remove last element if the queue is full
|
|
123
|
+
if (this.queue.length >= cst.PACKET_QUEUE_SIZE) {
|
|
124
|
+
this.queue.shift()
|
|
125
|
+
}
|
|
126
|
+
return this.queue.push({ channel: channel, data: data })
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (channel === 'status' && process.env.WS_JSON_PATCH) {
|
|
130
|
+
if (this._last_status == null || this._resend_status == true) {
|
|
131
|
+
if (this._resend_status)
|
|
132
|
+
log('Sending fresh new status')
|
|
133
|
+
this._resend_status = false
|
|
134
|
+
this._last_status = data
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
let patch = jsonPatch.compare(this._last_status, data)
|
|
138
|
+
|
|
139
|
+
let packet = {
|
|
140
|
+
payload: patch,
|
|
141
|
+
channel: 'status:patch'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this._last_status = data
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
if (process.env.WS_JSON_PATCH_BENCH) {
|
|
148
|
+
fs.writeFileSync('status', JSON.stringify(data))
|
|
149
|
+
fs.writeFileSync('patch', JSON.stringify(packet))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return this._ws.send(JSON.stringify(packet), (err) => {
|
|
153
|
+
packet = null
|
|
154
|
+
if (err) {
|
|
155
|
+
this.emit('error', err)
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
log('Sending packet over for channel %s', channel)
|
|
162
|
+
let packet = {
|
|
163
|
+
payload: data,
|
|
164
|
+
channel: channel
|
|
165
|
+
}
|
|
166
|
+
this._ws.send(JSON.stringify(packet), {
|
|
167
|
+
compress: cst.COMPRESS_PROTOCOL || false
|
|
168
|
+
}, (err) => {
|
|
169
|
+
packet = null
|
|
170
|
+
if (err) {
|
|
171
|
+
this.emit('error', err)
|
|
172
|
+
// buffer the packet to send it when the connection will be up again
|
|
173
|
+
this.queue.push({ channel: channel, data: data })
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Message received from keymetrics
|
|
180
|
+
* @private
|
|
181
|
+
* @param {String} json packet
|
|
182
|
+
*/
|
|
183
|
+
_onMessage (data) {
|
|
184
|
+
try {
|
|
185
|
+
data = JSON.parse(data)
|
|
186
|
+
} catch (err) {
|
|
187
|
+
return log('Bad packet received from remote : %s', err.message || err)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (process.env.WS_JSON_PATCH && data.channel == 'status:resend') {
|
|
191
|
+
log(`Wrong patch sent to backend, resending fresh status`)
|
|
192
|
+
this._resend_status = true
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ensure that all required field are present
|
|
196
|
+
if (!data || !data.payload || !data.channel) {
|
|
197
|
+
return log('Received message without all necessary fields')
|
|
198
|
+
}
|
|
199
|
+
log('Recevied data on channel %s', data.channel)
|
|
200
|
+
this.emit(data.channel, data.payload)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Hacked https://github.com/felixge/node-measured
|
|
4
|
+
|
|
5
|
+
// Based on http://en.wikipedia.org/wiki/Binary_Heap
|
|
6
|
+
// as well as http://eloquentjavascript.net/appendix2.html
|
|
7
|
+
module.exports = BinaryHeap
|
|
8
|
+
function BinaryHeap (options) {
|
|
9
|
+
options = options || {}
|
|
10
|
+
|
|
11
|
+
this._elements = options.elements || []
|
|
12
|
+
this._score = options.score || this._score
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
BinaryHeap.prototype.add = function (/* elements */) {
|
|
16
|
+
for (var i = 0; i < arguments.length; i++) {
|
|
17
|
+
var element = arguments[i]
|
|
18
|
+
|
|
19
|
+
this._elements.push(element)
|
|
20
|
+
this._bubble(this._elements.length - 1)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
BinaryHeap.prototype.first = function () {
|
|
25
|
+
return this._elements[0]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
BinaryHeap.prototype.removeFirst = function () {
|
|
29
|
+
var root = this._elements[0]
|
|
30
|
+
var last = this._elements.pop()
|
|
31
|
+
|
|
32
|
+
if (this._elements.length > 0) {
|
|
33
|
+
this._elements[0] = last
|
|
34
|
+
this._sink(0)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return root
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
BinaryHeap.prototype.clone = function () {
|
|
41
|
+
return new BinaryHeap({
|
|
42
|
+
elements: this.toArray(),
|
|
43
|
+
score: this._score
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
BinaryHeap.prototype.toSortedArray = function () {
|
|
48
|
+
var array = []
|
|
49
|
+
var clone = this.clone()
|
|
50
|
+
|
|
51
|
+
while (true) {
|
|
52
|
+
var element = clone.removeFirst()
|
|
53
|
+
if (element === undefined) break
|
|
54
|
+
|
|
55
|
+
array.push(element)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return array
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
BinaryHeap.prototype.toArray = function () {
|
|
62
|
+
return [].concat(this._elements)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
BinaryHeap.prototype.size = function () {
|
|
66
|
+
return this._elements.length
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
BinaryHeap.prototype._bubble = function (bubbleIndex) {
|
|
70
|
+
var bubbleElement = this._elements[bubbleIndex]
|
|
71
|
+
var bubbleScore = this._score(bubbleElement)
|
|
72
|
+
|
|
73
|
+
while (bubbleIndex > 0) {
|
|
74
|
+
var parentIndex = this._parentIndex(bubbleIndex)
|
|
75
|
+
var parentElement = this._elements[parentIndex]
|
|
76
|
+
var parentScore = this._score(parentElement)
|
|
77
|
+
|
|
78
|
+
if (bubbleScore <= parentScore) break
|
|
79
|
+
|
|
80
|
+
this._elements[parentIndex] = bubbleElement
|
|
81
|
+
this._elements[bubbleIndex] = parentElement
|
|
82
|
+
bubbleIndex = parentIndex
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
BinaryHeap.prototype._sink = function (sinkIndex) {
|
|
87
|
+
var sinkElement = this._elements[sinkIndex]
|
|
88
|
+
var sinkScore = this._score(sinkElement)
|
|
89
|
+
var length = this._elements.length
|
|
90
|
+
|
|
91
|
+
while (true) {
|
|
92
|
+
var swapIndex = null
|
|
93
|
+
var swapScore = null
|
|
94
|
+
var swapElement = null
|
|
95
|
+
var childIndexes = this._childIndexes(sinkIndex)
|
|
96
|
+
|
|
97
|
+
for (var i = 0; i < childIndexes.length; i++) {
|
|
98
|
+
var childIndex = childIndexes[i]
|
|
99
|
+
|
|
100
|
+
if (childIndex >= length) break
|
|
101
|
+
|
|
102
|
+
var childElement = this._elements[childIndex]
|
|
103
|
+
var childScore = this._score(childElement)
|
|
104
|
+
|
|
105
|
+
if (childScore > sinkScore) {
|
|
106
|
+
if (swapScore === null || swapScore < childScore) {
|
|
107
|
+
swapIndex = childIndex
|
|
108
|
+
swapScore = childScore
|
|
109
|
+
swapElement = childElement
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (swapIndex === null) break
|
|
115
|
+
|
|
116
|
+
this._elements[swapIndex] = sinkElement
|
|
117
|
+
this._elements[sinkIndex] = swapElement
|
|
118
|
+
sinkIndex = swapIndex
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
BinaryHeap.prototype._parentIndex = function (index) {
|
|
123
|
+
return Math.floor((index - 1) / 2)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
BinaryHeap.prototype._childIndexes = function (index) {
|
|
127
|
+
return [
|
|
128
|
+
2 * index + 1,
|
|
129
|
+
2 * index + 2
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
BinaryHeap.prototype._score = function (element) {
|
|
134
|
+
return element.valueOf()
|
|
135
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
// Hacked https://github.com/felixge/node-measured
|
|
3
|
+
|
|
4
|
+
var BinaryHeap = require('./BinaryHeap')
|
|
5
|
+
var units = require('./units')
|
|
6
|
+
|
|
7
|
+
module.exports = ExponentiallyDecayingSample
|
|
8
|
+
function ExponentiallyDecayingSample (options) {
|
|
9
|
+
options = options || {}
|
|
10
|
+
|
|
11
|
+
this._elements = new BinaryHeap({
|
|
12
|
+
score: function (element) {
|
|
13
|
+
return -element.priority
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
this._rescaleInterval = options.rescaleInterval || ExponentiallyDecayingSample.RESCALE_INTERVAL
|
|
18
|
+
this._alpha = options.alpha || ExponentiallyDecayingSample.ALPHA
|
|
19
|
+
this._size = options.size || ExponentiallyDecayingSample.SIZE
|
|
20
|
+
this._random = options.random || this._random
|
|
21
|
+
this._landmark = null
|
|
22
|
+
this._nextRescale = null
|
|
23
|
+
this._mean = null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
ExponentiallyDecayingSample.RESCALE_INTERVAL = 1 * units.HOURS
|
|
27
|
+
ExponentiallyDecayingSample.ALPHA = 0.015
|
|
28
|
+
ExponentiallyDecayingSample.SIZE = 1028
|
|
29
|
+
|
|
30
|
+
ExponentiallyDecayingSample.prototype.update = function (value, timestamp) {
|
|
31
|
+
var now = Date.now()
|
|
32
|
+
if (!this._landmark) {
|
|
33
|
+
this._landmark = now
|
|
34
|
+
this._nextRescale = this._landmark + this._rescaleInterval
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
timestamp = timestamp || now
|
|
38
|
+
|
|
39
|
+
var newSize = this._elements.size() + 1
|
|
40
|
+
|
|
41
|
+
var element = {
|
|
42
|
+
priority: this._priority(timestamp - this._landmark),
|
|
43
|
+
value: value
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (newSize <= this._size) {
|
|
47
|
+
this._elements.add(element)
|
|
48
|
+
} else if (element.priority > this._elements.first().priority) {
|
|
49
|
+
this._elements.removeFirst()
|
|
50
|
+
this._elements.add(element)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (now >= this._nextRescale) this._rescale(now)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
ExponentiallyDecayingSample.prototype.toSortedArray = function () {
|
|
57
|
+
return this._elements
|
|
58
|
+
.toSortedArray()
|
|
59
|
+
.map(function (element) {
|
|
60
|
+
return element.value
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
ExponentiallyDecayingSample.prototype.toArray = function () {
|
|
65
|
+
return this._elements
|
|
66
|
+
.toArray()
|
|
67
|
+
.map(function (element) {
|
|
68
|
+
return element.value
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
ExponentiallyDecayingSample.prototype._weight = function (age) {
|
|
73
|
+
// We divide by 1000 to not run into huge numbers before reaching a
|
|
74
|
+
// rescale event.
|
|
75
|
+
return Math.exp(this._alpha * (age / 1000))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
ExponentiallyDecayingSample.prototype._priority = function (age) {
|
|
79
|
+
return this._weight(age) / this._random()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
ExponentiallyDecayingSample.prototype._random = function () {
|
|
83
|
+
return Math.random()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
ExponentiallyDecayingSample.prototype._rescale = function (now) {
|
|
87
|
+
now = now || Date.now()
|
|
88
|
+
|
|
89
|
+
var self = this
|
|
90
|
+
var oldLandmark = this._landmark
|
|
91
|
+
this._landmark = now || Date.now()
|
|
92
|
+
this._nextRescale = now + this._rescaleInterval
|
|
93
|
+
|
|
94
|
+
var factor = self._priority(-(self._landmark - oldLandmark))
|
|
95
|
+
|
|
96
|
+
this._elements
|
|
97
|
+
.toArray()
|
|
98
|
+
.forEach(function (element) {
|
|
99
|
+
element.priority *= factor
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
ExponentiallyDecayingSample.prototype.avg = function (now) {
|
|
104
|
+
var sum = 0
|
|
105
|
+
this._elements
|
|
106
|
+
.toArray()
|
|
107
|
+
.forEach(function (element) {
|
|
108
|
+
sum += element.value
|
|
109
|
+
})
|
|
110
|
+
return (sum / this._elements.size())
|
|
111
|
+
}
|