Use split and join

Hello everyone,

I'm new to node-red, I'm doing a very simple flow, but I was left with a doubt with proper use of split.

My flow, performs the operations in infinite loop, like this:
node-a -> node-b -> node-c -> node-a

Within this loop I read a (local) folder, if there are files in that folder, I do a "split to open simultaneous processes, so each message now performs an action, my question is whether I should" always "use join at the end of operations.

Thank you
Marcelo

Often a loop is not the right way to do such things in node red.
Can you describe at a higher level what you want to achieve?

Hi Colin,

Sorry English, my goal is:

  1. Read all files (json) present in a folder, example "c: \ input".
  2. Each file found, extract the json and send the json in the body to an external API.
  3. If the API returns StatusCode 200, I remove the file from the local folder.

Important: This process must be infinite, that is, it must be reading this folder and whenever a new file appears, node-red must follow this process explained above.

Can you help me get to the correct and most optimized code for this?

Thank you!
Marcelo

You can use a Watch node to tell you whenever the folder contents change, that will give you a message each time a new file appears or an existing file is changed (I think, though I haven't used it in exactly this way).
If a new file is added to you want to action just this file or re-do the whole set of files again? Your description suggests the latter but that seems odd.
Whichever is the answer then you need to send a set of messages with a filename in each one on to the next nodes which process them accordingly and once complete will delete the file. So no need for a loop.

Hi Colin,

Thanks for the help again ..

I did a test with the watch node, but I see that it is triggered only once when the file is created inside the monitored folder.

My wish:
When a file appears in the monitored folder, I must read and send its contents to an external API, if the internet connection is off, the files remain in the folder, until the connection is on, then the files will be read and sent again and deleted .

Marcelo

So save the file name in a contect variable array or maybe a database (in case there coulld be more that one file at a time) Create a flow to be fired - say every 10 minutes) that chcks to see if you have internet connection then reads the list of files and send them, delete them and remove the name from the list.

In that case you need to queue a list of files to be processed and drop them off the list when they are done successfully. I have a flow for such requirements.

That is a example of how to do it (see below for flow for importing). It looks more complex than it is. It uses node-red-contrib-q-gate so you will have to install that first. Then feed messages in where the Inject node at the top is, and replace the second section with the flow to handle those messages, and as each one is processed then send a message either to the OK link or the FAIL link. When a message is processed ok then it will start the next one in the queue. If it fails then it will wait before trying again. The retry interval when messages fail is set in the Retry Inject node down the bottom.

To test the example flow clicking the Inject node at the top sends in messages and whether the processing fails or not is set by clicking the internet up/down inject nodes. If the internet simulation is down then repeatedly clicking the inject queues them up (you can see the queue count by the q-gate node) and when the internet up inject is pressed they will be released one at a time. Here the process is just a 4 second delay.

