deasync
deasync copied to clipboard
Not Working on production while locally on dev it works fine. - VERY STRANGE
Hi Abbr,
i have the following code which run with Node.js (v4.2.2)+ Express.js(v~4.13.1).
var request = require('request');
var deasync = require('deasync');
request = request.defaults({jar: true});
function doSomething() {
var done = false;
var data;
request("https://www.google.co.il/search?gws_rd=ssl&site=&source=hp&q=test", function (err, res, body) {
if(err) {console.log(err);return;}
data = "Test";
done=true;
});
deasync.loopWhile(function () {
return !done;
});
return data;
}
exports.doSomething = doSomething;
On my local machine (Macbook pro V10.11.1) the code works as expected no matter where and when i call the above function.
However, on a Ubuntu machine, the code runs if i call it directly from the router.get()
function, but if i call it from a nested callback environment, it will never stop looping. actually, the request()
is starting but never starting its callback.
Examples:
here is the file router/index.js: which executed as expected all the time
var express = require('express');
var router = express.Router();
var request = require('request');
request = request.defaults({jar: true});
var deasync = require('deasync');
var doSomething = require(PATH_TO_DOSOMETHING);
router.get('/', function(req, res, next) {
var text = doSomething.doSomething();
res.send(text);
});
It will be hard to write all the code that doesn't work, but I'll try to give you an idea about when your code is called and the same doSomething()
is stuck.
This is another router....
router.get('/', function(req, res, next) {
Indicative.validate(rules, paramsObj) // Indicative is a validation library
.then(function (success) {
//we passed the validation. start processing the request
Promise.join(//Bluebird function
Users.find().execAsync(),//mongoose call after it got promisification by bluebird
Cars.find().execAsync(),
function(users,cars){
bl.callStateMachine();//bl is a statemachine from the project stately.js
//AND doSomething() is called from within the statemachine.
}
);
});
});
I know its a big mess, but maybe you have a clue where to start?
I managed to isolate the problem. It is happening because of the Indiactive
promise. The following router will get stuck, because deasync is used inside Indiactive's promise result.
- AGAIN, this is happening only on the ubuntu server, and NOT locally.
router.get('/:phone', function(req, res, next) {
var paramsObj = {
phone: req.params.phone
};
var rules = {
phone: 'required|string'
};
Indicative.validate(rules, paramsObj)
.then(function (success) {
var done = false;
request(config.google.uri + config.google.queryStr + encodeURIComponent("plastiv"), function (err, response, body) {
done=true;
});
deasync.loopWhile(function () {
return !done;
});
}).catch(function (err) {
console.log(err);
next(err);
}).done();
res.send("");
});
module.exports = router;
If the request to google.com will be made outside of Indiactive's promise it will work as expected.