Retry Netlify Function after 15-30 minutes if error in the response

I have a client that has a unique request and was wondering if anyone knows if Netlify may have a solution for this.

I currently wrote a Netlify Function that is hit when a user clicks a button. Inside of that function, it makes an external call to another endpoint (outside of Netlify). The problem is that sometimes this endpoint has issues and is down. So the client would like me to retry it every 15 or 30 minutes or so, for a maximum of three retries.

Does anyone have any idea if Netlify has any kind of ability to handle this? I was thinking maybe I’d have to use background functions to make the calls since the user will most likely close the browser within that time while it’s retrying. If using a background function (which has a 15 min timeout), I’m thinking we set some kind of timer to call the external endpoint after 10 minutes or so within the function, and then if it fails, call the background function again.

Just wondering if anyone has any ideas on how this might be able to be solved. Thanks!

Netlify doesn’t have a built-in feature to handle this. There’s no quick and easy way to solve this on just Netlify. You can definitely use Background Functions if they suit your case.

Thank you for the response @hrishikesh !

Thanks for the explanation @hrishikesh ! Do you know if there is a way to cancel a Background Function? In our case, we’ll call the same Background Function up to three times. But if the call is successful say in the second call, we don’t want to waste resources and were wondering if there would be a way to cancel it at that time. Do you know if there is a way to cancel it if needed?

You’d have to log to an external database or similar and check there if you should proceed with the execution or not.

Thank you @hrishikesh !

Hi @hrishikesh ,

Thank you for your help, I do have another follow-up question for you if you do not mind.

Inside of this background function, we do have it call itself after a few minutes again to retry in case there was an error with an external service. This works fine locally as I can see it calling itself after the time elapses inside of the setTimeout, which is currently to 1 minute.

However, when deployed to production, and visiting the function logs for this background function, I do not see any more logging activity after the first call, so it appears to never get inside of the setTimeout, which calls the same background function again.

Do you know why it would behave differently on local (netlify dev), vs production?

Thanks!

Nevermind @hrishikesh ! The part where the background function was not being recalled again was due to an issue with my code. I refactored the setTimeout I had with this solution instead and it now works. Sorry to bother, thank you!