Skip to content
/ queuing Public

Asynchronous function queuing with adjustable concurrency and re-queuing

License

Notifications You must be signed in to change notification settings

uniibu/queuing

Repository files navigation

________                      .__                
\_____  \  __ __   ____  __ __|__| ____    ____  
 /  / \  \|  |  \_/ __ \|  |  \  |/    \  / ___\ 
/   \_/.  \  |  /\  ___/|  |  /  |   |  \/ /_/  >
\_____\ \_/____/  \___  >____/|__|___|  /\___  / 
       \__>           \/              \//_____/  

A Forked module from queue with ES6 Syntax, and a retry feature

Asynchronous function queue with adjustable concurrency.

npm Build Status

This module exports a class Queuing that implements most of the Array API. Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods. Processing begins when you call q.start().

Install

npm install queuing

Test

npm test

API

var q = queuing([opts])

Constructor. opts may contain inital values for:

  • q.concurrency
  • q.timeout
  • q.autostart
  • q.results
  • q.retry
  • q.delay

Instance methods

q.start([cb])

cb, if passed, will be called when the queue empties or when an error occurs.

q.stop()

Stops the queue. can be resumed with q.start().

q.end([err])

Stop and empty the queue immediately.

Instance methods mixed in from Array

Mozilla has docs on how these methods work here.

q.push(element1, ..., elementN)

q.unshift(element1, ..., elementN)

Properties

q.concurrency

Max number of jobs the queue should process concurrently, defaults to Infinity.

q.timeout

Milliseconds to wait for a job to execute its callback.

q.autostart

Ensures the queue is always running if jobs are available. Useful in situations where you are using a queue only for concurrency control.

q.results

An array to set job callback arguments on.

q.retry

Put back the job in que upon error.

q.delay

Milliseconds to wait before job execution per session

q.length

Jobs pending + jobs to process (readonly).

Events

q.emit('success', result, job)

After a job executes its callback.

q.emit('error', err, job)

After a job passes an error to its callback.

q.emit('retry', err, job)

After a job passes an error and queued back to jobs list

q.emit('timeout', continue, job)

After q.timeout milliseconds have elapsed and a job has not executed its callback.

q.emit('end'[, err])

After all jobs have been processed

Example

var queue = require('queuing')

var q = queue()
var results = []

// add jobs using the familiar Array API
q.push(function (cb) {
  results.push('two')
  cb()
})

q.push(
  function (cb) {
    results.push('four')
    cb()
  },
  function (cb) {
    results.push('five')
    cb()
  }
)

// jobs can accept a callback or return a promise
q.push(function () {
  return new Promise(function (resolve, reject) {
    results.push('one')
    resolve()
  })
})

q.unshift(function (cb) {
  results.push('one')
  cb()
})

// use the timeout feature to deal with jobs that
// take too long or forget to execute a callback
q.timeout = 100

q.on('timeout', function (next, job) {
  console.log('job timed out:', job.toString().replace(/\n/g, ''))
  next()
})

q.push(function (cb) {
  setTimeout(function () {
    console.log('slow job finished')
    cb()
  }, 200)
})

q.push(function (cb) {
  console.log('forgot to execute callback')
})

// get notified when jobs complete
q.on('success', function (result, job) {
  console.log('job finished processing:', job.toString().replace(/\n/g, ''))
})

// begin processing, get notified on end / failure
q.start(function (err) {
  if (err) throw err
  console.log('all done:', results)
})

Releases

The latest stable release is published to npm.

  • 1.0.0

    • Initial fork
    • New reque option
  • 1.1.0

    • Removal of non-useful Array methods
    • Refactor test
    • Rename reque option to retry option
    • Add delay option
    • Emitter will only emit end event if the job que is empty on process.nextTick's event, approx 4-5 millisecond
    • Wrap all jobs inside a promise
  • 1.3.0

    • Update modules
    • Extend nodejs compatibilty to Nodejs 12
    • Use Travis

Releases from original module

The latest stable release is published to npm. Abbreviated changelog below:

  • 4.4
    • Add results feature
  • 4.3
    • Add promise support (@kwolfy)
  • 4.2
    • Unref timers on end
  • 4.1
    • Add autostart feature
  • 4.0
    • Change license to MIT
  • 3.1.x
    • Add .npmignore
  • 3.0.x
    • Change the default concurrency to Infinity
    • Allow q.start() to accept an optional callback executed on q.emit('end')
  • 2.x
    • Major api changes / not backwards compatible with 1.x
  • 1.x
    • Early prototype

License

Copyright © 2014 Jesse Tane [email protected] Copyright © 2018 Uni Sayo [email protected]

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See LICENSE for full details.

About

Asynchronous function queuing with adjustable concurrency and re-queuing

Resources

License

Stars

Watchers

Forks

Packages

No packages published