# Context not updating according to flushInterval & Missing data

**URL:** <https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042>\
**Category:** General\
**Created:** [17 June 2022 07:14 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042 "2022-06-17T07:14:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Integration\_Chads](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Integration\_Chads](https://discourse.nodered.org/u/Integration_Chads)\
**Post date:** [17 June 2022 07:14 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/1 "2022-06-17T07:14:57Z")

</div>

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:

```javascript
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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 June 2022 09:14 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/2 "2022-06-17T09:14:09Z")

</div>

What happens if you change `cache: true` to `cache: false`

---

<div class="post-metadata">

**Author:** ![Integration\_Chads](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Integration\_Chads](https://discourse.nodered.org/u/Integration_Chads)\
**Post date:** [17 June 2022 10:03 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/3 "2022-06-17T10:03:19Z")

</div>

Thanks for the reply,

I don't want to do this as I will have to change the hundreds of `global.get()` calls and all their following code to use callbacks, and even nested callbacks. I have already started working on a MongoDB context, but fell into this asynchronous problem and decided to not use it.

I also don't know how to return and go to the next node from a callback, as returning from a callback does not return to the next node.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 June 2022 10:15 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/4 "2022-06-17T10:15:23Z")

</div>

From what I interpret the documentation saying ([Local Filesystem Context Store : Node-RED](https://nodered.org/docs/api/context/store/localfilesystem#options))  
`cache	Whether to cache context in memory. Default: true`  
If you set cache to true, (in settings.js) it stores it in memory. I’m wondering if this is causing what you are seeing.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [17 June 2022 10:58 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/5 "2022-06-17T10:58:07Z")

</div>

Try this;

```auto
contextStorage: {
    default: {
        module: 'localfilesystem',
           config: {
                    dir: '/data/',
                    flushInterval: '1'
                   }
          }
}

```

\*Note the speech marks around the flushInterval time period, that are missing in your post.

---

<div class="post-metadata">

**Author:** ![Integration\_Chads](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Integration\_Chads](https://discourse.nodered.org/u/Integration_Chads)\
**Post date:** [17 June 2022 11:22 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/6 "2022-06-17T11:22:15Z")

</div>

@zenofmud Yes you're right, though that will make the calls asynchronous as you can see [here](https://discourse.nodered.org/t/saving-context-variable-to-the-localfilesystem-immediately-on-change/28515/8).

@Paul-Reed Thank you, I tried it and it's still the same (I restarted Node-RED of course). The [documentation](https://nodered.org/docs/api/context/store/localfilesystem#options) shows it as an integer anyway since there are no quotes around it like the default of the `base` option.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [17 June 2022 18:21 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/7 "2022-06-17T18:21:25Z")

</div>

To test, I've changed my settings.js flushInterval to 1 second;

```auto
    contextStorage: {
      default: "RAM",
                RAM: { module: 'memory' },
                Disk: { module: 'localfilesystem',
                        config: {
                                flushInterval: '1'
                                },
                },
        },

```

...and injecting a timestamp every second into a change node, saving it to global context (test).  
Then 'tail' the context file, ` $ tail -F /home/pi/.node-red/context/global/global.json` and I get;

![log](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e6466b98b3de0e5a7c6663fe45ce73d7a2acf486.gif)

which shows the context data being written to disk every second.  
So, if this does not work for you, it at least eliminates issues with the settings.js entries.

---

<div class="post-metadata">

**Author:** ![Integration\_Chads](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Integration\_Chads](https://discourse.nodered.org/u/Integration_Chads)\
**Post date:** [21 June 2022 06:28 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/8 "2022-06-21T06:28:20Z")

</div>

Thank you, though it does not work for some reason.

Every other settings that I update does have an effect, so it is not me who does not know how to update settings.

Anyway, it's okay, I found a workaround - sending the needed variables through HTTP. It's not my favorite solution but it's fine.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 June 2022 07:57 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/9 "2022-06-21T07:57:11Z")

</div>

Are you editing the correct settings.js?

Look at your node-red start up output - it will state where the settings.js is that it is using.

Also, did you change the syntax like @Paul-Reed showed you (nested config object)? ...

```javascript
contextStorage: {
    default: {
        module: 'localfilesystem',
        config: {
            dir: '/data/',
            flushInterval: '1'
        }
    }
}

```

or ...

```javascript
contextStorage: {
    default: "Disk",
        RAM: { module: 'memory' },
        Disk: { 
            module: 'localfilesystem',
            config: { flushInterval: '1' },
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Integration\_Chads](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Integration\_Chads](https://discourse.nodered.org/u/Integration_Chads)\
**Post date:** [22 June 2022 08:47 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/10 "2022-06-22T08:47:06Z")

</div>

> [@Steve-Mcl](#):
>
> Are you editing the correct settings.js?

Yes, since changing other settings takes effect, including adding entries in `contextStorage`

> [@Steve-Mcl](#):
>
> nested config object

Ah, I did not pay attention to that! I am not using the filesystem anymore but will keep that in mind in case I will in the future, thank you everyone

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 August 2022 08:47 UTC](https://discourse.nodered.org/t/context-not-updating-according-to-flushinterval-missing-data/64042/11 "2022-08-21T08:47:58Z")

</div>

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