Skip to content

Commit

Permalink
Heap snapshot (#504)
Browse files Browse the repository at this point in the history
* fix(debug): Added option to generate process-specific heap snapshots
  • Loading branch information
andris9 authored Sep 28, 2023
1 parent c678a1e commit 69856eb
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

process.env.UV_THREADPOOL_SIZE = 16;

const v8 = require('node:v8');
const Path = require('path');
const os = require('os');
const config = require('wild-config');

if (process.env.NODE_CONFIG_ONLY === 'true') {
Expand All @@ -13,7 +16,6 @@ if (process.env.NODE_CONFIG_ONLY === 'true') {

const errors = require('./lib/errors');
const fs = require('fs');
const os = require('os');
const log = require('npmlog');
const packageData = require('./package.json');
const { init: initElasticSearch } = require('./lib/elasticsearch');
Expand Down Expand Up @@ -139,6 +141,48 @@ if (!processCount || processCount <= 1) {
}

process.on('unhandledRejection', err => {
log.error('App', 'Unhandled rejection: %s' + ((err && err.stack) || err));
log.error('App', 'Unhandled rejection: %s', (err && err.stack) || err);
errors.notify(err);
});

process.on('SIGPIPE', () => {
// generate memory dump
log.info('Process', 'PID=%s Generating heap snapshot...', process.pid);
let stream;

try {
stream = v8.getHeapSnapshot();
} catch (err) {
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
return;
}

if (stream) {
const path = Path.join(
os.tmpdir(),
`Heap-${process.pid}-${new Date()
.toISOString()
.substring(0, 19)
.replace(/[^0-9T]+/g, '')}.heapsnapshot`
);

let f;
try {
f = fs.createWriteStream(path);
} catch (err) {
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
return;
}

f.once('error', err => {
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
});
stream.once('error', err => {
log.error('Process', 'PID=%s Failed to generate heap snapshot: %s', process.pid, err.stack || err);
});
stream.pipe(f);
f.once('finish', () => {
log.info('Process', 'PID=%s Generated heap snapshot: %s', process.pid, path);
});
}
});

0 comments on commit 69856eb

Please sign in to comment.