Giter Site home page Giter Site logo

jobs-queues's Introduction

jobs-queues

Plugin that gives you sequential queues for your jobs as Array-like Objects in Express style

Installation

npm i jobs-queues

Test

npm test

Usage

const jobsQueues = require( 'jobs-queues' );
const queue = jobsQueues();
const jobList = queue.push(                             //Push a job list and return emitter
                                                        // Any job
    ( finish ) => {
        setTimeout( () => {
            console.log( 'First job finished' );
            finish( true, 'Hello world!' );             // At the end call finish() 
        }, 2500 );
    },

    ( finish, empty, ...results ) => {
                                                        // Another job
        if ( results[0] ) {
            finish( results[1] );                       // Call finish() with your result
        } else {                                        // OR
            empty( new Error( 'Some message' ) );       // Stop this job list
        }
        console.log( 'Second job finished' );
    },
    async ( finish, empty, result ) => {
                                                        // Last job in the job list
        finish( result );                               // Call finish() and emit "end" passing result
    }
);
const jobList2 = queue.push(
    ( finish, empty ) => {
        finish( 'Another job list' );
    }
);

jobList.on( 'end', data => console.log( data ) );
jobList.on( 'error', err => console.log( err ) );
jobList2.on( 'end', data => console.log( data ) );
jobList2.on( 'error', err => console.log( err ) );

Constructor

jobsQueues( started = true );

Parameters

started Default true - Set to false if you want to start your jobs later

Return

A JobsQueues instance that extends Array

Methods

push

queue.push( ...jobList: Function );

Parameters

jobList Optional - Any function that accepts three parameters:

  • finish Required - A callback you have to call at the end of every job. It accepts ...results and pass them to the next job in the same job list or to the end event of the job list emitter
  • empty Optional - A callback you have to call to stop all the jobs in the same job list and to emit an error
  • ...results Optional - Any result yo have passed in the finish() of the previous one job in the same job list

Return

An emitter related to the job list that can emit:

  • end If the last job in job list call finish()
  • error If empy() was called or if an error occurs

start

queue.start();

Note

A job list is not directly related with errors of another. It runs in anyway when the previous one exits.

jobs-queues's People

Contributors

angy91m avatar

Watchers

 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.