parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Abort Cloud Code execution when server has timed out

Open mtrezza opened this issue 5 years ago • 4 comments
trafficstars

Is your feature request related to a problem? Please describe.

Given a Parse Server is setup to have a request timeout set of 10s:

const httpServer = require('http').createServer(app);
httpServer.timeout = 10 * 1000;

When a Cloud Code function is called and takes 20s to execute, NodeJS destroys the socket after 10s, but the Cloud Code function keeps on executing. Some cloud code functions are just wasting resources in that case and it should be possible to abort them in a controlled manner.

Describe the solution you'd like

Add a status parameter to the request object of the Cloud Code function that is the current status of the request. When a request is aborted due to socket timeout, the status should be updated accordingly.

This allows it to query the request object at different points during the Cloud Code execution and abort the execution in a controlled manner without causing data inconsistencies.

The callback function of the server timeout can be used to update the request status:

httpServer.setTimeout(10 * 1000, {
   request.status = Parse.Cloud.FunctionRequest.Status.Timeout;  // pseudo code
});

Inside the Cloud Code function the status can be observed and processing aborted in a controlled manner:

Parse.Cloud.define("example", async (request) => {
   // Do something
   // ...

   if (request.status == Parse.Cloud.FunctionRequest.Status.Timeout) {
      // Abort here
      return;
   }

   // Do more
   // ...
});

Describe alternatives you've considered

Any ideas for alternatives?

Additional context

Originally posted as a question on SO, but since it seems that there is no current solution to this, opening an issue here.

mtrezza avatar May 10 '20 16:05 mtrezza

If someone is solving this already in some way, I'd be interested how. Maybe there is a better way than the suggested solution above.

mtrezza avatar May 11 '20 18:05 mtrezza

How about this vm feature? https://nodejs.org/api/vm.html#vm_timeout_interactions_with_asynchronous_tasks_and_promises

JoHuang avatar Aug 05 '21 14:08 JoHuang

https://www.nearform.com/blog/using-abortsignal-in-node-js/

This could work with AbortController. Thoughts @mtrezza?

dblythy avatar May 28 '22 17:05 dblythy

Good find, looks promising

mtrezza avatar May 28 '22 18:05 mtrezza