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

outstanding

v1.1.0

Published

A task register for facilitating graceful shutdown

Downloads

33

Readme

Outstanding

A task register for facilitating graceful shutdown.

NPM version NPM downloads Build Status Code Style Dependency Status devDependencies Status

In the world of continuous deployment applications are being started and stopped more frequently than ever. Stopping a process is normally achived by sending a 'SIGINT' or 'SIGTERM' signal. Your node application can listen for this and perform a gracesful shutdown by stopping listeners and closing connections, ensuring that new http requests are rejected, but in-flight ones can complete. But what happens to asynchronous processes that are not part of a request/response workflow, e.g.

setInterval(() => {
    fs.writeFile('file.txt', res.body, function(err) {

        // If SIGINT or SIGTERM is triggered now your application will shutdown
        // before the file is deleted

        fs.unlink('file.txt', function() {
            console.log('done')
        })
    })
}, 5000)

In truth there's nothing you can do to completely prevent this scenario. SIGKILL will kill your application instantly and cannot be listened for, but since most deployment mechanism try SIGTERM first and wait for a bit before invoking SIGKILL why not be a good citizen and wait until the asynchronous operations are complete?

This is where outstanding comes in handy. Outstanding assists with graceful shutdown of node.js applications by providing a way to track asynchronous tasks and block shutdown until they complete or an optional timeout expires.

TL;DR

const Outstanding = require('outstanding')
const outstanding = new Outstanding({ timeout: '5s' })
const signals = ['SIGINT', 'SIGTERM']
signals.forEach((signal) => {
    process.on(signal, () => {
        outstanding.shutdown((err, incomplete) => {
            if (err) {
                console.log(err.message, incomplete)
                process.exit(1)
            } else {
                process.exit(0)
            }
        })
    })
})

outstanding.run(someAsynchronousTask, (err, result) => {
    if (err) console.error(err)
    else console.log('The result of some asychronous task was', result)
})

Advanced Usage

Wrapping tasks

Sometimes you want to pass a function wrapped with outstanding to an executor.

var wrapped = outstanding.wrap(someAsynchronousTask)
async.times(3, wrapped)

Labeling tasks

When you run or wrap an asynchronous task, outstanding keeps track of the function name so you can at least log incomplete tasks on shutdown. If you use anonymous functions or prefer a custom label you can add an extra argument to both these functions, e.g.

outstanding.run('my label', someAsynchronousTask, (err, result) => {
    if (err) console.error(err)
    else console.log('The result of some asychronous task was', result)
})

Getting current tasks

Just call outstanding.list(). This will return a map of tokens to task details, e.g.

{
  "74a16a3b-129a-4921-bb71-897f2b6e64b7": { "name": "foo", "registered": 1472713271233 },
  "9e351837-421d-4785-bf7a-93f0aeed51b1": { "name": "bar", "registered": 1472713349045 }
}

Checking whether outstanding has been shutdown

Use outstanding.isClosed(). Returns false until outstanding.shutdown(cb) has been invoked, after which it returns true

Low Level API

The simplest way to use outstanding is to run or wrap tasks, but you can also use the api synchronously and asynchronously. In this case you need to explicitly use a label.

Asynchronous Usage

outstanding.register('my label', (err, token) => {
    if (err) return console.log('Cannot register task - the system is shutting down')
    someAsynchronousTask((err, result) => {
        outstanding.clear(token, () => {
            // Clear never returns an error
            if (err) console.error(err)
            else console.log('The result of some asychronous task was', result)
        })
    })
})

Synchronous Usage

const token = tasklist.register('my label')
if (token) {
    someAsynchronousTask((err, result) => {
        taskList.clear(token)
        if (err) console.error(err)
        else console.log('The result of some asychronous task was', result)
    })
} else {
    console.log('Cannot register task - the system is shutting down')
}