Node initialization order

Hi,
I need to guarantee that a specific function node initializes (its "On Start" code gets executed) before the construction/initialization of any other node in any other flow.

Will it be safe to assume that defining a special tab (containing a single function node) and placing it as the leftmost tab, will force this function node be the first to initialize?

If yes, can I also assume that placing this tab as rightmost, will initialize this node AFTER all the other nodes finished initializing?

Thanks

@knolleary

I believe that this is correct. Just bear in mind that as a mostly single-threaded and complex app with lots of async processing, assessing what order things get run in can be quite a challenge. Certainly in the earlier days of node-red though, we were told that the first tab would get executed first.

No, I don't believe so. But we would probably need confirmation from @knolleary.

In general, I believe that execution will happen in the order in which things are added to the flows file. But again, bearing in mind the caveat around async operations.

I think that any code in on start sections is executed when node-red initialises, I believe that would be before any of the flows start.

The basic logic is:

  1. flows are started in the order they appear in the editor
  2. within a flow, nodes are started in the order they were added to the flow

No. In a Function node, the 'on start' code is run when the node is started.

Ah, my bad assumption. Good to know.

Excellent, thank you!
This is exactly the assertion I was looking for.

But I'm not sure I understood your second comment, Nick:
Does node-red finish initializing all the nodes (with their 'On Start' sections) before starting the messaging? (else initialized nodes may be sending messages to yet-uninitialized nodes)

No. There are not a separate 'initialise' and 'start' phases. Nodes just get started. The 'on start' code of a Function is nothing special in the lifecycle of the node - it is code that gets run when the node is started, rather than when the node receives a message.

Understood, thanks.
Hence I will programmatically hold back unsolicited messages (e.g. auto-inject, incoming HTTP etc.) until I know their target nodes are ready (maybe set a flag in the rightmost tab).

Could be a nice feature request for the future - to enable global 'pre initialization' & 'post initialization' segments.

Thanks Nick.

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