Skip to content

Instantly share code, notes, and snippets.

@Team-509
Created November 13, 2017 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Team-509/b050468c3fa39162a875f10e399b405a to your computer and use it in GitHub Desktop.
Save Team-509/b050468c3fa39162a875f10e399b405a to your computer and use it in GitHub Desktop.
node-red-contrib-failover 0.0.1

I wanted to send payloads out to an alt broker if my primary failed. This is what I did. This function has an single input and 3 outputs. Input accepts any normal topic/payload combination and passes it to output 2 or output 3 depending on the status. Status is set by an input payload of 'safe' or 'fail'. On deploy it is 'fail' by default. If status is 'fail' all topic/payload combinations are passed to output 3. If a payload of 'safe' is received, all topic/payload combinations are passed to output 2. Additionally, every time an input of 'safe' or 'fail' arrives, it is passed only to output 1. The included flow is also setup as an example. Have fun.

[{"id":"36b543b8.a1594c","type":"inject","z":"a54a6c90.9f761","name":"","topic":"","payload":"safe","payloadType":"str","repeat":"","crontab":"","once":false,"x":728,"y":495,"wires":[["658a6118.20c19"]]},{"id":"21bc0e96.892b52","type":"inject","z":"a54a6c90.9f761","name":"","topic":"","payload":"fail","payloadType":"str","repeat":"","crontab":"","once":false,"x":728.5,"y":540,"wires":[["658a6118.20c19"]]},{"id":"658a6118.20c19","type":"function","z":"a54a6c90.9f761","name":"Failover","func":"var status = context.get('status')||\"fail\";\nvar msg1 = {payload: 0};\n\nif (status === \"safe\") {\n if (msg.payload === \"fail\") {\n context.set('status', \"fail\");\n msg1.payload = \"fail\";\n node.status({fill:\"red\",shape:\"ring\",text:\"fail\"});\n return [msg1, null, null];\n } else if (msg.payload === \"safe\") {\n msg1.payload = status;\n node.status({fill:\"green\",shape:\"dot\",text:\"safe\"});\n return [msg1, null, null];\n } else {\n msg1.payload = status;\n node.status({fill:\"green\",shape:\"dot\",text:\"safe\"});\n return [null, msg, null];\n }\n}\nelse if (status === \"fail\") {\n if (msg.payload === \"safe\") {\n context.set('status', \"safe\");\n msg1.payload = \"safe\";\n node.status({fill:\"green\",shape:\"dot\",text:\"safe\"});\n return[msg1, null, null];\n } else if (msg.payload === \"fail\") {\n msg1.payload = \"fail\";\n node.status({fill:\"red\",shape:\"ring\",text:\"fail\"});\n return [msg1, null, null];\n } else {\n node.status({fill:\"red\",shape:\"ring\",text:\"fail\"});\n return [null, null, msg]; \n }\n} \n","outputs":"3","noerr":0,"x":1025,"y":515,"wires":[["b2af2034.c9e7a"],["71726067.223b6"],["c97979e8.146128"]]},{"id":"c25bb4fd.ab2038","type":"inject","z":"a54a6c90.9f761","name":"","topic":"sample/date","payload":"","payloadType":"date","repeat":"10","crontab":"","once":true,"x":746.5,"y":590,"wires":[["658a6118.20c19"]]},{"id":"b2af2034.c9e7a","type":"debug","z":"a54a6c90.9f761","name":"","active":true,"console":"false","complete":"false","x":1229,"y":487,"wires":[]},{"id":"71726067.223b6","type":"debug","z":"a54a6c90.9f761","name":"","active":true,"console":"false","complete":"false","x":1231,"y":526,"wires":[]},{"id":"c97979e8.146128","type":"debug","z":"a54a6c90.9f761","name":"","active":true,"console":"false","complete":"false","x":1230,"y":563,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment