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

steppe

v0.2.3

Published

Simple processor for jobs that have multiple steps

Downloads

3

Readme

Steppe - a simple queueing system for linear jobs

Introduction

Sometimes, you have to process a task that consists of multiple steps, and that takes a bit longer than your normal read or write operation. It can be complex to handle such a task with a series of callbacks or promises, especially if you want to be able to retry individual steps in case of failure.

Add to that the problem of your node.js server restarting in the middle of such a long-lived task - for example because a new version of your code is being deployed - and you have a challenge to solve.

Steppe is a module that helps you perform such tasks. You can create jobs with any number of steps, and define handler functions to handle steps. The state of the job, including any amount of additional custom data, is stored in a database for persistence. Each step can be retried a few times before it's considered to have errored.

Installation

npm install steppe

Terminology

A step is a single task.

A job is a collection of 1 or more steps, which are processed in sequence.

A queue defines a type of job, with its accompanying handler functions. Each job that you create, is created in a queue. This determines which handler functions will be called to process each step.

Synopsis

    // You need 2 handler functions: one for each step, and one for when
    // the whole job has been finished.
    //
    // This is the step handler. It returns a promise to indicate
    // whether the step has been handled successfully or not.
    function stepHandler(data) {
        var stepData = data.stepData; // The data of this specific step.
        var jobData  = data.jobData;  // The data of the job. The same for each step.
        var action   = data.action;   // The action of the step.

        // Generate a promise with your favourite promise library.
        var promise = createPromise();

        // Perform the action necessary for this step.
        doStuff(action, stepData, jobData)
        .then(function(result) {
            promise.resolve();
        })
        .catch(function(error) {
            if (error === 'everything went wrong') {
                promise.reject({
                    fatal: true,
                    error: error
                });
            }
            else {
                promise.reject({
                    fatal: false,
                    error: error
                });
            }
        });

        return promise;
    }

    // This is the job finished handler. It is called either after the last step has been processed,
    // or after a step has errored in a fatal way.
    function jobFinished(job) {
        if (job.status === steppe.status.error) {
            console.error('Error: %s', job.error);
        }
        else {
            console.info('Job %s processed successfully', job.id);
        }
    }

    var Steppe = require('steppe');

    // One-time setup, at the start of your app.
    var steppe = new Steppe({
        db: {
            type: 'mongo',
            url: 'mongodb://localhost/testdb'
        },
    });

    // This defines a queue to create jobs in.
    steppe.defineQueue({
        name       : 'makePhotoAlbum',
        stepHandler: stepHandler,
        jobFinished: jobFinished,
        period     : 'every 20 seconds'
    });

    // Starts steppe. This means steppe will start looking for unfinished jobs
    // to process, for each queue that you have defined.
    steppe.start();

    // Now you can create jobs that will be handled.
    steppe.createJob({
        queueName: 'makePhotoAlbum',
        jobData: {
            url: 'http://photoalbums.example.net',
            auth_token: '1751FC0703C943D8904DAFCAD6F5814941CD1',
        },
        steps: [
            {
                action: 'createPhotoAlbum',
                stepData: {
                    albumName: 'My Pets',
                }
            },
            {
                action: 'uploadPhoto',
                stepData: {
                    filename: '/tmp/photo01.png',
                    description: 'My cat Mox',
                }
            },
            {
                action: 'uploadPhoto',
                stepData: {
                    filename: '/tmp/photo02.png'
                    description: 'My dog Iwan',
                }
            },
            {
                action: 'publishAlbum'
                stepData: { }
            }
        ]
    });