Self-modifying code in node.js, will the cluster work?

I ask about this since I don’t have the tool or time to test it right now, but the idea bothers me. I myself will answer this when I have time to play with him.

In node.js, how does require () work? Does it store the required function in memory? or make it read the file again?

Exemple:

launcher.js

var cluster = require('cluster');

if (cluster.isMaster) {
    cluster.fork();
    cluster.on('exit', function () {
        cluster.fork();
    }
}
if (cluster.isWorker) {
    var self = require('self_modifying.js');
    self.start()
}

As long as the self_modification.js function has a start () function, which is the "main" one, it can self-update by simply modifying its own source file and process.exit (0), and so reloading with this new code?

+1
source share
2 answers

To reply:

In node.js, how does require () work? Does it store the required function in memory? or make it read the file again?

node.js, , , , , . .: http://nodejs.org/api/modules.html#modules_caching

+3

@Tom Grant, . , , , ,

, self_modification.js

var cluster = require('cluster');

if (cluster.isMaster) {
    cluster.fork();
    cluster.on('exit', function () {
        delete require.cache[require.resolve('/full/path/to/self_modifying.js')];
        cluster.fork();
    }
}
if (cluster.isWorker) {
    var self = require('self_modifying.js');
    self.start()
}
+1

All Articles