Dynamic Change to [Inject Node?] Repeat Interval?

I have an Inject node set to repeat every minute. This works fine when the "looping" flow doesn't need to do something that takes longer than around 20 seconds to complete. However, when the overall flow has not completed in time this causes problems the next time it is called (as some states are not as anticipated).

I considered putting a conditional delay so that the next loop is delayed after it's triggered, but this would only cause problems to mount up as AFAIK the inject trigger would be fired even when the previous run has not completed.

Obviously, I could permanently lengthen the repeat interval (and AFAIK I can't do this dynamically), but is there a better way to manage such a 'loop'?

Have a look at the CRON+ node, it should let you do what you want.

Ah yes! Thank you!

There are various gate nodes that you can use to throw away any injects until your processing is complete.

An example using node-red-contrib-simple-gate:

[{"id":"f953b3eb54ae90f8","type":"inject","z":"ebe721b4236762ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":140,"wires":[["e3ffef82238a93a0"]]},{"id":"e3ffef82238a93a0","type":"gate","z":"ebe721b4236762ee","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":330,"y":140,"wires":[["26e67434989ae055","c9316a8dc6c3de34"]]},{"id":"26e67434989ae055","type":"change","z":"ebe721b4236762ee","name":"Close gate","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":200,"wires":[["e3ffef82238a93a0"]]},{"id":"c9316a8dc6c3de34","type":"delay","z":"ebe721b4236762ee","name":"Some prolonged processing","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":600,"y":140,"wires":[["248644a4404e4826","61b80d77d0769562"]]},{"id":"61b80d77d0769562","type":"debug","z":"ebe721b4236762ee","name":"debug 319","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":140,"wires":[]},{"id":"248644a4404e4826","type":"change","z":"ebe721b4236762ee","name":"Open gate","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":80,"wires":[["e3ffef82238a93a0"]]}]

Thanks. That looks like it might be a more straightforward way of doing what I need.

The best solution (in an event based env like Node) is "dont loop". There is almost always a better way.

1 Like

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