Artificial Delay flow with Openwhisk Action Wrapper

The core flow handles a control loop where delay and iteration numbers are dynamically passed on as input variables during the action invocation. This is needed to enhance experiment automation, so that we can pass through arguments the number of n repetitions of the main delay loop, in order to simulate a flow of n functions, each with the set delay. The respective flow should also be executable as an Openwhisk function, which means it needs to abide by the respective interface of the latter (one POST /init method and one POST /run method. The function node in the middle subflow extracts parameters from the incoming message and passes them through the msg.delay and msg.iterations fields. The follow-up node is a Node-RED built-in delay node that gets the needed delay from the assigned msg.delay field. The whole process is iterated until the number of needed delays are met.

dynamicdelayflow

[{"id":"ba607e41.91714","type":"switch","z":"bcbdd3c91c32b3d6","name":"if iterations finished","property":"iterations","propertyType":"msg","rules":[{"t":"gte","v":"1","vt":"num"},{"t":"lt","v":"1","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":770,"y":380,"wires":[["8b7b362e.ca3358"],["c934a3dcf8cb04b1"]]},{"id":"a440efec.8f87f","type":"http in","z":"bcbdd3c91c32b3d6","name":"","url":"/run","method":"post","upload":false,"swaggerDoc":"","x":180,"y":240,"wires":[["381140af.03f2e"]]},{"id":"381140af.03f2e","type":"function","z":"bcbdd3c91c32b3d6","name":"extract params and put in msg.delay and msg.iterations","func":"\nif (msg.payload.value.hasOwnProperty('iterations')){\n    msg.iterations=msg.payload.value.iterations;\n} else {\n    msg.iterations=3;\n}\n    \nif (msg.payload.value.hasOwnProperty('delay')){\n    msg.delay=msg.payload.value.delay;\n} else {\n    msg.delay=1000;\n}\nconsole.log(msg);\n\nvar instance = context.get('inst') || 0;\nif (!instance){\n    instance = Math.random();\n    context.set('inst', instance);\n}\nmsg.payload.value.randomID=  instance ;\n//msg.payload = { 'text': 'Random ID: ' + instance };\n\n\nmsg.start=Date.now();\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":240,"wires":[["8b7b362e.ca3358"]]},{"id":"8b7b362e.ca3358","type":"delay","z":"bcbdd3c91c32b3d6","name":"variable in msg.delay","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":880,"y":240,"wires":[["ef917b3f.a5bfa8"]]},{"id":"7e5967.286e8698","type":"http response","z":"bcbdd3c91c32b3d6","name":"","x":970,"y":500,"wires":[]},{"id":"e50ceb4.d689218","type":"http in","z":"bcbdd3c91c32b3d6","name":"","url":"/init","method":"post","upload":false,"swaggerDoc":"","x":180,"y":140,"wires":[["b6b9ab87.e37c38"]]},{"id":"b6b9ab87.e37c38","type":"http response","z":"bcbdd3c91c32b3d6","name":"","x":490,"y":140,"wires":[]},{"id":"728a5b4a.3b7ed4","type":"http request","z":"bcbdd3c91c32b3d6","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"localhost:1880/run2","tls":"","persist":false,"proxy":"","authType":"basic","x":470,"y":600,"wires":[["6fec2721.679488"]]},{"id":"6843f3cb.b3cf7c","type":"inject","z":"bcbdd3c91c32b3d6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":120,"y":600,"wires":[["bd1c60c7.064d7"]]},{"id":"6fec2721.679488","type":"debug","z":"bcbdd3c91c32b3d6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":600,"wires":[]},{"id":"bd1c60c7.064d7","type":"function","z":"bcbdd3c91c32b3d6","name":"","func":"msg.payload={};\nmsg.payload.value={};\nmsg.payload.value.iterations=3;\nmsg.payload.value.delay=3000;//milliseconds\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":600,"wires":[["728a5b4a.3b7ed4"]]},{"id":"ef917b3f.a5bfa8","type":"function","z":"bcbdd3c91c32b3d6","name":"iterations--","func":"//flow.set('iterations',flow.get('iterations')+1);\n//msg.iterations=flow.get('iterations');\nmsg.iterations=msg.iterations-1;\n//msg.delay=flow.get('delay');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":380,"wires":[["ba607e41.91714"]]},{"id":"61929d7.3a82f64","type":"comment","z":"bcbdd3c91c32b3d6","name":"TEST MANUALLY","info":"Iterations \nDelay in milliseconds","x":280,"y":560,"wires":[]},{"id":"c934a3dcf8cb04b1","type":"function","z":"bcbdd3c91c32b3d6","name":"","func":"msg.end=Date.now()-msg.start;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":420,"wires":[["7e5967.286e8698","4ffb1cabee37bbd2"]]},{"id":"4ffb1cabee37bbd2","type":"debug","z":"bcbdd3c91c32b3d6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1030,"y":360,"wires":[]}]

Flow Info

Created 2 years, 5 months ago
Updated 2 years, 4 months ago
Rating: 1 1

Owner

Actions

Rate:

Node Types

Core
  • comment (x1)
  • debug (x2)
  • delay (x1)
  • function (x4)
  • http in (x2)
  • http request (x1)
  • http response (x2)
  • inject (x1)
  • switch (x1)

Tags

Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option