# Simple test node, close event never called?

**URL:** <https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897>\
**Category:** General\
**Created:** [24 June 2020 15:34 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897 "2020-06-24T15:34:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [24 June 2020 15:34 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/1 "2020-06-24T15:34:16Z")

</div>

I created a simple node example, just to test input and close events. The close event never seems to fire? Did I do something boneheaded here? The node works as expected, it get the input notification in log, but never seem to get the close notification in the log. Send works, in that I get output in debug as expected.

```auto
        function PiEnvironmentNode(config) {
                RED.nodes.createNode(this, config);
                var node = this;
                 //
                node.on('input', function(msg, send, done) {
                        //
                        try {
                                //
                                node.log('Input');
                                node.status(BLUE);
                                //
                                msg.payload = 'Pi Enviornment Node Under Construction';
                                send(msg);
                                //
                                setTimeout(() => { node.status(RESET); }, TIMEOUT);
                        } catch (theError) {
                                //
                                node.status(FAIL);
                                node.debug(theError);
                                if (done) {
                                        done(theError);
                                }
                        }
                });
                //
                node.on('close', function(done) {
                        //
                        node.log('Close');
                        node.status(RESET);
                        done();
                });
        }
        RED.nodes.registerType("pi-environment", PiEnvironmentNode);

```

---

<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:** [24 June 2020 15:36 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/2 "2020-06-24T15:36:34Z")

</div>

If you modify the node in the node-red editor then deploy (or do a full deploy) you should get a close event.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [24 June 2020 15:54 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/3 "2020-06-24T15:54:36Z")

</div>

Ok, maybe I don't understand the event model right? I got the close event on a full deploy I explicitly did. Thanks for the help. But if I just do an inject to my node, I will not get the close event every time? Oh... because the node is not deleted and recreated explicitly, right? The inject is just another input event.

New question then... is it required to call done, per input event? Per the documentation " The **done** function must be called when it has **finished** handling the message. It takes one optional argument, an error object if the **node** has failed to handle the message for some reason." I read this to include the input event. I don't see that in the examples I have seen thus far... using done seems to be still rare as the new methodology.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 June 2020 16:00 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/4 "2020-06-24T16:00:13Z")

</div>

> [@Nodi.Rubrum](#):
>
> New question then... is it required to call done, per input event?

Yes. The input event is triggered when a node is passed a message. The handler is called, it does its work and when it has finished processing the message it must call the `done` function it was given.

It is true to say this new API style is not yet wide-spread. We're still working through the core nodes to add it in.

The `close` event is only triggered when the flows are stopping (because the runtime is stopping, or a new set of flows are being deployed).

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [24 June 2020 16:04 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/5 "2020-06-24T16:04:47Z")

</div>

Excellent! Again thanks. Really trying to get some good habits set here... been a solution designer, integrator, and coder at times for many years, but new to node-red. If it is a scripting language, I likely have used it at some point! Even HyperCard on Macintosh. (Don't tell anyone I know C, real C, as well... Does that date me? Oh well!).

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 June 2020 16:06 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/6 "2020-06-24T16:06:01Z")

</div>

how do i handle done events for something that works like the daemon node? so it starts a process and that process handles input and output. Do i call done when the long running process finishes or every time the process has an output in response to the message?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 June 2020 16:14 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/7 "2020-06-24T16:14:39Z")

</div>

> [@JGKK](#):
>
> Do i call done when the long running process finishes or every time the process has an output in response to the message?

You should only call `done` _once_ for a given message. When you call it will depend on what you think makes sense for the end user.... I think we're still figuring out for these sorts of edges cases what makes the most sense.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [24 June 2020 16:20 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/8 "2020-06-24T16:20:47Z")

</div>

The way I have my code setup right now, testing, evaluating design is along the following:

input-\>do something-\>success-\>send-\>done  
input-\>do something-\>error-\>handle error-\>done  
close-\>done

In a Sync or Async model the above is still consistent, say using spawned tasks. The only real difference with Async is if you have a timeout scenario.

input-\>do something-\>success-\>send-\>done  
input-\>do something-\>error-\>handle error (non fatal resolve)-\>send-\>done  
input-\>do something-\>error-\>handle error (fatal notify)-\>send (if applicable at all)-\>done  
input-\>do something-\>timeout-\>handle timeout-\>send (if applicable at all)-\>done  
close-\>done

A daemon is just a logical loop, persistent entity, that handles each input in turn.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 June 2020 16:26 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/9 "2020-06-24T16:26:34Z")

</div>

But what if its not even if there is more than one output per input msg to the child from the child? Should you make the done dependent of a timeout? What if a message arrives from the child after that timeout but before a new input?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 June 2020 16:39 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/10 "2020-06-24T16:39:46Z")

</div>

I believe you should not call done till all actions initiated by the input message are complete. Some nodes can never call done because the action is indefinite. Done is not important to the node-red system it is for the benefit of the user. For nodes where it is not relevant to call done then the user will presumably not be trying to catch it.

It was originally introduced for nodes like the e-mail node, so that the user could tell when the message had been sent (or failed to be sent).

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 June 2020 16:44 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/11 "2020-06-24T16:44:22Z")

</div>

But having said that... once we have a larger critical mass of nodes that support `done`, then we will be able to introduce features like graceful shutdown of flows - where the runtime is able to stop accepting 'new' work, but allow existing work to complete before stopping everything (without appropriate timeouts for things that don't behave).

In the case of a long running process that isn't expected to stop - it may be that calling `done` once the process has successfully started is semantically the right thing to do.

As I said, I think we're still figuring this out a bit.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 June 2020 16:57 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/12 "2020-06-24T16:57:28Z")

</div>

Yes I see, so for ongoing action nodes then call done to say 'OK, got that, if you need to shut me down then any time is fine'.  
This thread has come at a convenient time as it has reminded me that I need to implement done in a new node I am developing, it is the first node I have created since the new feature was introduced.

---

<div class="post-metadata">

**Author:** ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)\
**Post date:** [24 June 2020 17:15 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/13 "2020-06-24T17:15:20Z")

</div>

I keep reminding myself done is like memory cleanup in a heap... you do done when you are done... daemon or long loop process that handles many messages... is not done until fatal error or controlled exit.

---

<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:** [23 August 2020 17:15 UTC](https://discourse.nodered.org/t/simple-test-node-close-event-never-called/28897/14 "2020-08-23T17:15:30Z")

</div>

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