0

In nodejs is there a mechanism for dynamically having code that checks for updates and then runs said updates?

For example, I've got a distributed computing network with dozens (and plan to expand to hundreds) of workers.

When I update the 'worker' code, in order to actively implement on all the worker systems, I need to go to each system and stop node, copy the updated file, and then restart node.

I was thinking I could wrap the entire thing in a wrapper that goes out and periodically checks for updates (or just gets them via an HTTP request from a master server). When it sees new code, it unloads the currently runny process, grabs the new code, and starts a new process with that code... but I'm unsure how to dynamically unload and reload code in nodejs. Perhaps a call to an exec?

Questions such as Loading remote js file using require with node.js are similar... but not exactly what I'm looking for - as those are only loading remote files on startup.

Guidance?

lowcrawler
  • 6,777
  • 9
  • 37
  • 79
  • would [Dynamic import](https://v8.dev/features/dynamic-import) be useful? – Jaromanda X Sep 20 '20 at 02:49
  • Would dynamic import allow 'unloading' previous imported modules? – lowcrawler Sep 20 '20 at 02:53
  • Implement CI/CD, the moment you push your code on git trigger deployment on node servers. – r7r Sep 20 '20 at 03:18
  • You cannot unload previously loaded modules in node.js. Once the interpreter has loaded their code, you can't "unload" it from memory. Generally, one will restart the node.js process to load new code. If you use clustering, you can shut down one server, upgrade it, start it up, shut down another, upgrade it, start it up, etc... and go without interrupting service. If you don't use clustering, you can manually bring up a second server, change a load balancer or proxy to now point at it, upgrade the first and reverse. – jfriend00 Sep 20 '20 at 03:35
  • A node wrapper using exec and process.kill (with apporiate checks) perhaps? Have the child process, itself, be the `node index.js` process? Is this so terrible as to be a non-starter? – lowcrawler Sep 21 '20 at 02:57
  • @r7r - How So it gets deployed to the workers via CI/CD ... how do I have the workers automatically run the new code? – lowcrawler Sep 21 '20 at 02:57
  • 1
    The moment it gets deployed, new code will run (technically new instance will start) also if you create your deployment scripts in manner their will be 0 downtime. – r7r Sep 21 '20 at 04:47
  • Thanks -- will look into this. Appreciated! – lowcrawler Sep 21 '20 at 05:16

0 Answers0