I believe the subject already says it really.
I'm running latest Node-RED release with Node 18 and I just finished a big bunch of code that is supposed to run whenever I deploy, NR crashes (luckily rarely happens) or when I have to reboot the underlaying server.
For me it seems like sending messages from the new code that I added to the "On Start" tab of a function node is only executed during deploy and flow restart but it's not executed when the whole nodered.service starts up.
Is that possible?
Simple flow.
It should show both in the debug window and also node status.
I added a delay to make 100 % sure I don't miss anything during start and the Web UI is surely fully initialized after 15 seconds.
So this doesn't fire anything when I do a systemctl restart nodered.service
It should & probably does, however, there may be a timing issue due to attempting to "send" a message before the other nodes in your flow have initialised. It may even be sent but
the simplest way is to use an inject set to trigger on startup to perform initialisations.
NOTE: I forget whether the on start code is even permitted to send messages
to prove what I am saying, do an node.error('testing 123') in the On Start function - you will see this in the Node-REd console output.
node.done is not needed (and it doesnt do anything without (brackets) anyhow) return is unnecessary if it is the last statement
So I did a test using:
let m = {};
m.payload = "Test Payload";
node.send(m);
node.error('deliberate node.error');
And sure enough i DO get a message in the console log.
However the results are mixed, as I expected.
FULL Deploy
The debug node does NOT receive a message
Modify func node then do a NODE ONLY deploy - the msg is sent.
recreate the function AFTER all other connected nodes, deploy (Full or Node only) and it DOES send
Proving that the on start occurs in order of node creation (the order the node is stored in the flow)
This, is not a reliable way to use "On Start" IMO. As i said before, to ensure all nodes are initialised and running, you should use an inject (as the inject wont fire untill the "Flows Started" message (i.e. all nodes are initialised)
Thanks for that! I forgot the brackets only with the testing node.
I hardly ever use return msg since I mainly send async messages so I use node.done() and return in several places in the code and adding it here is just because of a habit.
Wow, many thanks for that!
Honestly this makes it completely unreliable for me but since I always do partial deploys and hardly ever any service restarts I actually never noticed that "On Start" only works for sending messages in a predictable way during partial deploys (if I understood your post correctly).
So, thanks again, that means I will stop using that tab completely for sending any messages. I can't remember reading anything about this limitation in the documentation so I just thought it was a convenient way.
I think it isn't that the sending doesn't work, but that in a full deploy, other nodes may not have been initialised and so are not ready to receive messages, so the message gets lost. When you do a partial deploy of, for instance, the function node, but the other nodes are already deployed and waiting for messages, then it works.
Exactly how I understood the post of @Steve-Mcl .
The problem is since there is no order displayed the only way to check would be to into the JSON export but that would be such a pain in the backside that I rather stop sending any messages via "On Start".
It was mentioned in a previous post by @knolleary, that the initialization order is always tab by tab, left to right (and within a tab, by node creation order). I believe if you put your startup node on the last (rightmost) tab, it will ensure all the other nodes are initialized by the time it sends its startup messages.