eclipsesource / eclipsesource/J2V8

NodeJS.handleMessage() hangs for some JS functions

Open
#349 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.6k
Forks
387
PR merge metrics
No merged PRs in 30d

Description

[UserManagement.zip](https://github.com/eclipsesource/J2V8/files/1321082/UserManagement.zip)
Hello -
(Complete JS and java attached...)
Using:
j2v8 4.6
JDK 1.8

I'm finding that running a simple JS function using NodeJS works fine. The java looks like this:

public void testGetUser4() throws IOException, ScriptException {
String scriptFilename = "UserManagement.js";
String script = loadFile(scriptFilename);
NodeJS nodeJS = NodeJS.createNodeJS();
V8Object exports = nodeJS.require(new File(scriptFilename));
JavaCallback callback = new JavaCallback() {
// @Override
public Object invoke(final V8Object receiver, final V8Array parameters) {
System.out.println("Callback invoked.");
return null;
}
};
V8Function function = new V8Function(nodeJS.getRuntime(), callback);

V8Object event = new V8Object(nodeJS.getRuntime());
event = event.add("user_id", "1755ac52-94c7-11e7-b9f8-0adfa8abb906");
exports.executeJSFunction("_get_user2", event, function);

runMessageLoop(nodeJS);
event.release();
function.release();
exports.release();
nodeJS.release();

System.out.println("Done.");
}

private void runMessageLoop(NodeJS nodeJS) {
int cnt = 0;
while (nodeJS.isRunning()) {
boolean res = nodeJS.handleMessage();
System.out.println("Result for message " + cnt++ + " is " + res);
}
}

The simple JS function is:

exports._get_user2 = function(event, callback) {
console.log('Event: ', JSON.stringify(event, null, '\t'));
var userId = event.user_id;
console.log('Searching for user with id: ', userId);
callback(null, userId);
console.log('JS function complete.');
};

The test completes successfully with this output:

Event: {
"user_id": "1755ac52-94c7-11e7-b9f8-0adfa8abb906"
}
Searching for user with id: 1755ac52-94c7-11e7-b9f8-0adfa8abb906
Callback invoked.
JS function complete.
Result for message 0 is true
Result for message 1 is false
Done.

But when I enhance this JS function with a call to mysql, the test doesn't complete. The function is:

exports._get_user = function(event, callback) {
console.log('Event: ', JSON.stringify(event, null, '\t'));
var userId = event.user_id;
console.log('Searching for user with id: ', userId);
var sql = "call usermanagement.get_user(?)";
pool.query(sql, userId, function(error, results, fields) {
if (error) {
console.log("JS function failed", error);
callback(error);
} else {
var res = '{}';
if (results[0].length != 0) {
res = results[0][0];
}

console.log(res);
callback(null, res);
}
});

console.log('JS function complete.');
};

Now the test seems to run all of the code, but never returns from the 'runMessageLoop()' method:

Event: {
"user_id": "1755ac52-94c7-11e7-b9f8-0adfa8abb906"
}
Searching for user with id: 1755ac52-94c7-11e7-b9f8-0adfa8abb906
JS function complete.
Result for message 0 is true
Result for message 1 is true
Result for message 2 is true
Result for message 3 is true
Result for message 4 is true
Result for message 5 is true
Result for message 6 is true
Result for message 7 is true
{}
Callback invoked.
Result for message 8 is true
Result for message 9 is true
Result for message 10 is true
Result for message 11 is true
(no more output and the test doesn't complete.)

Amazon seems to have hit a similar issue, and some are using 'context.callbackWaitsForEmptyEventLoop = false' in lambdas to get around it. But Amazon indicates that this should be used only as a last resort.
Am I doing something wrong?

Thank you -

Brian.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.