Flow works unreliably

I am doing an operation which consists of sending an api request from a function node to a http node. The operation modifies a label on a remote resource. Due to some limitation the modification operation cannot be done directly but consists of removing the existing label and adding a new label. This corresponds to sending two requests to the http node - first a deletelabel followed by addlabel:

for (var count = 0; count < num_resources; count++) {
  //example url
  msg.url = "https://myresourceurl/";

  //delete existing label
  msg.payload = JSON.stringify(del_label_jsonobj);
  node.send(msg);

  //add new label
  msg.payload = JSON.stringify(newlabel_jsonobj);
  node.send(msg);
}

The issue is that its not doing it reliably. If there are 10 resources to modify, these are the behaviours I have observed:

  1. Most of the resources have both the newlabel and the old label present (which means deletelabel did not work but addlabel worked)
  2. Few labels have only modified label (which means both the operation worked)

The code seems to be ok since it works 100% reliably if the addlabel and deletelabel are done individually. It's the combination of two operation that does not work. Any insight/help is appreciated.

Hi @trexnickel and welcome to the Forums.

I think maybe you're experiencing some race conditions.
node.send is an async method, so will not wait before continuing down your code block.

Therefore the web service maybe processing newlabel_jsonobj first (as you don't wait for del_label_jsonobj to finish)

I would also be careful if modifying payload whillst using async methods, as the payload can be modified whilst its in transit.

See attached for a possible alternative

[{"id":"51decd3bab97be26","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"01d0828a5e57a822","type":"function","z":"51decd3bab97be26","name":"function","func":"for (var count = 0; count < num_resources; count++) {\n    const Message = {\n        url: \"https://myresourceurl/\",\n        payload: JSON.stringify(del_label_jsonobj),\n        add:JSON.stringify(newlabel_jsonobj)\n    }\n    node.send(Message)\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":250,"wires":[["5cff1d01fa3bbbb1"]]},{"id":"5cff1d01fa3bbbb1","type":"http request","z":"51decd3bab97be26","name":"Process Delete","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":450,"y":250,"wires":[["255e8f1f7c24274b"]]},{"id":"902dc7389078ecf2","type":"http request","z":"51decd3bab97be26","name":"Process Add","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":885,"y":250,"wires":[[]]},{"id":"255e8f1f7c24274b","type":"change","z":"51decd3bab97be26","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"add","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":245,"wires":[["902dc7389078ecf2"]]}]

This method will guard against modifying the object, whilst its in use and will wait for the response from the delete request before adding

Hi @marcus-j-davies. Thanks for pointing out the possible race condition. I believe that was the issue. Also the possibility of modification of payload before the remote web server processes the request. After incorporating the alternative, it worked! Thank you very much for your help:)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.