Giter Site home page Giter Site logo

codelusion / concurrency-queue Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 11 KB

A lightweight, minimalist FIFO queue that holds jobs, releasing only a fixed number for concurrent processing

Home Page: https://www.npmjs.com/package/concurrency-queue

License: MIT License

JavaScript 100.00%
concurrency-queue throttling fifo-queue concurrent-queues concurrent-processes

concurrency-queue's Introduction

concurrency-queue

A lightweight, minimalist FIFO queue that holds jobs, releasing only a fixed number for concurrent processing.

Usage

Install

    npm install concurrency-queue

Use

Use .createInstance() to setup a concurrency queue with maxConcurrency parameter.

var cQ = require('concurrent-queue')
            .createInstance({
                'maxConcurrency': 3
            });

Then push a job into the queue:

    var jobId = cQ.push(job)    

The queue generates a unique jobId for each job instance pushed into the queue.

Events

Ready Event

The 'ready' event is fired when a job is released to be processed based on the maxConcurrency rules.

cQ.on('ready', function (job, jobId, status) {
    //Do some job processing here
    
    //when complete, drain the queue of the job
    cQ.drain(jobId); 
});

The status field shows current state of the queue: { maxConcurrency: xx, processing: xx, queued: xx }.

When job processing is completed, use .drain(jobId) to drain queue of job, releasing next job in queue, if one exists.

Queued Event

The 'queued' event is fired when a job is queued i.e, max number of concurrent jobs already being processed.

cQ.on('queued', function (job, jobId, status) {
    //job with jobId has been queued
});

Drained Event

The 'drained' event is fired when a job is drained from the queue. This releases the next queued job, if any are available.

cQ.on('drained', function (job, jobId, status) {
    //job with jobId has been drained from the queue
});

Empty Event

Finally an 'empty' event is fired when the last job is drained from the queue.

For examples see the /examples/ folder.

License:

MIT - See LICENSE file.

concurrency-queue's People

Contributors

codelusion avatar

Stargazers

 avatar

Watchers

 avatar  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.