Skip to content

Node.js based forum software built for the modern web

using async in batch

Baris Usaklid469f53ebaba
diff --git a/src/batch.js b/src/batch.js
index fac78cd021..3a91775025 100644
--- a/src/batch.js
+++ b/src/batch.js
@@ -49,18 +49,20 @@ exports.processSortedSet = function (setKey, process, options, callback) {
return !done;
},
function (next) {
- db.getSortedSetRange(setKey, start, stop, function (err, ids) {
- if (err) {
- return next(err);
- }
- if (!ids.length || options.doneIf(start, stop, ids)) {
- done = true;
- return next();
- }
- process(ids, function (err) {
- if (err) {
- return next(err);
+ async.waterfall([
+ function (next) {
+ db.getSortedSetRange(setKey, start, stop, next);
+ },
+ function (ids, _next) {
+ if (!ids.length || options.doneIf(start, stop, ids)) {
+ done = true;
+ return next();
}
+ process(ids, function (err) {
+ _next(err);
+ });
+ },
+ function (next) {
start += utils.isNumber(options.alwaysStartAt) ? options.alwaysStartAt : options.batch + 1;
stop = start + options.batch;
@@ -69,8 +71,8 @@ exports.processSortedSet = function (setKey, process, options, callback) {
} else {
next();
}
- });
- });
+ },
+ ], next);
},
callback
);
@@ -106,17 +108,21 @@ exports.processArray = function (array, process, options, callback) {
done = true;
return next();
}
- process(currentBatch, function (err) {
- if (err) {
- return next(err);
- }
- start += batch;
- if (options.interval) {
- setTimeout(next, options.interval);
- } else {
- next();
- }
- });
+ async.waterfall([
+ function (next) {
+ process(currentBatch, function (err) {
+ next(err);
+ });
+ },
+ function (next) {
+ start += batch;
+ if (options.interval) {
+ setTimeout(next, options.interval);
+ } else {
+ next();
+ }
+ },
+ ], next);
},
function (err) {
callback(err);
diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js
index 5d461043fe..feeed1e20a 100644
--- a/src/database/mongo/sorted.js
+++ b/src/database/mongo/sorted.js
@@ -480,32 +480,33 @@ module.exports = function (db, module) {
return !done;
},
function (next) {
- cursor.next(function (err, item) {
- if (err) {
- return next(err);
- }
- if (item === null) {
- done = true;
- } else {
- ids.push(item.value);
- }
-
- if (ids.length < options.batch && (!done || ids.length === 0)) {
- return next(null);
- }
-
- process(ids, function (err) {
- if (err) {
- return next(err);
+ async.waterfall([
+ function (next) {
+ cursor.next(next);
+ },
+ function (item, _next) {
+ if (item === null) {
+ done = true;
+ } else {
+ ids.push(item.value);
+ }
+
+ if (ids.length < options.batch && (!done || ids.length === 0)) {
+ return next(null);
}
+ process(ids, function (err) {
+ _next(err);
+ });
+ },
+ function (next) {
ids = [];
if (options.interval) {
setTimeout(next, options.interval);
} else {
next();
}
- });
- });
+ },
+ ], next);
},
callback
);
diff --git a/src/routes/debug.js b/src/routes/debug.js
index 2ec3f23934..460534fcdc 100644
--- a/src/routes/debug.js
+++ b/src/routes/debug.js
@@ -2,77 +2,10 @@
var express = require('express');
var nconf = require('nconf');
-var winston = require('winston');
-var user = require('../user');
-var categories = require('../categories');
-var topics = require('../topics');
-var posts = require('../posts');
module.exports = function (app) {
var router = express.Router();
- router.get('/uid/:uid', function (req, res) {
- if (!req.params.uid) {
- return res.redirect('/404');
- }
-
- user.getUserData(req.params.uid, function (err, data) {
- if (err) {
- winston.error(err);
- }
-
- if (data) {
- res.send(data);
- } else {
- res.status(404).json({
- error: "User doesn't exist!",
- });
- }
- });
- });
-
- router.get('/cid/:cid', function (req, res) {
- categories.getCategoryData(req.params.cid, function (err, data) {
- if (err) {
- winston.error(err);
- }
-
- if (data) {
- res.send(data);
- } else {
- res.status(404).send("Category doesn't exist!");
- }
- });
- });
-
- router.get('/tid/:tid', function (req, res) {
- topics.getTopicData(req.params.tid, function (err, data) {
- if (err) {
- winston.error(err);
- }
-
- if (data) {
- res.send(data);
- } else {
- res.status(404).send("Topic doesn't exist!");
- }
- });
- });
-
- router.get('/pid/:pid', function (req, res) {
- posts.getPostData(req.params.pid, function (err, data) {
- if (err) {
- winston.error(err);
- }
-
- if (data) {
- res.send(data);
- } else {
- res.status(404).send("Post doesn't exist!");
- }
- });
- });
-
router.get('/test', function (req, res) {
res.redirect(404);
});