setTimeOut in Backend not Working

I’ve set a function to execute in my backend after a certain amount of time, using a setTimeOut. Oddly, if I set the timeout to be less than 60000 ms, the code executes. Anything greater than or equal to that, then the code within the setTimeout does not run. After searching online, I think it may have something to do with “The This Problem” as shown in this link:

Based on that article, I was under the impression that I need to use .bind, but I am really not too familiar with this and am struggling to get the proper syntax.

My question is, should I be using .bind() to fix this issue and if so what is the proper way to add it to the code?

As a current example here is what a version of my code looks like. With this code, the setTimeout with 6000 ms works, but the one with 60,000 does not. Happy to provide more information.

// In Front End

userComplete(a, b);

// In Backend


export function userComplete (a,b) {

setTimeout(() => {
addData(a.proj_sub_id, b, a.proj_instance_id); 
}, 60000);

setTimeout(() => { 
addData(a.proj_sub_id, b, a.proj_instance_id);
}, 6000);

Desperate for some help here, so bumping.

Hi David.
Can you describe what are you trying to achieve? Why to use delayed execution? Maybe it can be achieved in another way?

Please pay attention, using setTimeout on the backend can lead to non-deterministic behaviour.

Regards,
Genry.

Genry,

I have a database of projects. The projects are created and managed by “Project Managers” and the actual task at hand can be completed by “Users”. As soon as a Project Manager marks a project as completed, I transact payment and the User gets paid. However, in certain instances a User may complete a project, but a Project Manager neglects to mark the project as complete. So, I need to set it up where a User can mark a project as completed and if the Project Manager does not contest that after 3 days, it automatically marks itself as completed.

So, I need the code to operate where as soon as a User marks a project as complete, it will wait 3 days, check to see if Project Manager marked it as complete and if not, complete it and transact charge regardless.

I can handle all of the coding, it’s just the ability to set the timeout which I am really struggling with. I assumed that I had to setTimeout in the backend, as anything in the front-end would kill the timer as soon as the browser is exited?

Thanks!

Hi David.

Sounds interesting, what you need is an ability for a background task.
This is currently not available in WixCode and you are welcome to add a feature request post in the feature requests forum. https://www.wix.com/code/home/forum/feature-requests

As a side note, regarding the problem at hand, since the period of the waiting can be up to 3 days, this means that you need periodic polling of the status up to 3 days. Pay attention, with the current solution in question, when the user closes the browser, the backend call will be terminated also - meaning, if Project Manager will not mark - it will not be marked.

The reason why for smaller delay it works for you and for larger not, is because you reach a request timeout. Client calls for backend to execute the web method, once it does not receive a response for a particular amount of seconds - it times out and terminates the request.

Hope this helps.

Regards,
Genry.

Genry, appreciate the prompt response. Unfortunately, that news is a bit devastating. I will add a feature request, however is there any other work around that you could think of? Part of the reason I ran it in the backend is I would’ve assumed it never terminates itself like a user session would. Is there any reason to believe Wix already has some sort of feature to help with this in the works??

Thanks!

Maybe if you come up with the scheduling mechanism externally, you can create an web endpoint on site and call it from such mechanism to perform the logic there.

More info about wix-http-functions: wix-http-functions - Velo API Reference - Wix.com

Best regards,
Genry.

1 Like

Genry, interesting solution. I am trying to research this separately, but want to make sure I am not taking myself down the rabbit hole. I stumbled across third party CRON services, such as https://www.easycron.com/.

Is this what your referring to?

Thanks!

From what I see, it can be used as a scheduling service by interval. You even can think of a general task that will perform those “marked already” polls by some “queue”, which can be actually a collection with “still to be verified projects”.

Regards,
Genry.

Hi David,

Did you figure this out? I had a similar issue and found a solution that is a bit of a hack but should work ok.

-Eric

Hi Genry,

Do you know the default timeout condition for requests on the server side? The client side seems to be 14 seconds, but I’m more concerned about when the server will timeout, and whether this can be changed. I don’t need to get responses back from the server to the client, so the client timeout does not negatively impact me.

Thanks,
Eric