Skip to content

Commit

Permalink
remove wrap async and use promise
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoventurini committed Nov 17, 2023
1 parent 93146d0 commit 52e9ba6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages
.idea
.envrc
49 changes: 27 additions & 22 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ Meteor.methods({
"monti.profileCpu": profileMethodHandler,
});

function profileMethodHandler (arg1, arg2, type) {
async function profileMethodHandler (arg1, arg2, type) {
check(arguments, [Match.Any]);

this.unblock();

if (type === 'remote') {
Expand Down Expand Up @@ -53,7 +54,7 @@ remoteProfileCPU = function(timeToProfileSecs, id) {
}
};

localProfileCPU = function(timeToProfileSecs, outputLocation) {
localProfileCPU = async function(timeToProfileSecs, outputLocation) {
if(!process.env.KADIRA_PROFILE_LOCALLY && !process.env.MONTI_PROFILE_LOCALLY) {
throw new Meteor.Error(403, "run your app with `MONTI_PROFILE_LOCALLY` env variable to profile locally.")
}
Expand All @@ -63,42 +64,46 @@ localProfileCPU = function(timeToProfileSecs, outputLocation) {
outputLocation = path.resolve(os.tmpdir(), name + '.cpuprofile');
}
console.log('Monti APM: Started CPU profiling for %s secs.', timeToProfileSecs);
var profile = getCpuProfile(name, timeToProfileSecs);
var profile = await getCpuProfile(name, timeToProfileSecs);

console.log('Monti APM: Saving CPU profile to: ' + outputLocation);
writeToDisk(outputLocation, profile);
await fs.promises.writeFile(outputLocation, profile);
console.log('Monti APM: CPU profile saved.');

return "Cpu profile has been saved to: " + outputLocation;
};

getCpuProfile = Kadira._wrapAsync(function(name, timeToProfileSecs, callback) {
var v8Profiler = binaryRequire('v8-profiler-next');
v8Profiler.startProfiling(name);
setTimeout(function() {
var profile = v8Profiler.stopProfiling(name);
profile.export((err, result) => {
if (err) {
callback(err);
}

profile.delete();
callback(null, result);
});
}, timeToProfileSecs * 1000);
});
getCpuProfile = function(name, timeToProfileSecs) {
return new Promise((resolve, reject) => {
var v8Profiler = binaryRequire('v8-profiler-next');
v8Profiler.startProfiling(name);
setTimeout(function() {
var profile = v8Profiler.stopProfiling(name);
profile.export((err, result) => {
if (err) {
reject(err);
return
}

profile.delete();
resolve(result);
});
}, timeToProfileSecs * 1000);
})
};

writeToDisk = Kadira._wrapAsync(fs.writeFile);
writeToDisk = fs.promises.writeFile

function uploadProfile (profile, url) {
async function uploadProfile (profile, url) {
console.trace();
return Kadira.coreApi._send(url, {
data: profile,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(profile)
},
method: 'PUT',
}).await();
})
}

function binaryRequire(moduleName) {
Expand Down
6 changes: 3 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ Package.onTest(function(api) {
});

function configurePackage(api) {
api.versionsFrom('METEOR@1.4');
api.versionsFrom('METEOR@3.0-alpha.17');
api.use('check');
api.use('random');
api.export('MontiProfiler');
api.use('montiapm:agent@2.44.2');
api.imply('montiapm:agent@2.44.2');
api.use('montiapm:agent');
api.imply('montiapm:agent');
api.use('montiapm:[email protected]');

api.addFiles('lib/server.js', 'server');
Expand Down

0 comments on commit 52e9ba6

Please sign in to comment.