How to add timeout for cloud code?

How to add timeout for cloud code? İf cloud code didn’t return anything in 30 second, i wanna terminate process. How to do it? Or is it possible?

It’s possible with a recursive promise style code. Did you have an example of the cloud code you run ?

1 Like

It really depends on how you want to handle the timeout.

You can add a general timeout for Parse Server that includes requests to Cloud Code. See the http.server timeout parameter. This will notify your client after 30s that the requests could not be completed, but it won’t stop the Cloud Code execution on the server, so Parse Server will still use resources processing the requests. It usually makes sense to also set the timeout for the database requests, because if Cloud Code times out after 30s, there may be little use running a database query for longer because its results will be discarded. See the maxTimeMS Parse Server configuration for MongoDB.

If you want to also abort the Cloud Code function to free the server resources, this is currently not implemented in Parse Server. There is an open feature requests for this.

1 Like

Yeah sure

  Parse.Cloud.define("getHomeObjects", async (request) => {

  const user = request.user;

  const Post = Parse.Object.extend("Post");

  const Follow = Parse.Object.extend("Follow");

  const getFollow = new Parse.Query(Follow);
  getFollow.equalTo("owner",user);

  const getPosts = new Parse.Query(Post);
  getPosts.matchesKeyInQuery("user","who",getFollow);
  getPosts.include("user");
  if(request.params.date!==undefined){
    getPosts.lessThan("createdAt",request.params.date);
  }
  getPosts.exclude("words");
  getPosts.descending("createdAt");
  getPosts.limit(40);

  const postList = await getPosts.find({useMasterKey:true});
  if(postList.length<1){
    return postList;
  }




  const Like = Parse.Object.extend("Like");
  const getLikes = new Parse.Query(Like);
  getLikes.equalTo("owner",user);
  getLikes.containedIn("post",postList);

  const likeList = await getLikes.find({useMasterKey:true});

  var likeListIDs = [];

  var i;
  for(i=0;i<likeList.length;i++){
    likeListIDs.push(likeList[i].get("post").id);
  }


  const SavedPost = Parse.Object.extend("SavedPost");
  const getSaves = new Parse.Query(SavedPost);
  getSaves.equalTo("owner",user);
  getSaves.containedIn("post",postList);

  const saveList = await getSaves.find({useMasterKey:true});

  var saveListIDs = [];

  var i;
  for(i=0;i<saveList.length;i++){
    saveListIDs.push(saveList[i].get("post").id);
  }




  const tpL = postList.map(post => ({
    ...post.toJSON(),
    liked2: likeListIDs.indexOf(post.id) >= 0,
    saved2: saveListIDs.indexOf(post.id) >= 0,
    likenumber2:post.likenumber,
    commentnumber2:post.commentnumber,
    commentable2:post.commentable
  }));



  var lastList = [];

  for(i=0;i<tpL.length;i++){
    var post = tpL[i];

      var jsonUser = post.user;
      delete jsonUser.email;
      delete jsonUser.ACL;
      delete jsonUser.token;

      jsonUser.__type = "Object";
      jsonUser.className = "_User";
      post.user=jsonUser;

    post.likenumber2 = post.likenumber;
    post.commentnumber2 = post.commentnumber;

    post.commentable2 = post.commentable;
    post.__type = "Object";
    post.className = "Post";
    lastList.push(post);
  }

  
  return lastList;





});

Thanks. I will check it out