diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
commit | abd94a7f5a50f43c797a11b53549ae48fff667c3 (patch) | |
tree | ab8ed457f65cdd72e13e0571d2975729428f1551 /node_modules/orchestrator | |
parent | a0247c6a3fd6a09a41a7e35a3441324c4dcb58be (diff) | |
download | wallet-core-abd94a7f5a50f43c797a11b53549ae48fff667c3.tar.xz |
add node_modules to address #4364
Diffstat (limited to 'node_modules/orchestrator')
-rw-r--r-- | node_modules/orchestrator/.npmignore | 10 | ||||
-rw-r--r-- | node_modules/orchestrator/LICENSE | 20 | ||||
-rw-r--r-- | node_modules/orchestrator/README.md | 286 | ||||
-rw-r--r-- | node_modules/orchestrator/index.js | 304 | ||||
-rw-r--r-- | node_modules/orchestrator/lib/runTask.js | 66 | ||||
-rw-r--r-- | node_modules/orchestrator/package.json | 114 |
6 files changed, 800 insertions, 0 deletions
diff --git a/node_modules/orchestrator/.npmignore b/node_modules/orchestrator/.npmignore new file mode 100644 index 000000000..2422e2040 --- /dev/null +++ b/node_modules/orchestrator/.npmignore @@ -0,0 +1,10 @@ +.DS_Store +*.log +node_modules +build +*.node +components +*.orig +.idea +test +.travis.yml diff --git a/node_modules/orchestrator/LICENSE b/node_modules/orchestrator/LICENSE new file mode 100644 index 000000000..3d20afd94 --- /dev/null +++ b/node_modules/orchestrator/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2013 [Richardson & Sons, LLC](http://richardsonandsons.com/) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/orchestrator/README.md b/node_modules/orchestrator/README.md new file mode 100644 index 000000000..3d3fee45c --- /dev/null +++ b/node_modules/orchestrator/README.md @@ -0,0 +1,286 @@ +[![Build Status](https://secure.travis-ci.org/orchestrator/orchestrator.svg?branch=master)](https://travis-ci.org/orchestrator/orchestrator) +[![Dependency Status](https://david-dm.org/orchestrator/orchestrator.svg)](https://david-dm.org/orchestrator/orchestrator) + +Orchestrator +============ + +A module for sequencing and executing tasks and dependencies in maximum concurrency + +Usage +----- + +### 1. Get a reference: + +```javascript +var Orchestrator = require('orchestrator'); +var orchestrator = new Orchestrator(); +``` + +### 2. Load it up with stuff to do: + +```javascript +orchestrator.add('thing1', function(){ + // do stuff +}); +orchestrator.add('thing2', function(){ + // do stuff +}); +``` + +### 3. Run the tasks: + +```javascript +orchestrator.start('thing1', 'thing2', function (err) { + // all done +}); +``` + +API +--- + +### orchestrator.add(name[, deps][, function]); + +Define a task + +```javascript +orchestrator.add('thing1', function(){ + // do stuff +}); +``` + +#### name +Type: `String` + +The name of the task. + +#### deps +Type: `Array` + +An array of task names to be executed and completed before your task will run. + +```javascript +orchestrator.add('mytask', ['array', 'of', 'task', 'names'], function() { + // Do stuff +}); +``` + +**Note:** Are your tasks running before the dependencies are complete? Make sure your dependency tasks +are correctly using the async run hints: take in a callback or return a promise or event stream. + +#### fn +Type: `function` + +The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: + +- Take in a callback +- Return a stream or a promise + +#### examples: + +**Accept a callback:** + +```javascript +orchestrator.add('thing2', function(callback){ + // do stuff + callback(err); +}); +``` + +**Return a promise:** + +```javascript +var Q = require('q'); + +orchestrator.add('thing3', function(){ + var deferred = Q.defer(); + + // do async stuff + setTimeout(function () { + deferred.resolve(); + }, 1); + + return deferred.promise; +}); +``` + +**Return a stream:** (task is marked complete when stream ends) + +```javascript +var map = require('map-stream'); + +orchestrator.add('thing4', function(){ + var stream = map(function (args, cb) { + cb(null, args); + }); + // do stream stuff + return stream; +}); +``` + +**Note:** By default, tasks run with maximum concurrency -- e.g. it launches all the tasks at once and waits for nothing. +If you want to create a series where tasks run in a particular order, you need to do two things: + +- give it a hint to tell it when the task is done, +- and give it a hint that a task depends on completion of another. + +For these examples, let's presume you have two tasks, "one" and "two" that you specifically want to run in this order: + +1. In task "one" you add a hint to tell it when the task is done. Either take in a callback and call it when you're +done or return a promise or stream that the engine should wait to resolve or end respectively. + +2. In task "two" you add a hint telling the engine that it depends on completion of the first task. + +So this example would look like this: + +```javascript +var Orchestrator = require('orchestrator'); +var orchestrator = new Orchestrator(); + +// takes in a callback so the engine knows when it'll be done +orchestrator.add('one', function (cb) { + // do stuff -- async or otherwise + cb(err); // if err is not null or undefined, the orchestration will stop, and note that it failed +}); + +// identifies a dependent task must be complete before this one begins +orchestrator.add('two', ['one'], function () { + // task 'one' is done now +}); + +orchestrator.start('one', 'two'); +``` + +### orchestrator.hasTask(name); + +Have you defined a task with this name? + +#### name +Type: `String` + +The task name to query + +### orchestrator.start(tasks...[, cb]); + +Start running the tasks + +#### tasks +Type: `String` or `Array` of `String`s + +Tasks to be executed. You may pass any number of tasks as individual arguments. + +#### cb +Type: `function`: `function (err) {` + +Callback to call after run completed. + +Passes single argument: `err`: did the orchestration succeed? + +**Note:** Tasks run concurrently and therefore may not complete in order. +**Note:** Orchestrator uses `sequencify` to resolve dependencies before running, and therefore may not start in order. +Listen to orchestration events to watch task running. + +```javascript +orchestrator.start('thing1', 'thing2', 'thing3', 'thing4', function (err) { + // all done +}); +``` +```javascript +orchestrator.start(['thing1','thing2'], ['thing3','thing4']); +``` + +**FRAGILE:** Orchestrator catches exceptions on sync runs to pass to your callback +but doesn't hook to process.uncaughtException so it can't pass those exceptions +to your callback + +**FRAGILE:** Orchestrator will ensure each task and each dependency is run once during an orchestration run +even if you specify it to run more than once. (e.g. `orchestrator.start('thing1', 'thing1')` +will only run 'thing1' once.) If you need it to run a task multiple times, wait for +the orchestration to end (start's callback) then call start again. +(e.g. `orchestrator.start('thing1', function () {orchestrator.start('thing1');})`.) +Alternatively create a second orchestrator instance. + +### orchestrator.stop() + +Stop an orchestration run currently in process + +**Note:** It will call the `start()` callback with an `err` noting the orchestration was aborted + +### orchestrator.on(event, cb); + +Listen to orchestrator internals + +#### event +Type: `String` + +Event name to listen to: +- start: from start() method, shows you the task sequence +- stop: from stop() method, the queue finished successfully +- err: from stop() method, the queue was aborted due to a task error +- task_start: from _runTask() method, task was started +- task_stop: from _runTask() method, task completed successfully +- task_err: from _runTask() method, task errored +- task_not_found: from start() method, you're trying to start a task that doesn't exist +- task_recursion: from start() method, there are recursive dependencies in your task list + +#### cb +Type: `function`: `function (e) {` + +Passes single argument: `e`: event details + +```javascript +orchestrator.on('task_start', function (e) { + // e.message is the log message + // e.task is the task name if the message applies to a task else `undefined` + // e.err is the error if event is 'err' else `undefined` +}); +// for task_end and task_err: +orchestrator.on('task_stop', function (e) { + // e is the same object from task_start + // e.message is updated to show how the task ended + // e.duration is the task run duration (in seconds) +}); +``` + +**Note:** fires either *stop or *err but not both. + +### orchestrator.onAll(cb); + +Listen to all orchestrator events from one callback + +#### cb +Type: `function`: `function (e) {` + +Passes single argument: `e`: event details + +```javascript +orchestrator.onAll(function (e) { + // e is the original event args + // e.src is event name +}); +``` + +LICENSE +------- + +(MIT License) + +Copyright (c) 2013 [Richardson & Sons, LLC](http://richardsonandsons.com/) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/orchestrator/index.js b/node_modules/orchestrator/index.js new file mode 100644 index 000000000..b13d9927d --- /dev/null +++ b/node_modules/orchestrator/index.js @@ -0,0 +1,304 @@ +/*jshint node:true */ + +"use strict"; + +var util = require('util'); +var events = require('events'); +var EventEmitter = events.EventEmitter; +var runTask = require('./lib/runTask'); + +var Orchestrator = function () { + EventEmitter.call(this); + this.doneCallback = undefined; // call this when all tasks in the queue are done + this.seq = []; // the order to run the tasks + this.tasks = {}; // task objects: name, dep (list of names of dependencies), fn (the task to run) + this.isRunning = false; // is the orchestrator running tasks? .start() to start, .stop() to stop +}; +util.inherits(Orchestrator, EventEmitter); + + Orchestrator.prototype.reset = function () { + if (this.isRunning) { + this.stop(null); + } + this.tasks = {}; + this.seq = []; + this.isRunning = false; + this.doneCallback = undefined; + return this; + }; + Orchestrator.prototype.add = function (name, dep, fn) { + if (!fn && typeof dep === 'function') { + fn = dep; + dep = undefined; + } + dep = dep || []; + fn = fn || function () {}; // no-op + if (!name) { + throw new Error('Task requires a name'); + } + // validate name is a string, dep is an array of strings, and fn is a function + if (typeof name !== 'string') { + throw new Error('Task requires a name that is a string'); + } + if (typeof fn !== 'function') { + throw new Error('Task '+name+' requires a function that is a function'); + } + if (!Array.isArray(dep)) { + throw new Error('Task '+name+' can\'t support dependencies that is not an array of strings'); + } + dep.forEach(function (item) { + if (typeof item !== 'string') { + throw new Error('Task '+name+' dependency '+item+' is not a string'); + } + }); + this.tasks[name] = { + fn: fn, + dep: dep, + name: name + }; + return this; + }; + Orchestrator.prototype.task = function (name, dep, fn) { + if (dep || fn) { + // alias for add, return nothing rather than this + this.add(name, dep, fn); + } else { + return this.tasks[name]; + } + }; + Orchestrator.prototype.hasTask = function (name) { + return !!this.tasks[name]; + }; + // tasks and optionally a callback + Orchestrator.prototype.start = function() { + var args, arg, names = [], lastTask, i, seq = []; + args = Array.prototype.slice.call(arguments, 0); + if (args.length) { + lastTask = args[args.length-1]; + if (typeof lastTask === 'function') { + this.doneCallback = lastTask; + args.pop(); + } + for (i = 0; i < args.length; i++) { + arg = args[i]; + if (typeof arg === 'string') { + names.push(arg); + } else if (Array.isArray(arg)) { + names = names.concat(arg); // FRAGILE: ASSUME: it's an array of strings + } else { + throw new Error('pass strings or arrays of strings'); + } + } + } + if (this.isRunning) { + // reset specified tasks (and dependencies) as not run + this._resetSpecificTasks(names); + } else { + // reset all tasks as not run + this._resetAllTasks(); + } + if (this.isRunning) { + // if you call start() again while a previous run is still in play + // prepend the new tasks to the existing task queue + names = names.concat(this.seq); + } + if (names.length < 1) { + // run all tasks + for (i in this.tasks) { + if (this.tasks.hasOwnProperty(i)) { + names.push(this.tasks[i].name); + } + } + } + seq = []; + try { + this.sequence(this.tasks, names, seq, []); + } catch (err) { + // Is this a known error? + if (err) { + if (err.missingTask) { + this.emit('task_not_found', {message: err.message, task:err.missingTask, err: err}); + } + if (err.recursiveTasks) { + this.emit('task_recursion', {message: err.message, recursiveTasks:err.recursiveTasks, err: err}); + } + } + this.stop(err); + return this; + } + this.seq = seq; + this.emit('start', {message:'seq: '+this.seq.join(',')}); + if (!this.isRunning) { + this.isRunning = true; + } + this._runStep(); + return this; + }; + Orchestrator.prototype.stop = function (err, successfulFinish) { + this.isRunning = false; + if (err) { + this.emit('err', {message:'orchestration failed', err:err}); + } else if (successfulFinish) { + this.emit('stop', {message:'orchestration succeeded'}); + } else { + // ASSUME + err = 'orchestration aborted'; + this.emit('err', {message:'orchestration aborted', err: err}); + } + if (this.doneCallback) { + // Avoid calling it multiple times + this.doneCallback(err); + } else if (err && !this.listeners('err').length) { + // No one is listening for the error so speak louder + throw err; + } + }; + Orchestrator.prototype.sequence = require('sequencify'); + Orchestrator.prototype.allDone = function () { + var i, task, allDone = true; // nothing disputed it yet + for (i = 0; i < this.seq.length; i++) { + task = this.tasks[this.seq[i]]; + if (!task.done) { + allDone = false; + break; + } + } + return allDone; + }; + Orchestrator.prototype._resetTask = function(task) { + if (task) { + if (task.done) { + task.done = false; + } + delete task.start; + delete task.stop; + delete task.duration; + delete task.hrDuration; + delete task.args; + } + }; + Orchestrator.prototype._resetAllTasks = function() { + var task; + for (task in this.tasks) { + if (this.tasks.hasOwnProperty(task)) { + this._resetTask(this.tasks[task]); + } + } + }; + Orchestrator.prototype._resetSpecificTasks = function (names) { + var i, name, t; + + if (names && names.length) { + for (i = 0; i < names.length; i++) { + name = names[i]; + t = this.tasks[name]; + if (t) { + this._resetTask(t); + if (t.dep && t.dep.length) { + this._resetSpecificTasks(t.dep); // recurse + } + //} else { + // FRAGILE: ignore that the task doesn't exist + } + } + } + }; + Orchestrator.prototype._runStep = function () { + var i, task; + if (!this.isRunning) { + return; // user aborted, ASSUME: stop called previously + } + for (i = 0; i < this.seq.length; i++) { + task = this.tasks[this.seq[i]]; + if (!task.done && !task.running && this._readyToRunTask(task)) { + this._runTask(task); + } + if (!this.isRunning) { + return; // task failed or user aborted, ASSUME: stop called previously + } + } + if (this.allDone()) { + this.stop(null, true); + } + }; + Orchestrator.prototype._readyToRunTask = function (task) { + var ready = true, // no one disproved it yet + i, name, t; + if (task.dep.length) { + for (i = 0; i < task.dep.length; i++) { + name = task.dep[i]; + t = this.tasks[name]; + if (!t) { + // FRAGILE: this should never happen + this.stop("can't run "+task.name+" because it depends on "+name+" which doesn't exist"); + ready = false; + break; + } + if (!t.done) { + ready = false; + break; + } + } + } + return ready; + }; + Orchestrator.prototype._stopTask = function (task, meta) { + task.duration = meta.duration; + task.hrDuration = meta.hrDuration; + task.running = false; + task.done = true; + }; + Orchestrator.prototype._emitTaskDone = function (task, message, err) { + if (!task.args) { + task.args = {task:task.name}; + } + task.args.duration = task.duration; + task.args.hrDuration = task.hrDuration; + task.args.message = task.name+' '+message; + var evt = 'stop'; + if (err) { + task.args.err = err; + evt = 'err'; + } + // 'task_stop' or 'task_err' + this.emit('task_'+evt, task.args); + }; + Orchestrator.prototype._runTask = function (task) { + var that = this; + + task.args = {task:task.name, message:task.name+' started'}; + this.emit('task_start', task.args); + task.running = true; + + runTask(task.fn.bind(this), function (err, meta) { + that._stopTask.call(that, task, meta); + that._emitTaskDone.call(that, task, meta.runMethod, err); + if (err) { + return that.stop.call(that, err); + } + that._runStep.call(that); + }); + }; + +// FRAGILE: ASSUME: this list is an exhaustive list of events emitted +var events = ['start','stop','err','task_start','task_stop','task_err','task_not_found','task_recursion']; + +var listenToEvent = function (target, event, callback) { + target.on(event, function (e) { + e.src = event; + callback(e); + }); +}; + + Orchestrator.prototype.onAll = function (callback) { + var i; + if (typeof callback !== 'function') { + throw new Error('No callback specified'); + } + + for (i = 0; i < events.length; i++) { + listenToEvent(this, events[i], callback); + } + }; + +module.exports = Orchestrator; diff --git a/node_modules/orchestrator/lib/runTask.js b/node_modules/orchestrator/lib/runTask.js new file mode 100644 index 000000000..97dae22cb --- /dev/null +++ b/node_modules/orchestrator/lib/runTask.js @@ -0,0 +1,66 @@ +/*jshint node:true */ + +"use strict"; + +var eos = require('end-of-stream'); +var consume = require('stream-consume'); + +module.exports = function (task, done) { + var that = this, finish, cb, isDone = false, start, r; + + finish = function (err, runMethod) { + var hrDuration = process.hrtime(start); + + if (isDone && !err) { + err = new Error('task completion callback called too many times'); + } + isDone = true; + + var duration = hrDuration[0] + (hrDuration[1] / 1e9); // seconds + + done.call(that, err, { + duration: duration, // seconds + hrDuration: hrDuration, // [seconds,nanoseconds] + runMethod: runMethod + }); + }; + + cb = function (err) { + finish(err, 'callback'); + }; + + try { + start = process.hrtime(); + r = task(cb); + } catch (err) { + return finish(err, 'catch'); + } + + if (r && typeof r.then === 'function') { + // wait for promise to resolve + // FRAGILE: ASSUME: Promises/A+, see http://promises-aplus.github.io/promises-spec/ + r.then(function () { + finish(null, 'promise'); + }, function(err) { + finish(err, 'promise'); + }); + + } else if (r && typeof r.pipe === 'function') { + // wait for stream to end + + eos(r, { error: true, readable: r.readable, writable: r.writable && !r.readable }, function(err){ + finish(err, 'stream'); + }); + + // Ensure that the stream completes + consume(r); + + } else if (task.length === 0) { + // synchronous, function took in args.length parameters, and the callback was extra + finish(null, 'sync'); + + //} else { + // FRAGILE: ASSUME: callback + + } +}; diff --git a/node_modules/orchestrator/package.json b/node_modules/orchestrator/package.json new file mode 100644 index 000000000..bd3bf678c --- /dev/null +++ b/node_modules/orchestrator/package.json @@ -0,0 +1,114 @@ +{ + "_args": [ + [ + { + "raw": "orchestrator@^0.3.0", + "scope": null, + "escapedName": "orchestrator", + "name": "orchestrator", + "rawSpec": "^0.3.0", + "spec": ">=0.3.0 <0.4.0", + "type": "range" + }, + "/home/dold/repos/taler/wallet-webex/node_modules/gulp" + ] + ], + "_from": "orchestrator@>=0.3.0 <0.4.0", + "_id": "orchestrator@0.3.7", + "_inCache": true, + "_location": "/orchestrator", + "_npmUser": { + "name": "phated", + "email": "blaine@iceddev.com" + }, + "_npmVersion": "1.4.14", + "_phantomChildren": {}, + "_requested": { + "raw": "orchestrator@^0.3.0", + "scope": null, + "escapedName": "orchestrator", + "name": "orchestrator", + "rawSpec": "^0.3.0", + "spec": ">=0.3.0 <0.4.0", + "type": "range" + }, + "_requiredBy": [ + "/gulp" + ], + "_resolved": "https://registry.npmjs.org/orchestrator/-/orchestrator-0.3.7.tgz", + "_shasum": "c45064e22c5a2a7b99734f409a95ffedc7d3c3df", + "_shrinkwrap": null, + "_spec": "orchestrator@^0.3.0", + "_where": "/home/dold/repos/taler/wallet-webex/node_modules/gulp", + "author": { + "name": "Rob Richardson", + "url": "http://robrich.org/" + }, + "bugs": { + "url": "https://github.com/robrich/orchestrator/issues" + }, + "dependencies": { + "end-of-stream": "~0.1.5", + "sequencify": "~0.0.7", + "stream-consume": "~0.1.0" + }, + "description": "A module for sequencing and executing tasks and dependencies in maximum concurrency", + "devDependencies": { + "event-stream": "~3.1.5", + "gulp-jshint": "~1.6.3", + "map-stream": "~0.1.0", + "merge-stream": "~0.1.2", + "mocha": "~1.17.0", + "q": "~1.0.0", + "should": "~3.0.1", + "vinyl-fs": "~0.3.4" + }, + "directories": {}, + "dist": { + "shasum": "c45064e22c5a2a7b99734f409a95ffedc7d3c3df", + "tarball": "https://registry.npmjs.org/orchestrator/-/orchestrator-0.3.7.tgz" + }, + "engines": { + "node": ">= 0.8" + }, + "gitHead": "f3fcb93e3560afd0bc0c4acd5c5db7bd109414e7", + "homepage": "https://github.com/robrich/orchestrator", + "keywords": [ + "async", + "task", + "parallel", + "compose" + ], + "licenses": [ + { + "type": "MIT", + "url": "http://github.com/robrich/orchestrator/raw/master/LICENSE" + } + ], + "main": "./index.js", + "maintainers": [ + { + "name": "robrich", + "email": "robrich@robrich.org" + }, + { + "name": "phated", + "email": "blaine@iceddev.com" + }, + { + "name": "contra", + "email": "contra@wearefractal.com" + } + ], + "name": "orchestrator", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git://github.com/robrich/orchestrator.git" + }, + "scripts": { + "test": "mocha" + }, + "version": "0.3.7" +} |