responsible code hotswap for zero downtime deploys
Safe code hotswap for zero downtime re-deploys.
An application in production should not need to be stopped in order to utilize new code.
liveswap is a library that helps you update an application’s code without stopping it. It also ships with a command-line tool and it doesn’t impose any special requirements or conventions on your application code.
It works by creating a light-weight master process that runs your application code as worker processes. It then starts listening for instructions.
When you send an upgrade instruction, workers take turns disconnecting their underlying
servers. When a server is disconnected, it lets the old connections finish up, but no
new connections are accepted. When all of the worker’s connections have been closed, it
will be retired and a new one will be created using the new code. For more in depth
this blog post.
npm install liveswap -g
Using the node.js hello world example, in a filed named
var http = require'http'httpcreateServerreswriteHead200 'Content-Type': 'text/plain'resend'Hello World\n'listen1337 '127.0.0.1'console.log'Server running at '
Start the application using liveswap from the command-line...
liveswap -s index1.js
Alternatively, start liveswap programmatically...
var liveswap = require'liveswap'liveswap'./index1.js'
Now we have an updated version of the code in
var http = require'http'httpcreateServerreswriteHead404 'Content-Type': 'text/plain'resend'Not Found\n'listen1337 '127.0.0.1'console.log'Server running at '
Update the current code by sending an update command to the server.
liveswap -u /index2js
After sending an update message the worker processes will wait for any current connections to end before restarting with the new code.
Options:-p, --port <port> specify the liveswap server port. [default: 3000]-a, --address <address> specify the ip address to run on. [default: "127.0.0.1"]-f, --forks <number> specify how many worker processes to spawn [default: 2]-u, --upgrade [<path>] specify the source code to upgrade to.--pre-upgrade <path> a module to handle pre upgrade logic.-k, --kill kill all forked worker processes and respawn.-d, --die kill all forked worker processes and quit master process.-m, --message <message> send a message to all the forked worker processes.-s, --start <path> start a node process cluster.-H, --head <path> path to HEAD file-z disable zero-downtime, upgrade will kill processes-v, --version print program version and exit
Pre-upgrade allows you to require a module that will be executed before each time the upgrade happens.
liveswap --pre-upgrade ./pull.js --start ./index1.js
The pre-upgrade module should export a single function as its interface. Here's an example of what that module might look like:
console.log'executing pre-upgrade script...';var err value = datavalue;try// execute pre-upgrade codecatch eerr = etoString;callbackerr value;moduleexports = preupgrade;
The main export of this library accepts an options object or a string. If a
string is specified, it will be interpreted as the
liveswaptarget: './index.js'port: 9008address: '0.0.0.0'forks: 2head: '/tmp/HEAD''pre-upgrade': './pull.js''zero-downtime': false