Circuit-Breaker
DESCRIPTION: This subflow checks the technical fail percentage of a flow. At its optimum use the flow has a function which counts every fail and success. The subflow takes these numbers every 15 seconds and calculates if the fail percentage threshold is exceeded. If yes the flow is stopped for some time(which is chosen by user) to cooldown. After that time ends the subflow gives the flow a go and then the cycle begins again.
DETAILS:
This subflow takes 2 variables from the user. cooldownTime and failThreshold. With these variables the user, who wishes to have a more active flow, can use it with a lesser cooldownTime or vice-versa. Also the user can reduce or increase their failThreshold depending on their fail expectancies.
As given above... This subflow works with a fail counter function. Which should count fails and successes. (The variables required will be given in a photo...)
Here is a Function Node example:
[{"id":"684766f6b176af89","type":"subflow","name":"Subflow 7","info":"","category":"","in":[],"out":[{"x":1090,"y":80,"wires":[{"id":"68511cac815a3cd3","port":0}]},{"x":1130,"y":220,"wires":[{"id":"373fac3af2b6fa87","port":0}]},{"x":1140,"y":360,"wires":[{"id":"373fac3af2b6fa87","port":1}]}],"env":[{"name":"cooldownTime","type":"num","value":"20"},{"name":"failThreshold","type":"num","value":"15"}],"meta":{},"color":"#3FADB5","outputLabels":["Flow Controller 1","Flow Controller 2","Status Checker"],"icon":"node-red-contrib-queue-gate/queue.png"},{"id":"373fac3af2b6fa87","type":"function","z":"684766f6b176af89","name":"calculate stats through flow variables","func":"\nmsg.failPercentage = flow.get(\"failCounter\") * 100 / (flow.get(\"failCounter\") + flow.get(\"successCounter\"));\nflow.set(\"successCounter\", 0);\nflow.set(\"failCounter\", 0);\nlet msg1;\nlet msg2;\n\nif(flow.get(\"failThreshold\") <= msg.failPercentage){\n msg.topic = \"control\";\n msg.payload = \"close\";\n msg[\"timeout\"] = flow.get(\"cooldownTime\");\n msg1 = msg;\n} else {\n msg.topic = \"control\";\n msg.payload = flow.get(\"failCounter\");\n msg2 = msg;\n}\n\n\nreturn [msg1, msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":200,"wires":[["0366e4dba1bbc8b2"],[]]},{"id":"29327a84206f99a7","type":"inject","z":"684766f6b176af89","name":"PERIODIC CHECK","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"failPercentage","v":"","vt":"num"}],"repeat":"15","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":300,"wires":[["56b259b5dbb88083"]]},{"id":"68511cac815a3cd3","type":"function","z":"684766f6b176af89","name":"control gate","func":"\n\n//if threshold is reached and timer is not set, close gate\n\n\n//if timer >limit open gate\n\nif(msg.payload === 1){\n msg.payload = \"open\";\n}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":80,"wires":[[]]},{"id":"0366e4dba1bbc8b2","type":"mytimeout","z":"684766f6b176af89","name":"","outtopic":"","outsafe":"","outwarning":"Warning","outunsafe":"off","warning":"5","timer":"30","debug":false,"ndebug":false,"ignoreCase":false,"repeat":false,"again":false,"x":450,"y":80,"wires":[["68511cac815a3cd3"],["68511cac815a3cd3"]]},{"id":"56b259b5dbb88083","type":"change","z":"684766f6b176af89","name":"","rules":[{"t":"set","p":"cooldownTime","pt":"flow","to":"cooldownTime","tot":"env"},{"t":"set","p":"failThreshold","pt":"flow","to":"failThreshold","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":260,"wires":[["373fac3af2b6fa87"]]}]