npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

liveswap

v0.4.3

Published

responsible code hotswap for zero downtime deploys

Downloads

37

Readme

SYNOPSIS

Safe code hotswap for zero downtime re-deploys.

BUILD STATUS

Build Status

MOTIVATION

An application in production should not need to be stopped in order to utilize new code.

DESCRIPTION

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 information read this blog post.

USAGE

Install

npm install liveswap -g

Example

Using the node.js hello world example, in a filed named index1.js...

First Version (Sample Code)

var http = require('http')
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'})
  res.end('Hello World\n')
}).listen(1337, '127.0.0.1')
console.log('Server running at http://127.0.0.1:1337/')

Startup

Start the application using liveswap from the command-line...

liveswap -s index1.js

Alternatively, start liveswap programmatically...

var liveswap = require('liveswap')
liveswap('./index1.js')

Second Version (Sample Code)

Now we have an updated version of the code in index2.js...

var http = require('http')
http.createServer(function (req, res) {
  res.writeHead(404, {'Content-Type': 'text/plain'})
  res.end('Not Found\n')
}).listen(1337, '127.0.0.1')
console.log('Server running at http://127.0.0.1:1337/')

Live Update

Update the current code by sending an update command to the server.

liveswap -u ./index2.js

After sending an update message the worker processes will wait for any current connections to end before restarting with the new code.

CLI Options

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:

function preupgrade(data, callback) {
  console.log('executing pre-upgrade script...');

  var err, value = data.value;
  try {
    // execute pre-upgrade code
  } catch (e) {
    err = e.toString();
  }

  callback(err, value);
}
module.exports = preupgrade;

API

liveswap(opts)

The main export of this library accepts an options object or a string. If a string is specified, it will be interpreted as the target option.

[option] { target: <String> }

[option] { port: <Number> }

[option] { address: <String> }

[option] { forks: <Number> }

[option] { head: <String> }

[option] { 'pre-upgrade': <String> }

[option] { 'zero-downtime': <Boolean> }

liveswap({
  target: './index.js',
  port: 9008,
  address: '0.0.0.0',
  forks: 2,
  head: '/tmp/HEAD',
  'pre-upgrade': './pull.js',
  'zero-downtime': false
})