async eachSeries + setTimeout fixes

This commit is contained in:
Akinwale Ariwodola 2017-09-25 17:38:34 +01:00
parent 4eef0897ce
commit ce50f0a27a

29
app.js
View file

@ -150,6 +150,7 @@ const createOrGetUserId = (username, callback) => {
};
const processCompletedDeposits = (callback) => {
const delay = 2000;
async.waterfall([
(cb) => {
db.query('SELECT C.DepositId, D.Amount, U.Username, U.Balance FROM CompletedDepositConfirmations C JOIN Deposits D ON D.Id = C.DepositId JOIN Users U ON U.Id = C.UserId', cb);
@ -157,24 +158,22 @@ const processCompletedDeposits = (callback) => {
(res, fields, cb) => {
if (res.length > 0) {
return async.eachSeries(res, (completedDeposit, ecb) => {
setTimeout(() => {
sendPMUsingTemplate('ondeposit.completed', { how_to_use_url: config.howToUseUrl, amount: completedDeposit.Amount, balance: completedDeposit.Balance },
'Deposit completed!', completedDeposit.Username, (err) => {
if (err) {
return ecb(err, null);
sendPMUsingTemplate('ondeposit.completed', { how_to_use_url: config.howToUseUrl, amount: completedDeposit.Amount, balance: completedDeposit.Balance },
'Deposit completed!', completedDeposit.Username, (err) => {
if (err) {
return setTimeout(ecb, delay, err);
}
// remove the entry from the DB
return db.query('DELETE FROM CompletedDepositConfirmations WHERE DepositId = ?', [completedDeposit.DepositId], (ierr) => {
if (ierr) {
return setTimeout(ecb, delay, ierr);
}
// remove the entry from the DB
return db.query('DELETE FROM CompletedDepositConfirmations WHERE DepositId = ?', [completedDeposit.DepositId], (ierr) => {
if (ierr) {
return ecb(ierr, null);
}
// success
return ecb(null, true);
});
// success
return setTimeout(ecb, delay, null, true);
});
}, 2000); // Wait 2 seconds between each request (if there are multiple to send)
});
// TODO: Implement inserting messages into a pending message queue instead
}, cb);
}