Forcing context to file without restart?


I just uncommeted in my settings.js the block for saving context to filesystem

    contextStorage: {
        default: {

I understand the setting will not be enforced until next restart.
But is there's a way to force the writing of current (in-memory) context to file before that restart ?

If not, it means that I will loose one last time my context with that restart.


That module defaults to a flush interval of 30s so it really isn't that bad. You can change it too:

    contextStorage: {
        default: {
            module: 'memory',
        file: {
            module: 'localfilesystem',
            // config: {
            //     flushInterval: '10', // default is 30s
            //     dir: '/home/user/.node-red/context',
            // },

Hi @TotallyInformation

Thanks but I think you didn't get my question.

Currently my NR runs in default storageContext in memory.
I have hanged the settings.js but I haven't yet restarting NR to enfore the new setting.js where storing to filesystem is enabled.
Restarting NR would means that I'm going to loose my current context.

I am asking if there is any way, to force storage to filesystem just once before restarting and definitively enable storing to filesystem.
Or to retrieve the current context data easily to put it in files so on restart I will get my context back.

For now my best guess would be to add a small sub-flow in each flow to retreive the context, create an object and write it to file context/<flowID>/flow.json

Simply write it to a file to temporarily save it. Then restart and read it back in.

I added this small sub-flow in each of my flows to save the current context into a json file at the very same location and Nodered is expecting to find it on next restart. Runs every 30 seconds too.
So I shouldn't loose anything on next restart.

The change node set the payload with every flow context variable.
Payload is then converted to a JSON string and written into a file located at /data/context/<msg.flow_id>/flow.json where <msg.flow_id> has been set in the initial change node.

[{"id":"f24d8a57e205d6f8","type":"comment","z":"1eb7e7266b417749","name":"Save flow context","info":"","x":180,"y":60,"wires":[]},{"id":"f1bf7f8e80347c6d","type":"inject","z":"1eb7e7266b417749","name":"Save flow context","props":[{"p":"payload"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{}","payloadType":"json","x":210,"y":100,"wires":[["496b1eb33d801008"]]},{"id":"496b1eb33d801008","type":"change","z":"1eb7e7266b417749","name":"","rules":[{"t":"set","p":"flow_id","pt":"msg","to":"1eb7e7266b417749","tot":"str"},{"t":"set","p":"payload.last_mission_created","pt":"msg","to":"last_mission_created","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":100,"wires":[["15a05c6094adcfa7"]]},{"id":"8b978ae385275feb","type":"debug","z":"1eb7e7266b417749","name":"flow context file","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":100,"wires":[]},{"id":"1fa109fc41b2a0a4","type":"file","z":"1eb7e7266b417749","name":"","filename":"\"/data/context/\" & msg.flow_id & \"/flow.json\"","filenameType":"jsonata","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":700,"y":100,"wires":[["8b978ae385275feb"]]},{"id":"15a05c6094adcfa7","type":"json","z":"1eb7e7266b417749","name":"","property":"payload","action":"","pretty":false,"x":570,"y":100,"wires":[["1fa109fc41b2a0a4"]]}]

This feels like you are manually trying to do with a flow what the filesystem context store already does. If you are going to do anything, write a flow that copies the memory variable to a filesystem variable and change all of your flows to use that filesystem version, then you can delete the memory version.

Exactly, until NodeRed restart to enforce the change in settings.json to use the filesystem.
Just repointing what I wrote above:

  • I have made the change to settings.json to use filesystem
  • But I haven't yet restarted NR, so my instance is still using memory
    Now that I've done that, I can restart NR, which will enforce using filesystem, without loosing my currnet values.

Isn't it exactly what I did ?

My NR is currently running the default setting which only allows to save context in memory
I can't have another option until I restart NR, which I want to do without loosing my current values.

The system is in production, I can't afford to loose my current values

I hadn't realised that you hadn't been able to restart.

That's the point, I need to have the json files ready before the restart so I don't loose any data
So that's the trick I came with

Thanks for the follow up

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