Hello!
The setup I have is 4 Node-RED instances, each one in a Docker container.
I want to share the global context between them, and therefore, in each instance's settings.js
file, I have set the following:
contextStorage: {
default: {
module: 'localfilesystem',
dir: '/data/',
cache: true,
flushInterval: 1
}
}
and I turned /data/global
into a Docker volume shared by all instances (for those not familiar with Docker, this just means that every instance's /docker/global
folder is the same - if once instance's folder is updated, they are all updated accordingly).
Upon using an exec node to execute cat /data/global/global.json
, I see that the file is indeed readable by all instances.
First problem - File does not update according to flushInterval
To make sure the file gets updated, I am executing global.set('_time', Date.now())
every second.
The first problem is that I noticed that the file /data/global/global.json
is updating every 30 seconds and not 1 as mentioned in flushInterval
.
Using an exec node to execute cat /data/global/global.json
, I see that the file is updated accordingly by all instances every 30 seconds as well.
Second problem - The context does not update on other instances
I have one "master" Node-RED container which is the one that sets most of the global context.
As mentioned previously, executing global.set('_time', Date.now())
successfully updates global.json
, and every Node-RED instance can read that file.
The problem is, when opening the "Context Data" tab on the other instances, it is empty (except for the values set by the itself).
If I execute global.set('_time2', Date.now())
every second in that instance, the content of global.json
starts to switch between the values of the contexts of the instances.
And this makes sense, but this wouldn't happen in the first place if the other instances were updating their contexts according to the global.json
file.
When I restart all the containers, they all get the same context. But of course, this is useless since after that, they become independent again.
Third problem - Sharing functions in context
In the context, I have a entry with functions inside. These are not written in the file (which makes sense), but I want to share them between instances.
I have found a solution for this problem: turning the functions into strings and then using eval()
in each instance to turn them back into functions. But it would be great to find an alternative.