1

I am creating a chess engine using nodejs in lambda but due to asynchronous call it showing timeout error on lambda every time. This is just partial part of the function. It is working fine on local nodejs console but not on the lambda. Please someone suggest something as I am new to this.

var chessjs = require('./chess');
var engine = require('uci');
var uciengine = new engine(process.env['LAMBDA_TASK_ROOT'] + '/stockfish');
var fs = require("fs");
var match;

function moveEngine() {
var curfen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
var depth = '20';

uciengine.runProcess().then(
        () => {
    console.log("Started.");
    return uciengine.uciCommand();
}
).then(
        () => {
    console.log("Is Ready?");
    return uciengine.isReadyCommand();
}
).then(
        () => {
    console.log("New game.");
    return uciengine.uciNewGameCommand();
}
).then(
        () => {
    console.log("Setting position.");
    return uciengine.positionCommand(curfen);
}
).then(
        () => {
    console.log('Starting position set');
    console.log('Starting analysis');
    return uciengine.depthLimitedGoCommand(depth, (info) => {
    });
}
).then((bestmove) => {
    console.log('Bestmove: ');
    console.log(bestmove);
    return uciengine.quitCommand();
}).then(() => {
    console.log('Stopped');
   response.sessionAttributes = {};
   context.succeed(response);
}).done();
}

  async call code
var chessjs = require('./chess');
var engine = require('uci');
var async= require('async');
var uciengine = new engine('/var/task/stockfish');
var fs = require("fs");
var match;

function moveEngine() {
   var curfen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
   var depth = '20';

async.auto({
runProcess: function(next, results) {
    uciengine.runProcess(next,results);
},
checkUiEngineReady:['runProcess',function(next,results) {
  uciengine.checkUiEngineReady(next,results);
}],
newGameCommand:['checkUiEngineReady',function(next,results) {
  uciengine.newGameCommand(next,results);
}],
position:['newGameCommand',function(next,results) {
  uciengine.positionCommand(curfen,next,results);
}],
godepth:['position',function(next,results) {
  uciengine.depthLimitedGoCommand(depth,next,results);
}]
}, function(err, response) {
if (err) {
    next(err);
} else {
    console.log(response);
    uciengine.quitCommand();
    context.succeed(response);
}
});
}
moveEngine();

async call is giving the same error like before and i think it is probably wrong.

2
  • Do you need to perhaps call engine.done() to close something persisting on the event loop? Or, perhaps you should set context.callbackWaitsForEmptyEventLoop = false (but avoid that if possible unless you understand its implications)? Commented Mar 22, 2018 at 14:44
  • tried that but ain't working. Is my syntax for the async function is correct? It is confusing. Thank you for the response. Commented Mar 23, 2018 at 5:57

1 Answer 1

2

You can handle the Async call in Lambda using async npm module which is a utility module to handle asynchronous programming in Nodejs.

You can install the async module with npm install --save async.

async.auto function will be useful to manage the above call.

Here is an example through which you can manage your code.

async.auto({
    runProcess: function(next, results) {
        runProcess(next,results);
    },
    checkUiEngineReady:['runProcess',function(next,results) {
      checkUiEngineReady(next,results);
    }],
    newGameCommand:['checkUiEngineReady',function(next,results) {
      newGameCommand(next,results);
    }]
}, function(err, response) {
    if (err) {
        next(err);
    } else {
        context.succeed(response);
    }
});

Thanks

Sign up to request clarification or add additional context in comments.

17 Comments

i already installed async in my modules but first time when i used it i just included var async = require('async'); in my code and it started working but now it ain't working don't know what's wrong now. is my path for the chess engine is correct??
Can you show me the error, and also have you set up the timeout time in your Lambda console ?
Response: { "errorMessage": "2018-03-22T10:41:08.435Z d48b4ae1-2dbc-11e8-9822-f7bc88c773ee Task timed out after 300.05 seconds" } Request ID: "d48b4ae1-2dbc-11e8-9822-f7bc88c773ee" Function Logs: START RequestId: d48b4ae1-2dbc-11e8-9822-f7bc88c773ee Version: $LATEST END RequestId: d48b4ae1-2dbc-11e8-9822-f7bc88c773ee REPORT RequestId: d48b4ae1-2dbc-11e8-9822-f7bc88c773ee Duration: 300047.20 ms Billed Duration: 300000 ms Memory Size: 128 MB Max Memory Used: 36 MB 2018-03-22T10:41:08.435Z d48b4ae1-2dbc-11e8-9822-f7bc88c773ee Task timed out after 300.05 seconds
From above logs, it seems that your code execution is taking more than 300 seconds (i.e 5 minutes). Are you using any long running task which is taking more than 5 minutes time and also can you increase your lambda function memory size from AWS lambda console ?
no, actually it task take 10 sec at max when i run it locally
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.