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;"done paramsInit with: _loopCount : " + _loopCount);

Moralis.Cloud.define("weirdFunctionThatRecallsOnError", async (request) => {
  _loopCount += 1;"_loopCount = " + _loopCount);"sleep 1 minutes");
  await sleep(1000 * 60);
// Should stop on error at above line, 
// but this function keep repeating it self from this point; 
//Not even reading next line !"This line will never be printed ?");


on frontend, I called it like this:

const testRecalling = async () => {
  await"paramsInit", {});
  await"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.


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:

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.