1 | var EventEmitter = require('events').EventEmitter;
|
2 | var util = require('util');
|
3 | var Session = require('./session');
|
4 |
|
5 | function TaskList(name, options) {
|
6 | if(!(this instanceof TaskList)) {
|
7 | return new TaskList(name, options);
|
8 | }
|
9 | this._name = name;
|
10 | this._options = options || {};
|
11 |
|
12 | this._pretty = (this._options.pretty === false)? false: true;
|
13 | this._ignoreErrors = this._options.ignoreErrors;
|
14 | this._taskQueue = [];
|
15 | }
|
16 |
|
17 | util.inherits(TaskList, EventEmitter);
|
18 |
|
19 | TaskList.prototype.run = function(sessions, options, callback) {
|
20 | var self = this;
|
21 |
|
22 | if(sessions instanceof Session) {
|
23 | sessions = [sessions];
|
24 | } else if(!(sessions instanceof Array)) {
|
25 | throw new Error('first param should be either a session or a list of sessions');
|
26 | }
|
27 |
|
28 | if(typeof(options) == 'function') {
|
29 | callback = options;
|
30 | options = {};
|
31 | }
|
32 | options = options || {};
|
33 | var summeryMap = {};
|
34 | var completed = 0;
|
35 |
|
36 | self.log('info', '\nStarted TaskList: ' + this._name);
|
37 | Array.prototype.forEach.call(sessions, function(session) {
|
38 | self._runTaskQueue(session, function(err, history) {
|
39 | summeryMap[session._host] = {error: err, history: history};
|
40 |
|
41 | if(++completed == sessions.length) {
|
42 | self.log('info', 'Completed TaskList: ' + self._name);
|
43 | if(callback) callback(summeryMap);
|
44 | }
|
45 | });
|
46 | });
|
47 | };
|
48 |
|
49 | TaskList.prototype.concat = function(taskLists, name, options) {
|
50 | if(typeof(name) == 'object') {
|
51 | options = name;
|
52 | name = null;
|
53 | }
|
54 |
|
55 | name = name || this._name + '+';
|
56 | options = options || this._options;
|
57 | var newTaskList = new TaskList(name, options);
|
58 |
|
59 |
|
60 | var actionQueueList = taskLists.map(function(taskList) { return taskList._taskQueue; });
|
61 | actionQueueList.unshift(this._taskQueue);
|
62 | newTaskList._taskQueue = newTaskList._taskQueue.concat.apply(newTaskList._taskQueue, actionQueueList);
|
63 |
|
64 | return newTaskList;
|
65 | };
|
66 |
|
67 | TaskList.prototype._runTaskQueue = function(session, callback) {
|
68 | var self = this;
|
69 | var cnt = 0;
|
70 | var taskHistory = [];
|
71 |
|
72 | runTask();
|
73 |
|
74 | function runTask() {
|
75 | var task = self._taskQueue[cnt++];
|
76 | if(task) {
|
77 | self.emit('started', task.id);
|
78 | self.log('info', util.format('[%s] %s', session._host, task.id));
|
79 | TaskList._registeredTasks[task.type](session, task.options, function(err) {
|
80 | if(err) {
|
81 | taskHistory.push({
|
82 | task: task.id,
|
83 | status: 'FAILED',
|
84 | error: err.message
|
85 | });
|
86 | self.emit('failed', err, task.id);
|
87 | self.log('info', util.format('[%s] %s: FAILED\n\t%s', session._host, task.id, err.message.replace(/\n/g, '\n\t')));
|
88 |
|
89 | if(self._ignoreErrors) {
|
90 | runTask();
|
91 | } else {
|
92 | callback(err, taskHistory);
|
93 | }
|
94 | } else {
|
95 | taskHistory.push({
|
96 | task: task.id,
|
97 | status: 'SUCCESS'
|
98 | })
|
99 | self.log('info', util.format('[%s] %s: SUCCESS', session._host, task.id));
|
100 | self.emit('success', task.id);
|
101 | runTask();
|
102 | }
|
103 | });
|
104 | } else {
|
105 | callback(null, taskHistory);
|
106 | }
|
107 | }
|
108 | };
|
109 |
|
110 | TaskList.prototype.log = function(type, message) {
|
111 | if(this._pretty) {
|
112 | console[type](message);
|
113 | }
|
114 | };
|
115 |
|
116 | TaskList._registeredTasks = {};
|
117 |
|
118 | TaskList.registerTask = function(name, callback) {
|
119 | TaskList._registeredTasks[name] = callback;
|
120 | TaskList.prototype[name] = function(id, options) {
|
121 | this._taskQueue.push({
|
122 | type: name,
|
123 | id: id,
|
124 | options: options
|
125 | });
|
126 | };
|
127 | };
|
128 |
|
129 | module.exports = TaskList; |
\ | No newline at end of file |