Skip to content

Commit

Permalink
fix(perf): Close MongoDB cursors after calling forEach2() (#405)
Browse files Browse the repository at this point in the history
* fix(perf): Close cursor after calls to mongodb.forEach2()

* fix

* 🧹 /api/track

* fix "TypeError: Cannot read property 's' of undefined"

* recognize errors

* models/notif.js: also jump over errors

* close cursor and skip errors in snapshotTrackScores()

* same in refreshTrackCollection()

* 🧹 updateAndPopulateMetadata() and populateTrackMetadata() + callers
  • Loading branch information
adrienjoly committed Nov 22, 2020
1 parent f9a78d0 commit 3f1ec06
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 321 deletions.
6 changes: 0 additions & 6 deletions app/controllers/admin/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ function listMissingUsers(uids, cb) {
}

var fileGenerators = {
populateTrackMetadata: function (p, cb) {
trackModel.populateTrackMetadata(function (r) {
console.log('populateTrackMetadata => ', r || { ok: 'done' });
});
cb('populating track metadata...');
},
refreshTrackCollection: function (p, cb) {
trackModel.refreshTrackCollection(function (r) {
console.log('refreshTrackCollection => ', r || { ok: 'done' });
Expand Down
156 changes: 0 additions & 156 deletions app/controllers/api/track.js

This file was deleted.

39 changes: 0 additions & 39 deletions app/models/deezer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,5 @@ exports.fetchTrackMetadata = function (_trackId, cb, raw) {
);
};

/*
var METADATA_MAPPING = {
"art": "artistName",
"tit": "trackTitle"
};
exports.getDeezerId = function(p, cb){
console.log("getting deezer track id", p.eId);
function returnMetaOr(orCb){
return function(tr) {
var meta = tr.meta || {};
if (meta.dz) {
console.log("found dz meta");
cb(tr);
}
else if (meta.art && meta.tit) {
var q = snip.filterFields(meta, METADATA_MAPPING);
console.log("querying dz...", q);
deezerApi.searchTracks(q, function(dzRes){
console.log("dz response => dzRes");
tr.dzRes = dzRes;
cb(tr);
});
}
else if (orCb)
orCb(tr);
else
cb(tr);
};
}
trackModel.fetchTrackByEid(p.eId, returnMetaOr(function(tr){
console.log("missing metadata => update metadata...");
trackModel.updateAndPopulateMetadata(p.eId, returnMetaOr(), true);
}));
},
*/
// searchTracks({trackTitle:"the monster", artistName:"eminem"}, console.log); // =>
// fetchTrackInfo(17782324, console.log);

exports.searchTracks = searchTracks;
exports.fetchTrackInfo = fetchTrackInfo;
7 changes: 5 additions & 2 deletions app/models/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ exports.forEach2 = function (colName, params, handler) {
cursor.next(function (err, item) {
if (err) {
console.error('mongodb.forEach2 ERROR', err);
handler({ error: err });
} else handler(item, item ? next : undefined);
handler({ error: err }, undefined, cursor.close.bind(cursor));
cursor.close();
} else {
handler(item, item ? next : undefined, cursor.close.bind(cursor));
}
});
})();
});
Expand Down
10 changes: 7 additions & 3 deletions app/models/notif.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,13 @@ exports.post = function (post) {
projection: { uId: true },
};
mongodb.forEach2('post', query, function (sameTrack, next) {
var author = sameTrack && mongodb.usernames[sameTrack.uId];
if (author) notifEmails.sendPostedSameTrack(author, next);
else if (next) next();
var author =
sameTrack && !sameTrack.error && mongodb.usernames[sameTrack.uId];
if (author) {
notifEmails.sendPostedSameTrack(author, next);
} else if (next) {
next();
}
});
};

Expand Down
33 changes: 9 additions & 24 deletions app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,17 @@ exports.fetchByAuthors = function (uidList, options, cb) {
var limit = params.limit;
params.limit = undefined; // prevent forEach2 from limiting cursor
params.q = query;
mongodb.forEach2('post', params, function (post, next) {
if (post && !uidSet[(post.repost || {}).uId]) posts.push(post);
mongodb.forEach2('post', params, function (post, next, closeCursor) {
if (post && !post.error && !uidSet[(post.repost || {}).uId]) {
posts.push(post);
}
if (!post || !next || posts.length == limit) {
//console.timeEnd("post.fetchByAuthors2...");
cb(processPosts(posts)); // TODO: close cursor
} else next();
});
};

var MAX_OTHER_AUTHORS = 3;

exports.fetchByOtherAuthors = function (uidList, options, cb) {
//console.time("post.fetchByOtherAuthors...");
var posts = [],
uidSet = snip.arrayToSet(uidList);
mongodb.forEach2('post', { q: {}, sort: [['_id', 'desc']] }, function (
post,
next
) {
if (post && !uidSet[post.uId]) posts.push(post);
if (!post || !next || posts.length == MAX_OTHER_AUTHORS) {
//console.timeEnd("post.fetchByOtherAuthors...");
cb(processPosts(posts)); // TODO: close cursor
} else next();
cb(processPosts(posts));
closeCursor();
} else {
next();
}
});
};

Expand Down Expand Up @@ -279,7 +266,6 @@ exports.savePost = function (postObj, handler) {
if (result) {
if (Array.isArray(result)) result = result[0];
searchModel.indexTyped('post', result);
//trackModel.updateAndPopulateMetadata(result.eId); // Notice: Cross platform player metadata extraction was shut down on 7/1/2015
result.isNew = !pId;
if (result.isNew) notif.post(result);
/*
Expand Down Expand Up @@ -361,7 +347,6 @@ exports.rePost = function (pId, repostObj, handler) {
//searchModel.indexPost(result);
result = result[0];
searchModel.indexTyped('post', result);
//trackModel.updateAndPopulateMetadata(result.eId); // Notice: Cross platform player metadata extraction was shut down on 7/1/2015
/*
recomModel.matchingEngine.addPost(result, function() {
console.log("=> added post to matching engine index");
Expand Down
Loading

0 comments on commit 3f1ec06

Please sign in to comment.