# Persistent data not being saved

**URL:** <https://discourse.nodered.org/t/persistent-data-not-being-saved/5680>\
**Category:** General\
**Created:** [11 December 2018 15:12 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680 "2018-12-11T15:12:46Z")\
**Posts on this page:** 17\
**Page:** 1

<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:** [11 December 2018 15:12 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/1 "2018-12-11T15:12:46Z")

</div>

Using v0.19.5 script installed, on a Pi running Debian Stretch.

Persistence to disk generally works well for me, but noticed that `node-red-stop` does not initiate a flush to disk, and this has caught me out a few times now.

I know this has been raised before in NR Slack, but never resolved.

Is `node-red-stop` the preferred way to stop node-RED, or is there a better way, which would initiate a flush?

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 December 2018 20:25 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/2 "2018-12-11T20:25:11Z")

</div>

I have the same issue when I stop NR on a Macintosh by killing its process, so it may not be a matter of how it is started or stopped.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [11 December 2018 21:16 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/3 "2018-12-11T21:16:14Z")

</div>

Have a look to the doc:

> The default configuration for a file context store is to use the directory ~/.node-red/context, with caching enabled and writes to storage happening every 30 seconds.

> The flushInterval is provided to minimise wear on the underlying storage, such as on a Raspberry Pi’s SD card. **Note that if Node-RED is unexpectedly killed, any data that has not yet been flushed will be lost.**

---

<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:** [11 December 2018 21:26 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/4 "2018-12-11T21:26:34Z")

</div>

I'm not talking about Node-RED being unexpectedly killed, I'm referring to it being gracefully shutdown by `node-red-stop`.

When persistence was first introduced, it was said that stopping Node-RED would trigger a flush, which it does not appear to do.

As I recall - although maybe wrong! it was something to do with the Node-RED shutdown process, which should trigger the buffer to be flushed to disk, before completing the shutdown.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [11 December 2018 21:32 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/5 "2018-12-11T21:32:41Z")

</div>

> [@Paul-Reed](#):
>
> I'm not talking about Node-RED being unexpectedly killed, I'm referring to it being gracefully shutdown by `node-red-stop`.

I know, my link to the doc was for @drmibell talking about killing the process.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 December 2018 21:54 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/6 "2018-12-11T21:54:19Z")

</div>

@cflurin, sorry for the sloppy language. MacOS uses control-c or command-. to send a SIGINT to the foreground process. NR should be able to handle this.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [12 December 2018 08:19 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/7 "2018-12-12T08:19:24Z")

</div>

@drmibell,  
Ok, I think node-red handles `SIGINT`.

Search in red.js for `process.on`:

```auto
process.on('SIGINT', function () {
    RED.stop().then(function() {
        process.exit();
    });
});

```

You can add a RED.log to test it on your mac:

```auto
process.on('SIGINT', function () {
    RED.log.info('SIGINT');
    RED.stop().then(function() {
        process.exit();
    });
});

```

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 December 2018 09:14 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/8 "2018-12-12T09:14:20Z")

</div>

@cflurin Exactly right. Both SIGINT on a mac and `node-red-stop` on a pi should produce an orderly shutdown of NR. According to a previous [discussion](https://discourse.nodered.org/t/questions-about-persistent-context/5547/2),

> [@Questions about persistent context](https://discourse.nodered.org/t/questions-about-persistent-context/5547/2):
>
> the filesystem context plugin flushes its contents as part of its closing down

This does not seem to happen. @Paul-Reed observed that context that is supposed to persist does not, and I was just offering another example.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [12 December 2018 09:28 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/9 "2018-12-12T09:28:37Z")

</div>

@drmibell,  
Let's see what the node-red team think about that.  
Because of this in the node.js doc:

> SIGINT from the terminal is supported on all platforms, and can usually be generated with `<Ctrl>+C` (though this may be configurable). It is not generated when terminal raw mode is enabled.

I suggested to add `RED.log`.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2018 09:46 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/10 "2018-12-12T09:46:14Z")

</div>

Also the Pi systemd service is set

```
# Use SIGINT to stop
KillSignal=SIGINT

```

So node-red-stop should def be calling a SIGINT... which should be calling an orderly stop.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 December 2018 09:57 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/11 "2018-12-12T09:57:11Z")

