diff options
author | 2020-11-16 00:10:28 +0100 | |
---|---|---|
committer | 2020-11-16 00:10:28 +0100 | |
commit | e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d (patch) | |
tree | 55713f725f77b44ebfec86e4eec3ce33e71458ca /node_modules/run-queue | |
download | website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.gz website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.bz2 website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.zip |
api, login, auth
Diffstat (limited to 'node_modules/run-queue')
-rw-r--r-- | node_modules/run-queue/README.md | 86 | ||||
-rw-r--r-- | node_modules/run-queue/package.json | 63 | ||||
-rw-r--r-- | node_modules/run-queue/queue.js | 95 |
3 files changed, 244 insertions, 0 deletions
diff --git a/node_modules/run-queue/README.md b/node_modules/run-queue/README.md new file mode 100644 index 0000000..e84214d --- /dev/null +++ b/node_modules/run-queue/README.md @@ -0,0 +1,86 @@ +# run-queue + +A promise based, dynamic priority queue runner, with concurrency limiting. + +```js +const RunQueue = require('run-queue') + +const queue = new RunQueue({ + maxConcurrency: 1 +}) + +queue.add(1, example, [-1]) +for (let ii = 0; ii < 5; ++ii) { + queue.add(0, example, [ii]) +} +const finished = [] +queue.run().then( + console.log(finished) +}) + +function example (num, next) { + setTimeout(() => { + finished.push(num) + next() + }, 5 - Math.abs(num)) +} +``` + +would output + +``` +[ 0, 1, 2, 3, 4, -1 ] +``` + +If you bump concurrency to `2`, then you get: + +``` +[ 1, 0, 3, 2, 4, -1 ] +``` + +The concurrency means that they don't finish in order, because some take +longer than others. Each priority level must finish entirely before the +next priority level is run. See +[PRIORITIES](https://github.com/iarna/run-queue#priorities) below. This is +even true if concurrency is set high enough that all of the regular queue +can execute at once, for instance, with `maxConcurrency: 10`: + +``` +[ 4, 3, 2, 1, 0, -1 ] +``` + +## API + +### const queue = new RunQueue(options) + +Create a new queue. Options may contain: + +* maxConcurrency - (Default: `1`) The maximum number of jobs to execute at once. +* Promise - (Default: global.Promise) The promise implementation to use. + +### queue.add (prio, fn, args) + +Add a new job to the end of the queue at priority `prio` that will run `fn` +with `args`. If `fn` is async then it should return a Promise. + +### queue.run () + +Start running the job queue. Returns a Promise that resolves when either +all the jobs are complete or a job ends in error (throws or returns a +rejected promise). If a job ended in error then this Promise will be rejected +with that error and no further queue running will be done. + +## PRIORITIES + +Priorities are any integer value >= 0. + +Lowest is executed first. + +Priorities essentially represent distinct job queues. All jobs in a queue +must complete before the next highest priority job queue is executed. + +This means that if you have two queues, `0` and `1` then ALL jobs in `0` +must complete before ANY execute in `1`. If you add new `0` level jobs +while `1` level jobs are running then it will switch back processing the `0` +queue and won't execute any more `1` jobs till all of the new `0` jobs +complete. diff --git a/node_modules/run-queue/package.json b/node_modules/run-queue/package.json new file mode 100644 index 0000000..5cfcb5a --- /dev/null +++ b/node_modules/run-queue/package.json @@ -0,0 +1,63 @@ +{ + "_from": "run-queue@^1.0.3", + "_id": "run-queue@1.0.3", + "_inBundle": false, + "_integrity": "sha1-6Eg5bwV9Ij8kOGkkYY4laUFh7Ec=", + "_location": "/run-queue", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "run-queue@^1.0.3", + "name": "run-queue", + "escapedName": "run-queue", + "rawSpec": "^1.0.3", + "saveSpec": null, + "fetchSpec": "^1.0.3" + }, + "_requiredBy": [ + "/copy-concurrently", + "/move-concurrently" + ], + "_resolved": "https://registry.npmjs.org/run-queue/-/run-queue-1.0.3.tgz", + "_shasum": "e848396f057d223f24386924618e25694161ec47", + "_spec": "run-queue@^1.0.3", + "_where": "/home/pruss/Dev/3-minute-website/node_modules/move-concurrently", + "author": { + "name": "Rebecca Turner", + "email": "me@re-becca.org", + "url": "http://re-becca.org/" + }, + "bugs": { + "url": "https://github.com/iarna/run-queue/issues" + }, + "bundleDependencies": false, + "dependencies": { + "aproba": "^1.1.1" + }, + "deprecated": false, + "description": "A promise based, dynamic priority queue runner, with concurrency limiting.", + "devDependencies": { + "standard": "^8.6.0", + "tap": "^10.2.0" + }, + "directories": { + "test": "test" + }, + "files": [ + "queue.js" + ], + "homepage": "https://npmjs.com/package/run-queue", + "keywords": [], + "license": "ISC", + "main": "queue.js", + "name": "run-queue", + "repository": { + "type": "git", + "url": "git+https://github.com/iarna/run-queue.git" + }, + "scripts": { + "test": "standard && tap -J test" + }, + "version": "1.0.3" +} diff --git a/node_modules/run-queue/queue.js b/node_modules/run-queue/queue.js new file mode 100644 index 0000000..500bf29 --- /dev/null +++ b/node_modules/run-queue/queue.js @@ -0,0 +1,95 @@ +'use strict' +module.exports = RunQueue + +var validate = require('aproba') + +function RunQueue (opts) { + validate('Z|O', [opts]) + if (!opts) opts = {} + this.finished = false + this.inflight = 0 + this.maxConcurrency = opts.maxConcurrency || 1 + this.queued = 0 + this.queue = [] + this.currentPrio = null + this.currentQueue = null + this.Promise = opts.Promise || global.Promise + this.deferred = {} +} + +RunQueue.prototype = {} + +RunQueue.prototype.run = function () { + if (arguments.length !== 0) throw new Error('RunQueue.run takes no arguments') + var self = this + var deferred = this.deferred + if (!deferred.promise) { + deferred.promise = new this.Promise(function (resolve, reject) { + deferred.resolve = resolve + deferred.reject = reject + self._runQueue() + }) + } + return deferred.promise +} + +RunQueue.prototype._runQueue = function () { + var self = this + + while ((this.inflight < this.maxConcurrency) && this.queued) { + if (!this.currentQueue || this.currentQueue.length === 0) { + // wait till the current priority is entirely processed before + // starting a new one + if (this.inflight) return + var prios = Object.keys(this.queue) + for (var ii = 0; ii < prios.length; ++ii) { + var prioQueue = this.queue[prios[ii]] + if (prioQueue.length) { + this.currentQueue = prioQueue + this.currentPrio = prios[ii] + break + } + } + } + + --this.queued + ++this.inflight + var next = this.currentQueue.shift() + var args = next.args || [] + + // we explicitly construct a promise here so that queue items can throw + // or immediately return to resolve + var queueEntry = new this.Promise(function (resolve) { + resolve(next.cmd.apply(null, args)) + }) + + queueEntry.then(function () { + --self.inflight + if (self.finished) return + if (self.queued <= 0 && self.inflight <= 0) { + self.finished = true + self.deferred.resolve() + } + self._runQueue() + }, function (err) { + self.finished = true + self.deferred.reject(err) + }) + } +} + +RunQueue.prototype.add = function (prio, cmd, args) { + if (this.finished) throw new Error("Can't add to a finished queue. Create a new queue.") + if (Math.abs(Math.floor(prio)) !== prio) throw new Error('Priorities must be a positive integer value.') + validate('NFA|NFZ', [prio, cmd, args]) + prio = Number(prio) + if (!this.queue[prio]) this.queue[prio] = [] + ++this.queued + this.queue[prio].push({cmd: cmd, args: args}) + // if this priority is higher than the one we're currently processing, + // switch back to processing its queue. + if (this.currentPrio > prio) { + this.currentQueue = this.queue[prio] + this.currentPrio = prio + } +} |