[{"id":"f82aed15.9f1a98","type":"q-gate","z":"bdd7be38.d3b55","name":"q-gate","controlTopic":"control","defaultState":"queueing","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"peek","dropCmd":"drop","statusCmd":"status","maxQueueLength":"100","keepNewest":false,"qToggle":false,"persist":false,"x":450,"y":1880,"wires":[["ae4b0558.378d8","b6aba10c.b691f"]]},{"id":"6ec96b3b.ec5c14","type":"debug","z":"bdd7be38.d3b55","name":"OK","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":1720,"wires":[]},{"id":"fed5956.ed73668","type":"inject","z":"bdd7be38.d3b55","name":"inject messages","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":1440,"wires":[["ec13c69e.384328"]]},{"id":"4ef6d24.5459eac","type":"inject","z":"bdd7be38.d3b55","name":"Retry","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"15","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"peek","payloadType":"str","x":130,"y":2000,"wires":[["50d6b6a6.3b4db"]]},{"id":"4270c3f6.19012c","type":"delay","z":"bdd7be38.d3b55","name":"process","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":1580,"wires":[["b0c78702.b01988"]]},{"id":"a8381ab7.d1949","type":"link out","z":"bdd7be38.d3b55","name":"","links":["5a2f45b9.929cc4"],"x":795,"y":1680,"wires":[]},{"id":"5a2f45b9.929cc4","type":"link in","z":"bdd7be38.d3b55","name":"","links":["a8381ab7.d1949"],"x":135,"y":1880,"wires":[["1ec5d8f7.0e6ae7"]]},{"id":"1ec5d8f7.0e6ae7","type":"function","z":"bdd7be38.d3b55","name":"get next","func":"node.send({topic: \"control\", payload: \"drop\"})\nmsg.topic = \"control\"\nmsg.payload = \"peek\"\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":1880,"wires":[["f82aed15.9f1a98"]]},{"id":"672111d5.4f229","type":"inject","z":"bdd7be38.d3b55","name":"Simulate internet up","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"internet","payload":"up","payloadType":"str","x":230,"y":1640,"wires":[["edb0af2.fd1645"]]},{"id":"bc5fa483.adc8f","type":"inject","z":"bdd7be38.d3b55","name":"Simulate internet down","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"internet","payload":"down","payloadType":"str","x":230,"y":1680,"wires":[["edb0af2.fd1645"]]},{"id":"edb0af2.fd1645","type":"join","z":"bdd7be38.d3b55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":1660,"wires":[["b7feed8e.7caf68"]]},{"id":"b0c78702.b01988","type":"change","z":"bdd7be38.d3b55","name":"topic:data","rules":[{"t":"set","p":"topic","pt":"msg","to":"data","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1580,"wires":[["edb0af2.fd1645"]]},{"id":"b7feed8e.7caf68","type":"switch","z":"bdd7be38.d3b55","name":"Data message?","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"data","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":380,"y":1740,"wires":[["d96e80be.41aff"]]},{"id":"d96e80be.41aff","type":"switch","z":"bdd7be38.d3b55","name":"OK/Fail?","property":"payload.internet","propertyType":"msg","rules":[{"t":"eq","v":"up","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":560,"y":1740,"wires":[["6ec96b3b.ec5c14","a8381ab7.d1949"],["ea9c5a96.2dcfe8","37735513.24d77a"]]},{"id":"ea9c5a96.2dcfe8","type":"debug","z":"bdd7be38.d3b55","name":"FAIL","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":1760,"wires":[]},{"id":"a20949f9.306a48","type":"gate","z":"bdd7be38.d3b55","name":"","controlTopic":"gate_control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":650,"y":2000,"wires":[["f82aed15.9f1a98"]]},{"id":"ae4b0558.378d8","type":"change","z":"bdd7be38.d3b55","name":"Close gate","rules":[{"t":"set","p":"topic","pt":"msg","to":"gate_control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1960,"wires":[["a20949f9.306a48"]]},{"id":"a78ce065.5c9b68","type":"change","z":"bdd7be38.d3b55","name":"Open gate","rules":[{"t":"set","p":"topic","pt":"msg","to":"gate_control","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":2100,"wires":[["a20949f9.306a48"]]},{"id":"e5978b76.2fb2b","type":"link in","z":"bdd7be38.d3b55","name":"","links":["b6aba10c.b691f"],"x":135,"y":1580,"wires":[["4270c3f6.19012c"]]},{"id":"b6aba10c.b691f","type":"link out","z":"bdd7be38.d3b55","name":"","links":["e5978b76.2fb2b"],"x":655,"y":1880,"wires":[]},{"id":"37735513.24d77a","type":"link out","z":"bdd7be38.d3b55","name":"","links":["7f6b726d.0cab2c"],"x":795,"y":1800,"wires":[]},{"id":"7f6b726d.0cab2c","type":"link in","z":"bdd7be38.d3b55","name":"","links":["37735513.24d77a"],"x":95,"y":2100,"wires":[["a78ce065.5c9b68"]]},{"id":"5d647435.11ea14","type":"comment","z":"bdd7be38.d3b55","name":"Processed ok","info":"","x":690,"y":1660,"wires":[]},{"id":"cda552d1.59b25","type":"comment","z":"bdd7be38.d3b55","name":"Failed","info":"","x":670,"y":1800,"wires":[]},{"id":"6777d4.5738d02c","type":"comment","z":"bdd7be38.d3b55","name":"Replace this with your message processing, sending a message to the Fail or Success outputs","info":"","x":350,"y":1500,"wires":[]},{"id":"d1158219.184d38","type":"comment","z":"bdd7be38.d3b55","name":"Messages to be processed come in here","info":"","x":200,"y":1540,"wires":[]},{"id":"4681668e.9d5cd8","type":"comment","z":"bdd7be38.d3b55","name":"Feed messages to be queued in here and remove Inject node","info":"","x":270,"y":1400,"wires":[]},{"id":"177f73a0.c29e2c","type":"status","z":"bdd7be38.d3b55","name":"","scope":["f82aed15.9f1a98"],"x":120,"y":2040,"wires":[["b95b8054.e3339"]]},{"id":"b95b8054.e3339","type":"function","z":"bdd7be38.d3b55","name":"Queue empty?","func":"// expects msg.status.text in form \"queuing: <number>\"\n// sends a message if queue is empty\nif (parseInt(msg.status.text.split(\":\")[1]) !== 0) {\n    msg = null\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":2040,"wires":[["a78ce065.5c9b68"]]},{"id":"e72ba10d.c92638","type":"comment","z":"bdd7be38.d3b55","name":"Set this to appropriate retry interval","info":"","x":190,"y":1960,"wires":[]},{"id":"50d6b6a6.3b4db","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"peek","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":2000,"wires":[["a20949f9.306a48"]]},{"id":"ec13c69e.384328","type":"link out","z":"bdd7be38.d3b55","name":"","links":["781b2145.876168"],"x":425,"y":1440,"wires":[]},{"id":"781b2145.876168","type":"link in","z":"bdd7be38.d3b55","name":"","links":["ec13c69e.384328"],"x":295,"y":1920,"wires":[["f82aed15.9f1a98","50d6b6a6.3b4db"]]},{"id":"7f6da01b.0e5768","type":"comment","z":"bdd7be38.d3b55","name":"Leave this alone except for setting retry interval","info":"","x":220,"y":1840,"wires":[]}]

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