</div>

Sorry for the diversion through Macintosh-land. I don't think there is any question that I'm getting a clean shutdown of NR. The console shows:

> 12 Dec 04:45:38 - [info] Stopping flows  
> 12 Dec 04:45:38 - [info] Stopped flows

Examining the files in `$HOME/.node-red/context` shows that they have not been updated.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [12 December 2018 10:13 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/12 "2018-12-12T10:13:46Z")

</div>

> [@drmibell](#):
>
> Examining the files in `$HOME/.node-red/context` shows that they have not been updated.

The same here:

```auto
Starting as a systemd service.
Started Node-RED graphical event wiring tool.
12 Dec 11:10:34 - [info]
Welcome to Node-RED
===================
12 Dec 11:10:34 - [info] Node-RED version: v0.19.5
12 Dec 11:10:34 - [info] Node.js version: v8.14.0
12 Dec 11:10:34 - [info] Linux 4.14.79-v7+ arm LE
12 Dec 11:10:35 - [info] Loading palette nodes
12 Dec 11:10:44 - [info] Dashboard version 2.12.2 started at /ui
12 Dec 11:10:45 - [info] Settings file : /home/pi/.node-red/settings.js
12 Dec 11:10:45 - [info] Context store : 'default' [module=localfilesystem]
12 Dec 11:10:45 - [info] Context store : 'memoryOnly' [module=memory]
12 Dec 11:10:45 - [info] User directory : /home/pi/.node-red
12 Dec 11:10:45 - [warn] Projects disabled : set editorTheme.projects.enabled=true to enable
12 Dec 11:10:45 - [info] Flows file : /home/pi/.node-red/flows_rpi4.json
12 Dec 11:10:46 - [info] Server now running at http://127.0.0.1:1880/
12 Dec 11:10:46 - [info] Starting flows
12 Dec 11:10:49 - [info] Started flows

```

---

<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:** [12 December 2018 10:55 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/13 "2018-12-12T10:55:36Z")

</div>

I just verified this (in OSX)

1. in terminal, start NR
2. create a simple flow (attached below) - an inject to two change nodes - each change node to a seperate debug node
3. set one change node to use a flow variable the other uses a global variable.
4. deploy and press the inject and use the 'context data' to examine the contents.
5. as soon as you see the timestamp in the context data, press the inject again and then swap to the terminal and press CTRL-C

If you go edit .node-red/context/global/flobal.json you will see the contents which you can compare against the debug. Here is what I see:

 ![50%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4c8e7217b595ef9d8d9bfb9939990e46f2559383.png)

![26%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1aa0596618ff3d727ffddde96f1659ec73c84491.png)

HERE SI THE FLOW:

```auto
[{"id":"78088df5.2fd3b4","type":"inject","z":"488e1c5.d552c64","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":200,"wires":[["54316793.93ec58","ecf6671d.852d8"]]},{"id":"54316793.93ec58","type":"change","z":"488e1c5.d552c64","name":"","rules":[{"t":"set","p":"timestamp","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":180,"wires":[["38182ee9.bf1872"]]},{"id":"ecf6671d.852d8","type":"change","z":"488e1c5.d552c64","name":"","rules":[{"t":"set","p":"timestamp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":240,"wires":[["35e6ac92.ee3d14"]]},{"id":"38182ee9.bf1872","type":"debug","z":"488e1c5.d552c64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":680,"y":140,"wires":[]},{"id":"35e6ac92.ee3d14","type":"debug","z":"488e1c5.d552c64","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2018 10:56 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/14 "2018-12-12T10:56:51Z")

</div>

Probably time to move this to an issue please.  
(with all that detail)  
It would also be useful to run with logging level set to debug as it should log when it flushes at debug level.

---

<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:** [12 December 2018 10:58 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/15 "2018-12-12T10:58:41Z")

</div>

Will do - by issue you mean on GitHub?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [12 December 2018 10:59 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/16 "2018-12-12T10:59:22Z")

</div>

yes on github

---

<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:** [12 December 2018 11:29 UTC](https://discourse.nodered.org/t/persistent-data-not-being-saved/5680/17 "2018-12-12T11:29:14Z")

</div>

done! here is the link [https://github.com/node-red/node-red/issues/2001](https://github.com/node-red/node-red/issues/2001)
