Node Context does not reset on redeploy

Hello,

I am scatching my head regarding the node context context.get and context.set

I am finding it does not get reset on a redeploy and the previous values remain.

I have changed my settings.js file to allow some context persistance, when I re-deploy after changing the associated node or even do a full re-deploy the previous values remain.

I changed my settings.js file to:-

contextStorage: {
default: {module: "memory" },
InFile: {module: "localfilesystem" },
},

and save using 'context.set('thermostat', thermostat)'
I assume as I am not specifying InFile it should be stored in memory and get re-initialsed after a change to the node, if thats not the case I am looking for a way to re-iniatalise these values when I make small changes

What version of Node-RED are you using?

No. Deploying a change does not reset context.

You could create a flow that sets your context values to undefined (which is how you delete them) and start it with an Inject node configured to fire on start.

1 Like

Hi
my version is 0.20.7.

I have done as reccomended by Nick, thanks. I have an inject node that ends a pulse on start and I set the context to undefined. I just need to remember to deploy any changes to the modified flows, rather than the modified nodes. Too be honest probably what I should be doing anyway to be sure of a proper restart.

Hi Nick,

No. Deploying a change does not reset context.
You could create a flow that sets your context values to undefined (which is how you delete them) and start it with an Inject node configured to fire on start.

ok for global and flow context, but how can do it for the local context inside the node?

Node context will be reset if a node is re-deployed. Obviously it doesn't get reset (and neither should it) if you do a partial deploy not involving that node.

Is it possible that there is a setting somewhere for this behaviour?
Or maybe there is something I don't understand properly.
I put a simple count in a function node, obtaining a certain value after a few inject

var count=context.get("count")||0;
count=count+1;
context.set("count",count);

If later on I modify the code inside this node, after redeploy the context count (locale variable) maintain the previous value. It's not "resetted".
For what I have understood this is because the local context has been already created and it's already existing . So If I want to reset it, I have to manually delete it. Only in this way I have the same behaviour as for the inizial loading, when this local context doesn't exist. And this is what I need.

[{"id":"361541f.23712be","type":"function","z":"a716f67a.bcc3d8","name":"Prova","func":"msg1={}; msg2={};\nvar count=context.get(\"count\") || 0;\nvar Alm_Status = context.get(\"Alm_Status\")|| false;\ncount=count+1;\nif (count!==0) {\n    switch (msg.payload) {\n        case false:\n            if (Alm_Status===true){\n                //do domething\n                msg1.payload = \"Reset allarme\"; \n            }\n            msg2.colour=\"#333333\";\n            context.set(\"Alm_Status\",false);\n            break;\n        case true:\n            if (Alm_Status===false){ \n                //do domething\n                msg1.payload= \"Alarm\"; \n                msg2.colour=\"#FF0000\";\n                context.set(\"Alm_Status\",true);\n            } else {\n                msg1=null;\n                msg2=null;                \n            }\n    }\n} else {\n    msg1 = null;\n    switch (msg.payload) {\n        case false:\n            msg2.colour=\"#333333\";\n            Alm_Status = false;\n            break;\n        case true:\n            msg2.colour=\"#FF0000\";\n            Alm_Status = true;\n    }  \n}\ncontext.set(\"count\",count);  \nreturn [msg1,msg2];\n","outputs":2,"noerr":0,"x":370,"y":300,"wires":[["adb90e0b.cbcd3"],["91f2127e.c24c4"]]},{"id":"adb90e0b.cbcd3","type":"debug","z":"a716f67a.bcc3d8","name":"DB1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":240,"wires":[]},{"id":"91f2127e.c24c4","type":"debug","z":"a716f67a.bcc3d8","name":"DB2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":320,"wires":[]},{"id":"1c9508de.3b3b27","type":"inject","z":"a716f67a.bcc3d8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":260,"wires":[["361541f.23712be"]]},{"id":"be51da04.0fcfe8","type":"inject","z":"a716f67a.bcc3d8","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":320,"wires":[["361541f.23712be"]]}]

Hi @Lupin_III

Colin was wrong to say that Node context will be reset if a node is re-deployed - it isn't.

You need to explicitly reset context yourself - you can do it manually using the Context sidebar by selecting the node and deleting the individual bits of node context you want to delete.

Ok, thanks. Could be really useful to have the possibility of setting this behaviour

if you are using in-memory context (the default) then stopping and restarting the server will flush it.

Just to make one small clarification:

if you don't have contextStorage configured, then the node context will get cleared on deploy. But if you do have contextStorage configured, even if it is the default memory-only store, then it won't get cleared.
https://nodered.org/docs/user-guide/context#context-stores

2 Likes

That explains my momentary confusion.