# Forcing context to file without restart?

**URL:** <https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371>\
**Category:** General\
**Created:** [8 April 2023 19:22 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371 "2023-04-08T19:22:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [8 April 2023 19:22 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/1 "2023-04-08T19:22:51Z")

</div>

Hi

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

```auto
    contextStorage: {
        default: {
            module:"localfilesystem"
        },
    },

```

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.

Thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 April 2023 19:43 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/2 "2023-04-08T19:43:23Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [8 April 2023 20:46 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/3 "2023-04-08T20:46:35Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [8 April 2023 20:48 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/4 "2023-04-08T20:48:35Z")

</div>

> [@Barbudor](#):
>
> Restarting NR would means that I'm going to loose my current context.

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

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [10 April 2023 14:47 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/5 "2023-04-10T14:47:18Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bda02adf6e796c9a3599164c31177ef49b12d833.png)

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.

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 April 2023 16:24 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/6 "2023-04-10T16:24:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [10 April 2023 16:52 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/7 "2023-04-10T16:52:32Z")

</div>

> [@TotallyInformation](#):
>
> This feels like you are manually trying to do with a flow what the `filesystem` context store already does

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.

> [@](#):
>
> If you are going to do anything, write a flow that copies the memory variable to a filesystem variable

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [10 April 2023 17:00 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/8 "2023-04-10T17:00:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Barbudor](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@Barbudor](https://discourse.nodered.org/u/Barbudor)\
**Post date:** [10 April 2023 17:03 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/9 "2023-04-10T17:03:18Z")

</div>

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

---

<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:** [9 June 2023 17:04 UTC](https://discourse.nodered.org/t/forcing-context-to-file-without-restart/77371/10 "2023-06-09T17:04:00Z")

</div>

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