I would like to "do some stuff" in my flow when NR is about to be stopped. Is there any event or so that I could use in a function node to get notified for this purpose. I saw in the api @node-red/runtime there is the function stop() but how can I catch the returned message in a function node? Are there any possibilities to subscribe to some callback or event in a function node that NR "is about to be stopped"?
If the runtime is stopped, it could also be a non-graceful exit.
Would an external watchdog that watches the node-red process be more helpful to perform some actions ? Or are the actions node-red related ?
My idea is that this is for a planned shutdown. There are maybe certain things that I want the flow to do at the end so to say, so a kind of pre-warning, now shutdown "will happen shortly". I also would like to inform external (Python, what ever) processes or other instances of NR that the "main" NR is shutting down
I think the way to do this is to get Node-RED to initiate the shutdown itself by doing exec call to the OS
I think I would do this the other way around.
Create a "graceful shutdown" flow which sends an MQTT msg and then waits for a period of time before shutting down Node-RED (or the device depending on what you are trying to achieve).
Then other flows can subscribe to that topic and can take action knowing the time period they have to work with.
Yes, this I have thought about as well and it is pretty straight forward, even make it react to incoming MQTT msg's like "shutdown now" or whatever
But it does not cover those cases where you stop NR via the node-red-stop script or via systemd. In addition it will not cover situations where NR is restarting by itself or by systemd, at least I have seen this happen from time to time (why this happens is for me still unknown). So I think NR itself should have such a message that is activated like "shutdown pending"...
If it is apparently restarting itself then there is a strong possibility that it has crashed and systemd has restarted it. In which case your suggestion would not help.
Looking in syslog should enable you to see whether this is the case, I think.
But once you've got to that stage, shutdown would be imminent, and you wouldn't have enough time to do anything further anyway.
Well, I was thinking NR would offer a slight delay between the msg and the actual shutdown
You could modify the systemd script so that when shutting down, rather than killing node red it runs a script that sends the shutdown message via mqtt. With a timeout so that if that doesn't work then eventually it kills it.
There would be a slight delay, but once it starts the shutdown process is it likely to start a new process running, like doing the functions which you want it to do.
I can only see this working if the shutdown is initiated by a flow, starting with an inject node or dashboard button, and ending with an exec node calling the shutdown.
Between the two, you could have any number of different functions, like broadcasting a MQTT message, displaying a pop-up, adding a delay, etc.
OK, I guess, I will do some experiments
Thank you all for ideas and suggestions!
As others have intimated, you need an external monitor for this, Node-RED itself will not be in a position to know what is going on.
One quick way to help create a monitor is to use MQTT with a heartbeat from Node-RED and an LWT. That way, other apps can know whether Node-RED is working or not and take other actions. Though this doesn't help you to do some quick tidying inside Node-RED but I don't think that anything will help you do that if Node-RED is failing for some reason.
With an external monitor, perhaps you could save some flags to a file that your Node-RED flow would read on startup. Then you could possibly do some tidying on restart instead?
It is really hard to give a definitive answer here as we don't really know exactly what you need to clean up.