Giter Club home page Giter Club logo

auto-version-switch's Introduction

What is auto-version-switch?

This module enables you to automatically deploy a new version of a node app without restarting the node process. It does this by forking the node app (using node's cluster module), checking the version, and reforking if necessary. This module ensures no loss of connectivity because it will run a new version of the app before killing the previous version.

How do I enable it in my app?

Let's say you have a JavaScript file, app.js, that runs a web server:

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200);
    res.end("Hello, world v1")
}).listen(process.env.PORT);

Executing node app.js runs the web server, but in production, you would want to keep multiple versions of the app in different folders and switch between them as needed. For example, you would set up a folder structure like this:

v1/
    app.js
v2/
    app.js
version.txt
...

where version.txt holds the version number that needs to be running.

With auto-version-switch, a change in version.txt automatically makes the correct version of app.js run, without any need for restarting the app, and without any loss of connectivity. It enables this by adding a small .js file that runs (and re-runs) the correct version of the app, ensuring that no loss of connectivity ensues due to the version switch.

For the above example, you just need to create another JavaScript file, runner.js, with the following code:

require('auto-version-switch')(run, fetchExpectedVersion);

function run(version, switchVersionIfNeededFunc) {
   require('./' + version + '/app.js').run(switchVersionIfNeededFunc);
}

function fetchExpectedVersion(callback) {
   var fs = require('fs');

   fs.readFile('version.txt', function (err, content) {
       if (err)
           callback(err);

       callback(null, content.toString().trim());
   });
}

And slightly modify app.js to use switchVersionIfNeededFunc:

var http = require('http');

exports.run = function(version, switchVersionIfNeededFunc) {
    http.createServer(function (req, res) {
        switchVersionIfNeededFunc(function (err) {
            if (err)
                console.error(err);
            res.writeHead(200);
            res.end("Hello, world v1")
        });
    }).listen(process.env.PORT || 3000);
}

// optional - ensure app.js runs as a standalone module
if (require.main == module) {
    exports.run(function(callback) {
        process.nextTick(callback);
    });
}

This example can be found in the demo folder of the module. Follow these steps to run the demo:

  1. Execute npm run demo.
  2. Browse to http://localhost:300 and view "Hello, world v1".
  3. Change version.txt in the demo folder to be "v2".
  4. Browse to http://localhost:300 and view "Hello, world v2". You may need to refresh two or three times before seeing the version change. Remember, no loss of connectivity means that the old app may be running a little while longer.

Reference

The module is a single function, which accepts two functions (some would call them strategies):

  • run(version, switchVersionIfNeededFunc, [options]): a function that runs your application.
  • fetchExpectedVersion(callback): a function that returns the version identifier that is expected to run.

run(version, switchVersionIfNeededFunc)

This function, which you supply, should run the app. It accepts two parameters:

  • version. The version that should run.
  • switchVersionIfNeededFunc(callback). Your app should call this function from time to time to check whether the version has changed. It will switch versions of the app if it finds that the current version of the app is different than the version it gets by calling fetchExpectedVersion. If it isn't, it calls the callback to continue running your app. If the expected and current versions are different, it will initiate the procedure that switches the app. In this case, it will still call the callback to ensure that the current request is being handled. If there was an error, the callback will be called with an error; otherwise, it will be called with undefined. Even if the function returned an error, you can continue with your app, because it may be a temporary failure.

fetchExpectedVersion(callback)

This function, which you supply, should call the callback with the expected version. This uses the standard node callback signature:

  • callback(err, version). Where err is the error (or falsy if there is no error) and version is the version returned. Note that the version can be any primitive type (string, int, etc.), as it is compared using !=== against version values returned by previous calls to this function.

auto-version-switch's People

Contributors

mfshaeffer avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.