How to create a self-updating Node.js application?

Golo Roden picture Golo Roden · Dec 22, 2012 · Viewed 12.2k times · Source

I'd like to create an application with Node.js that periodically checks for updates and installs them if there are any.

The basic components are clear to me:

  • A web server (or an FTP server, a file system, ...) which contains the update packages
  • A version system (such as SemVer) so that you can tell which package is newer
  • A public-key algorithm for signing update packages

Then, there may be different strategies on when to check for updates and install updates:

  • On application start
  • On application end
  • When idleing

The application may even be shut down hard and restarted automatically.

But there are some questions left:

  • Is there any module on npm available that already provides such a system?
  • How to organize the different versions in the file system? Basically you'd have a host and multiple (versioned) cores. Should there be one data folder for all of them?
  • How to deal with npm install & co. for newly downloaded packages?
  • How to deal with broken updates?
  • How could you implement such a thing so that it's compatible to Heroku & co. where you don't have a permanent drive that you can drive to?

In general: How would you try to achieve a system like this?

Answer

Mike Bild picture Mike Bild · Dec 22, 2012

First, you could use NPM itself for package and app delivery. Second, you could add a script file to add a cron job in the package.json file (e.g. 'postinstall' or 'update') for a periodic update process. In this script you could check all you need like broken updates. In Heroku you can use a Scheduler Worker for update processing.