I have a timer that works in debug mode but not when deployed. I bought the 5-20 second limit expansion pack. It should be working but I don’t know what’s wrong. Here’s my code:
Backendless.enablePromises();
Backendless.ServerCode.addTimer({
name: ‘blue’,
startDate: 1470093960000,
frequency: {
schedule: ‘once’
},
execute(req){
function Game1(args) {
args = args || {};
this.aggVotes = args.aggVotes || “”;
this.pVote1 = args.pVote1 || “”;
this.pVoteResult1 = args.pVoteResult1 || “”;
this.player = args.player || “”;
this.isPres1 = args.isPres1 || “”;
this.reVote = args.reVote || “”;
this.isElim = args.isElim || “”;
this.voted1 = args.voted1 || “”;
}
var query = new Backendless.DataQuery();
query.options = {
pageSize: 11
};
return Backendless.Persistence.of(Game1).find(query).then(result => {
const games = result.data;
for (i = 0; i < games.length; i++) {
if (games[0].player == games.pVote1)
{
games[0].pVoteResult1 += 1;
}
if (games[1].player == games.pVote1)
{
games[1].pVoteResult1 += 1;
}
if (games[2].player == games.pVote1)
{
games[2].pVoteResult1 += 1;
}
if (games[3].player == games.pVote1)
{
games[3].pVoteResult1 += 1;
}
if (games[4].player == games.pVote1)
{
games[4].pVoteResult1 += 1;
}
if (games[5].player == games.pVote1)
{
games[5].pVoteResult1 += 1;
}
if (games[6].player == games.pVote1)
{
games[6].pVoteResult1 += 1;
}
if (games[7].player == games.pVote1)
{
games[7].pVoteResult1 += 1;
}
if (games[8].player == games.pVote1)
{
games[8].pVoteResult1 += 1;
}
if (games[9].player == games.pVote1)
{
games[9].pVoteResult1 += 1;
}
if (games[10].player == games.pVote1)
{
games[10].pVoteResult1 += 1;
}
if (games[0].pVote1 == games.player)
{
games[0].voted1 += 1;
}
if (games[1].pVote1 == games.player)
{
games[1].voted1 += 1;
}
if (games[2].pVote1 == games.player)
{
games[2].voted1 += 1;
}
if (games[3].pVote1 == games.player)
{
games[3].voted1 += 1;
}
if (games[4].pVote1 == games.player)
{
games[4].voted1 += 1;
}
if (games[5].pVote1 == games.player)
{
games[5].voted1 += 1;
}
if (games[6].pVote1 == games.player)
{
games[6].voted1 += 1;
}
if (games[7].pVote1 == games.player)
{
games[7].voted1 += 1;
}
if (games[8].pVote1 == games.player)
{
games[8].voted1 += 1;
}
if (games[9].pVote1 == games.player)
{
games[9].voted1 += 1;
}
if (games[10].pVote1 == games.player)
{
games[10].voted1 += 1;
}
}
function check0()
{
if (games[0].pVoteResult1 > games[1].pVoteResult1 &&
games[0].pVoteResult1 > games[2].pVoteResult1 &&
games[0].pVoteResult1 > games[3].pVoteResult1 &&
games[0].pVoteResult1 > games[4].pVoteResult1 &&
games[0].pVoteResult1 > games[5].pVoteResult1 &&
games[0].pVoteResult1 > games[6].pVoteResult1 &&
games[0].pVoteResult1 > games[7].pVoteResult1 &&
games[0].pVoteResult1 > games[8].pVoteResult1 &&
games[0].pVoteResult1 > games[9].pVoteResult1 &&
games[0].pVoteResult1 > games[10].pVoteResult1
)
{
games[0].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[0]);
}
function check1()
{
if (games[1].pVoteResult1 > games[0].pVoteResult1 &&
games[1].pVoteResult1 > games[2].pVoteResult1 &&
games[1].pVoteResult1 > games[3].pVoteResult1 &&
games[1].pVoteResult1 > games[4].pVoteResult1 &&
games[1].pVoteResult1 > games[5].pVoteResult1 &&
games[1].pVoteResult1 > games[6].pVoteResult1 &&
games[1].pVoteResult1 > games[7].pVoteResult1 &&
games[1].pVoteResult1 > games[8].pVoteResult1 &&
games[1].pVoteResult1 > games[9].pVoteResult1 &&
games[1].pVoteResult1 > games[10].pVoteResult1
)
{
games[1].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[1]);
}
function check2()
{
if (games[2].pVoteResult1 > games[0].pVoteResult1 &&
games[2].pVoteResult1 > games[1].pVoteResult1 &&
games[2].pVoteResult1 > games[3].pVoteResult1 &&
games[2].pVoteResult1 > games[4].pVoteResult1 &&
games[2].pVoteResult1 > games[5].pVoteResult1 &&
games[2].pVoteResult1 > games[6].pVoteResult1 &&
games[2].pVoteResult1 > games[7].pVoteResult1 &&
games[2].pVoteResult1 > games[8].pVoteResult1 &&
games[2].pVoteResult1 > games[9].pVoteResult1 &&
games[2].pVoteResult1 > games[10].pVoteResult1
)
{
games[2].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[2]);
}
function check3()
{
if (games[3].pVoteResult1 > games[0].pVoteResult1 &&
games[3].pVoteResult1 > games[1].pVoteResult1 &&
games[3].pVoteResult1 > games[2].pVoteResult1 &&
games[3].pVoteResult1 > games[4].pVoteResult1 &&
games[3].pVoteResult1 > games[5].pVoteResult1 &&
games[3].pVoteResult1 > games[6].pVoteResult1 &&
games[3].pVoteResult1 > games[7].pVoteResult1 &&
games[3].pVoteResult1 > games[8].pVoteResult1 &&
games[3].pVoteResult1 > games[9].pVoteResult1 &&
games[3].pVoteResult1 > games[10].pVoteResult1
)
{
games[3].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[3]);
}
function check4()
{
if (games[4].pVoteResult1 > games[0].pVoteResult1 &&
games[4].pVoteResult1 > games[1].pVoteResult1 &&
games[4].pVoteResult1 > games[2].pVoteResult1 &&
games[4].pVoteResult1 > games[3].pVoteResult1 &&
games[4].pVoteResult1 > games[5].pVoteResult1 &&
games[4].pVoteResult1 > games[6].pVoteResult1 &&
games[4].pVoteResult1 > games[7].pVoteResult1 &&
games[4].pVoteResult1 > games[8].pVoteResult1 &&
games[4].pVoteResult1 > games[9].pVoteResult1 &&
games[4].pVoteResult1 > games[10].pVoteResult1
)
{
games[4].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[4]);
}
function check5()
{
if (games[5].pVoteResult1 > games[0].pVoteResult1 &&
games[5].pVoteResult1 > games[1].pVoteResult1 &&
games[5].pVoteResult1 > games[2].pVoteResult1 &&
games[5].pVoteResult1 > games[3].pVoteResult1 &&
games[5].pVoteResult1 > games[4].pVoteResult1 &&
games[5].pVoteResult1 > games[6].pVoteResult1 &&
games[5].pVoteResult1 > games[7].pVoteResult1 &&
games[5].pVoteResult1 > games[8].pVoteResult1 &&
games[5].pVoteResult1 > games[9].pVoteResult1 &&
games[5].pVoteResult1 > games[10].pVoteResult1
)
{
games[5].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[5]);
}
function check6()
{
if (games[6].pVoteResult1 > games[0].pVoteResult1 &&
games[6].pVoteResult1 > games[1].pVoteResult1 &&
games[6].pVoteResult1 > games[2].pVoteResult1 &&
games[6].pVoteResult1 > games[3].pVoteResult1 &&
games[6].pVoteResult1 > games[4].pVoteResult1 &&
games[6].pVoteResult1 > games[5].pVoteResult1 &&
games[6].pVoteResult1 > games[7].pVoteResult1 &&
games[6].pVoteResult1 > games[8].pVoteResult1 &&
games[6].pVoteResult1 > games[9].pVoteResult1 &&
games[6].pVoteResult1 > games[10].pVoteResult1
)
{
games[6].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[6]);
}
function check7()
{
if (games[7].pVoteResult1 > games[0].pVoteResult1 &&
games[7].pVoteResult1 > games[1].pVoteResult1 &&
games[7].pVoteResult1 > games[2].pVoteResult1 &&
games[7].pVoteResult1 > games[3].pVoteResult1 &&
games[7].pVoteResult1 > games[4].pVoteResult1 &&
games[7].pVoteResult1 > games[5].pVoteResult1 &&
games[7].pVoteResult1 > games[6].pVoteResult1 &&
games[7].pVoteResult1 > games[8].pVoteResult1 &&
games[7].pVoteResult1 > games[9].pVoteResult1 &&
games[7].pVoteResult1 > games[10].pVoteResult1
)
{
games[7].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[7]);
}
function check8()
{
if (games[8].pVoteResult1 > games[0].pVoteResult1 &&
games[8].pVoteResult1 > games[1].pVoteResult1 &&
games[8].pVoteResult1 > games[2].pVoteResult1 &&
games[8].pVoteResult1 > games[3].pVoteResult1 &&
games[8].pVoteResult1 > games[4].pVoteResult1 &&
games[8].pVoteResult1 > games[5].pVoteResult1 &&
games[8].pVoteResult1 > games[6].pVoteResult1 &&
games[8].pVoteResult1 > games[7].pVoteResult1 &&
games[8].pVoteResult1 > games[9].pVoteResult1 &&
games[8].pVoteResult1 > games[10].pVoteResult1
)
{
games[8].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[8]);
}
function check9()
{
if (games[9].pVoteResult1 > games[0].pVoteResult1 &&
games[9].pVoteResult1 > games[1].pVoteResult1 &&
games[9].pVoteResult1 > games[2].pVoteResult1 &&
games[9].pVoteResult1 > games[3].pVoteResult1 &&
games[9].pVoteResult1 > games[4].pVoteResult1 &&
games[9].pVoteResult1 > games[5].pVoteResult1 &&
games[9].pVoteResult1 > games[6].pVoteResult1 &&
games[9].pVoteResult1 > games[7].pVoteResult1 &&
games[9].pVoteResult1 > games[8].pVoteResult1 &&
games[9].pVoteResult1 > games[10].pVoteResult1
)
{
games[9].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[9]);
}
function check10()
{
if (games[10].pVoteResult1 > games[0].pVoteResult1 &&
games[10].pVoteResult1 > games[1].pVoteResult1 &&
games[10].pVoteResult1 > games[2].pVoteResult1 &&
games[10].pVoteResult1 > games[3].pVoteResult1 &&
games[10].pVoteResult1 > games[4].pVoteResult1 &&
games[10].pVoteResult1 > games[5].pVoteResult1 &&
games[10].pVoteResult1 > games[6].pVoteResult1 &&
games[10].pVoteResult1 > games[7].pVoteResult1 &&
games[10].pVoteResult1 > games[8].pVoteResult1 &&
games[10].pVoteResult1 > games[9].pVoteResult1
)
{
games[10].isPres1 = true
}
return Backendless.Persistence.of(Game1).save(games[10]);
}
check0();
check1();
check2();
check3();
check4();
check5();
check6();
check7();
check8();
check9();
check10();
});
}
});
When I manually run this in debug mode it works and my terminal says 16:16:45.132 - [248D160F-C003-64CD-FFFC-9BA1DFD95B00] New task arrived!
16:16:45.976 - [248D160F-C003-64CD-FFFC-9BA1DFD95B00] Processing finished
But it does not work at all when deployed.Can anyone help me?
I’ve used Webstorm and IntelliJ and the terminals in both suggest that the time it takes to run is about 1 second.
Using npm 2.15.8
How do you determine that it does not work? Nothing is saved in the database?
Also, could you add a line of code to print out the time right after the call to check10() ?
It does not save anything to the database, and I shall do that
Also, please let us know what version of JS CodeRunner you’re using.
After the Game1 function I put this in:
var d = new Date();
console.log(d.getTime());and after the check10 function I put this in:console.log(d.getTime());Then I manually ran it in debug mode and got this:16:43:55.674 - [76336115-0813-FA7D-FFA7-205235495700] New task arrived!
1470084235680
1470084235680
16:43:56.577 - [76336115-0813-FA7D-FFA7-205235495700] Processing finished
It did exactly want I wanted it to do but then I put it into production but it did not save anything!
How do I find what version of Coderunner I have? I’ve always downloaded the generated code files right from the business logic section.
I opened the UNIX executable in the bin file of the servercode If I’m not mistaken it is v1.7.4
According to the documentation, the business logic scripts may start asynchronous (running in background) operations. The CodeRunner can wait for their completeness before proceeding with an event
To make it work and let the CodeRunner knowledge about when the asynchronous operations are totally completed, the business logic should return a Promise which should be resolved only when the job is totally done.
Once CLOUD CodeRunner gets the Promise resolved it exits the process, interrupting any un-declared asynchronous operations. An operations which were not included into returned Promise.
DEBUG CodeRunner doesn’t exit the process, it just waits for other events, and thus allowing un-declared asynchronous operation to complete
Your problem is that your returned promise doesn’t include all async operations you run.
It is getting resolved right after retrieving all the games and starting your checks
You have to include all asynchronous operations started in checks to the returned promise.
This way CodeRunner will wait for their completeness in allowed execution time limits
Since all your check methods already return promises you should just combine them into one promise and return to the CodeRunner
Here is the idea :
//here we return a Promise to the CodeRunner
return Backendless.Persistence.of(Game1).find(query).then(result => {
//...
//here we prolongate a returned Promise by another bunch of async jobs
return Promise.all([
check0(),
//...
check9(),
check10()
])
}
It worked, thanks a bunch! I also learned a lot!