Skip to content

Commit

Permalink
chore: clean old snapshot method
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed Jul 20, 2018
1 parent dc61bca commit d064750
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions lib/Daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Daemon.prototype.innerStart = function(cb) {
var profiler;

try {
profiler = semver.satisfies(process.version, '<= 10.0.0') ? require('v8-profiler-node8') : require('inspector');
profiler = semver.satisfies(process.version, '>= 10.0.0') ? require('inspector') : null;
} catch(e) {
profiler = null;
}
Expand All @@ -164,43 +164,31 @@ Daemon.prototype.innerStart = function(cb) {
*/
function snapshotPM2(msg, cb) {
if (profiler == null) {
console.log('v8-profiler is not available');
return cb(new Error('v8-profiler is not available'));
console.log('Heap snapshot is not available (node 10+)');
return cb(new Error('Heap snapshot is not available (node 10+)'));
}

if (semver.satisfies(process.version, '>= 10.0.0')) {
const session = new profiler.Session()
session.connect()
session.post('HeapProfiler.enable')

const chunks = []
session.on('HeapProfiler.addHeapSnapshotChunk', (data) => {
chunks.push(data.params.chunk)
})

session.post('HeapProfiler.takeHeapSnapshot', {
reportProgress: false
}, (err, data) => {
if (err) return cb(err)

fs.writeFile(msg.pwd, chunks.join(''), function() {
session.post('Profiler.disable')
session.disconnect()

return cb(null, {file : msg.pwd});
});
})

} else {
var snapshot1 = profiler.takeSnapshot();
var path = require('path');
snapshot1.export(function(error, result) {
fs.writeFile(msg.pwd, result, function() {
snapshot1.delete();
return cb(null, {file : msg.pwd});
});
const session = new profiler.Session()
session.connect()
session.post('HeapProfiler.enable')

const chunks = []
session.on('HeapProfiler.addHeapSnapshotChunk', (data) => {
chunks.push(data.params.chunk)
})

session.post('HeapProfiler.takeHeapSnapshot', {
reportProgress: false
}, (err, data) => {
if (err) return cb(err)

fs.writeFile(msg.pwd, chunks.join(''), function() {
session.post('Profiler.disable')
session.disconnect()

return cb(null, {file : msg.pwd});
});
}
})
}

function startProfilingPM2(msg, cb) {
Expand Down

0 comments on commit d064750

Please sign in to comment.