Cloud Function calling it self again after hitting error (?)

Hi Guys,
I have the issue that Cloud function, when hitting an error, sometimes not reporting error, but automatically recalling it self from beginningā€¦ Is it an intended behavior? or there might be that issue but not an intended behavior? Or that is just a bug of my code?
I am using Moralis-React on front end.

Hi @tungtien,

Well, could you please attach some screenshots and also your code to perhaps understand the issue. If this is a real issue, we can report it to the devs.

Happy BUIDLing. :man_mechanic:

1 Like

OK. My code is big and confusingā€¦ Will try to isolate the problem and recreate the issueā€¦

Hi @malik,

finaly I reacreated this issue:

My Clouds code is:


const logger = Moralis.Cloud.getLogger();

const sleep = async (ms) => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

var _loopCount = 0;

Moralis.Cloud.define("paramsInit", async (request) => {
  _loopCount = 0;
  logger.info("done paramsInit with: _loopCount : " + _loopCount);
  return;
});

Moralis.Cloud.define("weirdFunctionThatRecallsOnError", async (request) => {
  _loopCount += 1;
  logger.info("_loopCount = " + _loopCount);

  logger.info("sleep 1 minutes");
  await sleep(1000 * 60);
  aBugHere; 
// Should stop on error at above line, 
// but this function keep repeating it self from this point; 
//Not even reading next line !

  logger.info("This line will never be printed ?");

  return;
});

on frontend, I called it like this:

const testRecalling = async () => {
  await Moralis.Cloud.run("paramsInit", {});
  await Moralis.Cloud.run("weirdFunctionThatRecallsOnError", {});
  return { Error: null };
};

Intended behavior: Function should stop and report error but not looping it self.
In reality, it loops forever:

PS: sometimes, function loops but the ERROR notice even did not shown up at allā€¦ But I could not isolate that issueā€¦

Hey @tungtien

The behavior of the code may seem unexpected. But this is just a feature of cloud functions. You should try not to declare variables globally, declare variables only inside of the cloud function.

If you want to start a cloud function periodically, you can use Cloud Jobs

1 Like

So do you mean: By design, cloud function will restart from beginning when it hit error?
But like in above case, without the 1 minute sleep time it will not be restarted, but just stop on error.
So, what exactly caused the restarting behaviour ? The sleep function? Or my usage of global variables?

Hi @tungtien

Iā€™ve did some more tests. You are right itā€™s a bug because of promise(if you will change time e.g. to 60 ms it will work correctly). We need some time to manage the problem.

Thank you for the report :raised_hands:

1 Like

Yes. that the problem I was struggling for last few daysā€¦ Clouds function keep restarting if a process take a little long time to implementā€¦

Hi @tungtien

After some time looking at the issue. The cloudfunctions do not recallon error.
This is a behaviour of crome and mozilla browsers that retries on failure:

Cloud functions recalls itself only when you have an active request from browser.

2 Likes

Very nice insight you laid down here! :star_struck:

1 Like

Thanks Yomoo,
So Do you mean it is caused by my code on front end? Maybe some thing related to React code (on which I am not very familiar yet)ā€¦ and I am also seeing some red alerts about CORS things on the browser consoleā€¦
Any suggestions for me to solve this?

Hey @tungtien

  1. You should not do artificial slowdowns on the side of cloud functions (setTimeout), if you need it, then it is better to do it on the client side.
  2. You can track the results of the request onError and etc. Take a look at: https://github.com/MoralisWeb3/react-moralis#handling-responses

Let me know if you have any questions :man_mechanic:

The setTimeOut is just to reproduce the error.
In practice, I want the clouds funciton to make some onchain querry for prices, allowance and token transfersā€¦ I want to make a bot to work independently event after user has closed his front end connectionā€¦ So the bot should make some loops and await for some market conditions before implementing some swap ordersā€¦
Will the team work to solve this recalling problem or this issue is by design and unsolvable?

If you want to start a cloud function periodically, you need to use Cloud Jobs

The problem is that you are using the wrong approach for this task. You need to use triggers, cloud jobs and not sending the function to sleep.

1 Like

Thanksā€™ for your kind help @Yomoo, I will study Cloud Jobsā€¦
Regarding the recalling issue, I think that cloud function recalls even if there is no errors at all. Because the same code I tested on front end can run smoothly, but it doesnā€™t runs on clouds, recalls from beginning of function and without any noticesā€¦ I hope the team will give the solution for that soonā€¦

Great. I got it. That is why I was thinking why we need that Clouds Job while it is also a kind of clouds function :slight_smile:

Can you suggest any link for tutorial or sample code for the use of Cloud Job? I ve watched thru almost all videos on the channel but could not find about thisā€¦

Hi @Yomoo,
I ve read about cloud job and
I am planning to solve this this way to implement the limit trade:
I will create Cloud job to fetch token price every second. Then save data to database. Then use Trigger to call swap order when certain price is meet.
Do you think it would work? Any better suggestions?

Iā€™m curious how you trigger a swap order, as youā€™ll need to sign the transaction.