Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

job-schedule

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

job-schedule

Schedule Jobs for run one time

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

job-schedule

Schedule job for run one time.

This is similar to PriorityQueue

Creates a queue object with the specified concurrency. Tasks added to the queue are processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one becomes available. Once a worker completes a task, that task's callback is called.

However, it can specific running-condition for the task. This is very useful to schedule the time based jobs for run one time only.

Install

$ npm i -S job-schedule

API

// parameters and return value if similar to `priorityQueue`
let q = new Queue(worker, concurrency)

// Start task when this function returns `true`
// Call `cb` to resume the queue `q`
q.when = function (node, cb) {
  if (someConditions(node)) {
    return true
  }
  doSomething(() => cb())
}

// remove jobs with condition
// true: removing job
q.remove(node => {
  if (matched(node)) {
    return true
  }
  return false
})

Example

const Queue = require('job-schedule')

var timerId = null
var q = new Queue((task, cb) => {
  console.log('priority:hello', task.name)
  cb()
}, 1)

// node: {  priority, data }
q.when = function (node, cb) {
  // start task if it's priority less than 2
  if (node.priority -- < 2) {
    clearTimeout(timerId)
    console.log('start:', node.data.name)
    return true
  }
  // resume queue after 1s
  timerId = setTimeout(cb, 1000)
}

// call when all tasks finished
q.drain = function () {
  console.log('all items have been processed')
}

// add some items to the queue
q.push({name: 'foo'}, 10, function (err) {
  console.log('finished processing foo', err)
})
q.push({name: 'bar'}, 2, function (err) {
  console.log('finished processing bar', err)
})

// add some items to the queue (batch-wise)
q.push([{name: 'baz'}, {name: 'bay'}, {name: 'bax'}], 6, function (err) {
  console.log('finished processing item', err)
})

Prior art

async

Keywords

FAQs

Package last updated on 14 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc