Added HeapDump support.
Ylian Saint-Hilaire committed
Mar 28, 2020 at 17:28 UTC
a61acc48906b7aa9b8d3b2841edb05db8107e8f3
2 files changed
+19
meshcentral.js
+4
@@ -2459,6 +2459,10 @@ function mainStart() {
2459
if (allsspi == false) { modules.push('otplib@10.2.3'); } // Google Authenticator support (v10 supports older NodeJS versions).
2460
}
2461
2462
+ // Setup heapdump support if needed, useful for memory leak debugging
2463
+ // https://www.arbazsiddiqui.me/a-practical-guide-to-memory-leaks-in-nodejs/
2464
+ if (config.settings.heapdump === true) { modules.push('heapdump'); }
2465
+
2466
// Install any missing modules and launch the server
2467
InstallModules(modules, function () { meshserver = CreateMeshCentralServer(config, args); meshserver.Start(); });
2468
meshuser.js
+15
@@ -690,6 +690,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
690
switch (cmd) {
691
case 'help': {
692
var fin = '', f = '', availcommands = 'help,info,versions,args,resetserver,showconfig,usersessions,closeusersessions,tasklimiter,setmaxtasks,cores,migrationagents,agentstats,webstats,mpsstats,swarmstats,acceleratorsstats,updatecheck,serverupdate,nodeconfig,heapdump,relays,autobackup,backupconfig,dupagents,dispatchtable,badlogins,showpaths,le,lecheck,leevents,dbstats';
693
+ if (parent.parent.config.settings.heapdump === true) { availcommands += ',heapdump'; }
694
availcommands = availcommands.split(',').sort();
695
while (availcommands.length > 0) {
696
if (f.length > 80) { fin += (f + ',\r\n'); f = ''; }
@@ -699,6 +700,20 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
700
r = 'Available commands: \r\n' + fin + '.';
701
break;
702
}
703
+ case 'heapdump': {
704
+ // Heapdump support, see example at:
705
+ // https://www.arbazsiddiqui.me/a-practical-guide-to-memory-leaks-in-nodejs/
706
+ if (parent.parent.config.settings.heapdump === true) {
707
+ var dumpFileName = parent.path.join(parent.parent.datapath, `heapDump-${Date.now()}.heapsnapshot`);
708
+ try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Generating dump file at: " + dumpFileName, tag: command.tag })); } catch (ex) { }
709
+ require('heapdump').writeSnapshot(dumpFileName, (err, filename) => {
710
+ try { ws.send(JSON.stringify({ action: 'serverconsole', value: "Done.", tag: command.tag })); } catch (ex) { }
711
+ });
712
+ } else {
713
+ r = "Heapdump not supported, add \"heapdump\":true to settings section of config.json.";
714
+ }
715
+ break;
716
+ }
717
case 'le': {
718
if (parent.parent.letsencrypt == null) {
719
r = "Let's Encrypt not in use.";