A question about freeing resources in on('close')

I am developing a node that will spawn a process which may run for minutes, so in on('close') I should kill the spawned process. The complicating factor is that if the node is sent multiple messages it will spawn a process for each message, therefore I have declared the variables relating to each process (pid etc) as local variables in on('input'). The result is that I don't have access to those variables in the close function. Is there some neat trick I can use to get over this or do I need to do something like storing them in an array at the node level and keeping the index in the input function. I can see that getting a bit involved to manage.

Well you need to make sure you do have access to them :-). You could save them in a variable that is global (within your node) or attach it as a property to the node. Eg node.mypids. Etc

Yes, that is what I was thinking of, but I can't work out the best way to organise the data. I have only recently started using Javascript. For example, if I use an array and push the new ones on the end and set old ones to null (or something) the indexes will get bigger and bigger over time, but does the fact that js uses sparse arrays mean that doesn't matter as the ones I null out won't actually exist any more. Or perhaps there is a better way. In C++, for example, I would use a linked list of objects.

[Edit] OK, I see the Exec node does a similar thing, I am looking at how it does it....

I'd possibly use an object & dynamic properties. E.g.

//Create a property using pid as Property name
node.pids[pid] = process;//store the child process obj as a property keyed by pid 

Then you can easily look up a child process...

var proc = node.pids[msg.pid];

and when it's finished with, you can delete it...

delete node.pids[pid];

Other tips....

//Get & kill all processes
var pids = Object.keys(node.pids);
for(var p in pids){
  var child = node.pids[p];
  child.kill();
}

Yes just use an object not an array, with a suitable key. Maybe “P”+ pid or something easy (but javascript friendly). As you will want to also remove them when they complete normally.

Javascript objects need strings as keys (I know, you can also use integers).
If you don't want to invent a string key for storing that stuff, you can have a look at
new Map() or new Set(), which can take arbitrary objects as "keys" (i.e. you could save the process directly in the set) and allows nice iteration without having to worry about hasOwnProperty or using Object.keys(), and it has functions for deleting etc. (If you are a c++ guy, they are pretty similar to some of the std containers)

Just saying, the method with the standard object Steve and Dave posted are of course also valid.

Yes, that is how the exec node does it, obvious when you know how, as are many things.
Thanks all.

1 Like

Does it? that was of the top of my head, on my mobile, 3 beers in. Great minds must think alike :wink:

1 Like

...Fools seldom differ ... :stuck_out_tongue_winking_eye:

3 Likes

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