AnsweredAssumed Answered

Script Node move

Question asked by smcardle on Feb 13, 2013
Latest reply on Feb 20, 2013 by smcardle
Hi All

I'm trying to get to the bottom of an issue in a JavaScript webscript controller.

We have thousands of spaces to move to a new parent structure. The current structure has maybe 50 parent spaces each of which has several thousand sub spaces to move under a new top level space. The following is an example of the current structure…


Branches —
     AB —
          10001 —
          10002 —
          …
     CD —
          200002 —
          210001 —
          …
     EF —
     …

And we are moving to this structure

Clients —
    10001 —
    10002 —
    10003 —
    ….

i.e the Clients under each branch are being flattened into a dedicated Clients space

As I said, each branch has several thousand clients and in total there are 10s of thousands to move. There should not be any overlap of clients i.e. a client space should not exist in more than one branch. However, there are exceptions :(

So, I created a javascript web script to move the clients in each branch to the new Clients space (one branch at a time only) /api/move-branch/{branchid}

Now my problems … There are 2

As this is a webscript everything is done in one transaction so if the webscript produces an exception everything roles back (this works). The problem here is that the script node move is supposed to return true or false, however, if the space already exists under the client structure it throws an exception and even if I catch the exception and handle it Client move's occurs, it seam to be that the exception triggers a transaction rollback which I would have only expected has I exited the web script via an exception. I'm pretty sure this is not correct.

The only way I could get this to work was to check the new Client parent space for the existence of the new Client prior to doing the move and only if it does not exist attempt the move… This adds extra checking that should not really be needed.

My second problem…. It take over an HOUR just to move about two thousand spaces ????? Why does this method take so long ??? It means our migration script could run for DAYS???

Here is the webscript….



function moveBranch() {

    var notMovedClients = new Array();       
    var movedClients = new Array();       

   var branchid = url.templateArgs["branchid"]
   if(branchid) {
      try {
         var branch = companyhome.childByNamePath("/Branches/" + branchid);
         if(!branch) {
            status.setCode(status.STATUS_OK, "Branch [" + branchid + "] does not exist. Nothing to do.");
            return;
         }
         var clients = companyhome.childByNamePath("/Clients");
         var remove = true;

         var len = branch.children.length;
         for(var i = 0; i < len; i++) {
            var child = branch.children;
            try {
               if(clients.childByNamePath(child.name)) {
                  // Should not be required as move should return true or false.
                  // This does stop move throwing exceptions and causing the whole transaction to abort
                  remove = false;
                  notMovedClients.push(child.name);
                  continue;
               }
               var moved = child.move(clients);
               if(!moved) {
                  remove = false;
                  notMovedClients.push(child.name);
               } else {
                  movedClients.push(child.name);
               }
            }catch(e) {
               // The move should not throw an exception but it does so catch it
               // and treat it as a failure to move
               remove = false;
               notMovedClients.push(child.name);
            }
         }
         if(remove) {
            branch.remove();
         }
         var message = "Finished processing Branch [" + branchid + "].";

         if(notMovedClients.length != 0) {
            message += " The following clients were not able to be moved automatically and will need to be moved by hand [" + notMovedClients + "].";
         }
         if(movedClients.length != 0) {
            message += " The follwing clients were moved [" + movedClients + "].";
         }
      
         status.setCode(status.STATUS_OK, message);
         return;
      }catch(e) {
         status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, e.toString());
         return;
      }

   } else {
      status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, "Branch must be defined");   
      return;
   }
}

moveBranch();


Any insights here or an alternative method to achieve the same thing would be good..



Steve

Outcomes