Creating a loop

Hi

We have NodeRed running in a docker and as part of teh process I need to send a file to a windows machine which processes the file and then outputs a new file. I need to build a process in NodeRed which outputs the file and then monitors a folder waiting for it to come back, it needs to time out after 1 hour and error if it hasn't come back

In our previous solutuion I did this by having a script which had a for loop in for 60 instances and sleep for 1 minute so it just checked the folder using the FileSystemObject and if it was there then it broke out of the for loop and continued processing. However I have been advised that I can't install the FileSystemObject libary as there are security concerns?!?!

So I'm trying to figure out how to create somethign similar using nodeRed nodes and would appreciate any ideas

Perhaps you can use the Watch node to watch for changes on the folder.

There's no timeout on that unfortunatley so if the job faield on the other process the job would just sit there

Do the timeout separately. You can probably use a Trigger node, usually that is the one to use for timeouts.

This is not a good way to do things in JavaScript. If you really need a periodic check, you should use a setInterval. That will clear the process loop until the appropriate time (in milliseconds) has passed. This can be used in a function node but you have to keep a context variable reference to it so it doesn't get recreated each time a msg hits it.

However, as Colin has said, the "Node-RED way" would be a watch node which will trigger when the folder is updated and a trigger node to handle the timeout. Set the trigger node to extend the time if another msg is received.

Set the trigger node to Send Nothing then Wait 1 hour, then send the timeout message. Send it a message when you send the file. When the Watch node sees the response then send the Trigger a message with msg.reset true, which will reset it so it doesn't send the error message.

For this application you don't want Extend Delay set.

This flow will check for the existence of a file, at 5 second intervals, for a maximum of 5 tries.

[{"id":"6a1930e52aa39bff","type":"change","z":"1fea539c47f57171","name":"count ++","rules":[{"t":"set","p":"count","pt":"msg","to":"count + 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":200,"wires":[["ab6c2dd3313b6a8c"]]},{"id":"ab6c2dd3313b6a8c","type":"switch","z":"1fea539c47f57171","name":"","property":"count","propertyType":"msg","rules":[{"t":"gte","v":"5","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":200,"wires":[["d026a82783c97e1c"],["a559353cc6c69139","2e9f3b7fa4389351"]]},{"id":"d026a82783c97e1c","type":"debug","z":"1fea539c47f57171","name":"timeout","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"filename","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":200,"wires":[]},{"id":"a559353cc6c69139","type":"delay","z":"1fea539c47f57171","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":300,"y":200,"wires":[["6a1930e52aa39bff"]]},{"id":"55b3120bc6155e44","type":"inject","z":"1fea539c47f57171","name":"New file","props":[{"p":"filename","v":"/home/pi/junk.txt","vt":"str"},{"p":"count","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":200,"wires":[["a559353cc6c69139"]]},{"id":"2e9f3b7fa4389351","type":"file in","z":"1fea539c47f57171","name":"","filename":"filename","filenameType":"msg","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":720,"y":280,"wires":[["e2d62919e3229f8e","91095075d97bd933"]]},{"id":"e2d62919e3229f8e","type":"debug","z":"1fea539c47f57171","name":"File exists","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":240,"wires":[]},{"id":"91095075d97bd933","type":"change","z":"1fea539c47f57171","name":"set reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":140,"y":280,"wires":[["a559353cc6c69139"]]},{"id":"8913b169c00f4271","type":"catch","z":"1fea539c47f57171","name":"read failed","scope":["2e9f3b7fa4389351"],"uncaught":false,"x":300,"y":340,"wires":[["01197d8aae868738"]]},{"id":"01197d8aae868738","type":"debug","z":"1fea539c47f57171","name":"Not yet","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"filename","targetType":"msg","statusVal":"","statusType":"auto","x":460,"y":340,"wires":[]}]