forked from apla/dataflo.ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.js
592 lines (459 loc) · 15.3 KB
/
workflow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
var define;
if (typeof define === "undefined")
define = function (classInstance) {
classInstance (require, exports, module);
}
define (function (require, exports, module) {
var EventEmitter = require ('events').EventEmitter,
util = require ('util'),
common = require ('./common'),
taskClass = require ('./task/base');
var taskStateNames = taskClass.prototype.stateNames;
var hasOwnProperty = Object.prototype.hasOwnProperty;
function isEmpty(obj) {
if (obj === void 0)
return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj === true)
return !obj;
if (obj.toFixed && obj !== 0)
return false;
if (obj.length && obj.length > 0)
return false;
for (var key in obj) {
if (hasOwnProperty.call(obj, key))
return false;
}
return true;
}
function taskRequirements (requirements, dict) {
var result = [];
for (var k in requirements) {
var requirement = requirements[k];
for (var i = 0; i < requirement.length; i++) {
try {
if (isEmpty (common.pathToVal (dict, requirement[i])))
result.push (k);
} catch (e) {
result.push (k);
}
}
}
return result;
}
function checkTaskParams (params, dict, prefix) {
// parse task params
// TODO: modify this function because recursive changes of parameters works dirty (indexOf for value)
if (prefix == void 0) prefix = '';
if (prefix) prefix += '.';
var modifiedParams;
var failedParams = [];
if (params.constructor == Array) { // params is array
modifiedParams = [];
params.forEach(function (val, index, arr) {
if (val.indexOf || val.interpolate) { // string
try {
var tmp = val.interpolate (dict);
if (tmp === void 0)
modifiedParams.push(val);
else
modifiedParams.push(tmp);
// console.log (val, ' interpolated to the "', modifiedParams[key], '" and ', isEmpty (modifiedParams[key]) ? ' is empty' : 'is not empty');
if (isEmpty (modifiedParams[modifiedParams.length-1]))
throw "EMPTY VALUE";
} catch (e) {
failedParams.push (prefix+'['+index+']');
}
} else if (val.toFixed) {
modifiedParams.push(val);
} else {
var result = checkTaskParams(val, dict, prefix+'['+index+']');
modifiedParams.push(result.modified);
failedParams = failedParams.concat (result.failed);
}
});
} else { // params is hash
modifiedParams = {};
for (var key in params) {
var val = params[key];
var valCheck = val;
if ((key == '$bind' || key == 'bind') && prefix == '') {
// bind is a real js object. it can be circular
modifiedParams[key] = val;
} else if (val.interpolate) { // val is string || number
try {
var tmp = modifiedParams[key] = val.interpolate (dict);
if (tmp === void 0)
modifiedParams[key] = val;
// if (tmp === false || tmp === 0 || tmp === "")
// console.log (val, ' interpolated to the "', modifiedParams[key], '" and ', isEmpty (modifiedParams[key]) ? ' is empty' : 'is not empty');
if (isEmpty (modifiedParams[key]))
throw "EMPTY VALUE";
} catch (e) {
failedParams.push (prefix+key);
}
} else if (val.toFixed) {
modifiedParams[key] = val;
} else { // val is hash || array
var result = checkTaskParams(val, dict, prefix+key);
modifiedParams[key] = result.modified;
failedParams = failedParams.concat (result.failed);
}
}
}
return {
modified: modifiedParams,
failed: failedParams || []
};
}
/**
* @class workflow
* @extends events.EventEmitter
*
* The heart of the framework. Parses task configurations, loads dependencies,
* launches tasks, stores their result. When all tasks are completed,
* notifies subscribers (inititators).
*
* @cfg {Object} config (required) Workflow configuration.
* @cfg {String} config.$class (required) Class to instantiate
* (alias of config.className).
* @cfg {String} config.$function (required) Synchronous function to be run
* (instead of a class). Alias of functionName.
* @cfg {String} config.$set Path to the property in which the produced data
* will be stored.
* @cfg {String} config.$method Method to be run after the class instantiation.
* @cfg {Object} reqParam (required) Workflow parameters.
*/
var workflow = module.exports = function (config, reqParam) {
var self = this;
util.extend (true, this, config); // this is immutable config skeleton
util.extend (true, this, reqParam); // this is config fixup
this.created = new Date().getTime();
// here we make sure workflow uid generated
// TODO: check for cpu load
var salt = (Math.random () * 1e6).toFixed(0);
this.id = this.id || (this.started ^ salt) % 1e6;
if (!this.stage) this.stage = 'workflow';
//if (!this.stageMarkers[this.stage])
// console.error ('there is no such stage marker: ' + this.stage);
var idString = ""+this.id;
while (idString.length < 6) {idString = '0' + idString};
this.coloredId = [
"" + idString[0] + idString[1],
"" + idString[2] + idString[3],
"" + idString[4] + idString[5]
].map (function (item) {
try {
var _p = process;
return "\x1B[0;3" + (parseInt(item) % 8) + "m" + item + "\x1B[0m";
} catch (e) {
return item;
}
}).join ('');
this.data = this.data || {};
// console.log ('!!!!!!!!!!!!!!!!!!!' + this.data.keys.length);
// console.log ('config, reqParam', config, reqParam);
self.ready = true;
// TODO: optimize usage - find placeholders and check only placeholders
this.tasks = config.tasks.map (function (taskParams) {
var task;
var checkRequirements = function () {
var dict = util.extend(true, {}, reqParam);
dict.data = self.data;
if ($isServerSide) {
dict.project = project
}
var result = checkTaskParams (taskParams, dict);
if (result.failed && result.failed.length > 0) {
this.unsatisfiedRequirements = result.failed;
return false;
} else if (result.modified) {
util.extend (this, result.modified);
return true;
}
}
// check for data persistence in self.templates[taskTemplateName], taskParams
var taskTemplateName = taskParams.$template;
if (self.templates && self.templates[taskTemplateName]) {
taskParams = util.extend(true, self.templates[taskTemplateName], taskParams);
delete taskParams.$template;
}
// console.log (taskParams);
var taskClassName = taskParams.className || taskParams.$class;
var taskFnName = taskParams.functionName || taskParams.$function;
if (taskClassName && taskFnName)
self.logError ('defined both className and functionName, using className');
if (taskClassName) {
// self.log (taskParams.className + ': initializing task from class');
var xTaskClass;
// TODO: need check all task classes, because some compile errors may be there
// console.log ('task/'+taskParams.className);
try {
xTaskClass = require (taskClassName);
} catch (e) {
console.log ('require '+taskClassName+':', e);
self.ready = false;
return;
}
task = new xTaskClass ({
className: taskClassName,
method: taskParams.method || taskParams.$method,
require: checkRequirements,
important: taskParams.important || taskParams.$important
});
} else if (taskParams.coderef || taskFnName) {
// self.log ((taskParams.functionName || taskParams.logTitle) + ': initializing task from function');
if (!taskFnName && !taskParams.logTitle)
throw "task must have a logTitle when using call parameter";
var xTaskClass = function (config) {
this.init (config);
};
util.inherits (xTaskClass, taskClass);
util.extend (xTaskClass.prototype, {
run: function () {
var failed = false;
if ((taskParams.$bind || taskParams.bind) && taskFnName) {
try {
var functionRef = taskParams.bind || taskParams.$bind;
// TODO: use pathToVal
var fSplit = taskFnName.split (".");
while (fSplit.length) {
var fChunk = fSplit.shift();
functionRef = functionRef[fChunk];
}
this.completed (functionRef.call (taskParams.bind || taskParams.$bind, this));
} catch (e) {
failed = 'failed call function "'+taskFnName+'" from ' + (taskParams.bind || taskParams.$bind) + ' with ' + e;
}
} else if (taskFnName) {
var fn = $mainModule[taskFnName];
if (fn && fn.constructor == Function) {
this.completed (fn (this));
} else {
// this is solution for nodejs scope:
// exports can be redefined
var mainExports = eval ($scope);
var fn = mainExports[taskFnName];
if (fn && fn.constructor == Function) {
$mainModule = mainExports;
this.completed (fn (this));
} else {
// TODO: fix description for window
failed = "you defined functionName as " + taskFnName
+ " but we cannot find this name in current scope (" + $scope
+ ").\nplease add " + ($isClientSide ? "'window." : "'module.exports.")
+ taskFnName + " = function (params) {...}}' in your main module";
}
}
} else {
// TODO: detailed error description
// if (taskParams.bind)
this.completed (taskParams.coderef (this));
}
if (failed) throw failed;
}
});
task = new xTaskClass ({
functionName: taskFnName,
logTitle: taskParams.logTitle || taskParams.$logTitle,
require: checkRequirements,
important: taskParams.important || taskParams.$important
});
}
// console.log (task);
return task;
});
};
util.inherits (workflow, EventEmitter);
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
// one second low resolution timer
$stash.currentDate = new Date ();
$stash.currentDateInterval = setInterval (function () {
$stash.currentDate = new Date ();
}, 1000);
function timestamp () {
var time = [
pad($stash.currentDate.getHours()),
pad($stash.currentDate.getMinutes()),
pad($stash.currentDate.getSeconds())
].join(':');
var date = [
$stash.currentDate.getFullYear(),
pad($stash.currentDate.getMonth() + 1),
pad($stash.currentDate.getDate())
].join ('-');
return [date, time].join(' ');
}
util.extend (workflow.prototype, {
checkTaskParams: checkTaskParams,
taskRequirements: taskRequirements,
isIdle: true,
haveCompletedTasks: false,
/**
* @method run Initiators call this method to launch the workflow.
*/
run: function () {
if (!this.started)
this.started = new Date().getTime();
var self = this;
if (self.stopped)
return;
self.failed = false;
self.isIdle = false;
self.haveCompletedTasks = false;
// self.log ('workflow run');
this.taskStates = [0, 0, 0, 0, 0, 0, 0];
// check task states
this.tasks.map (function (task) {
if (task.subscribed === void(0)) {
self.addEventListenersToTask (task);
}
task.checkState ();
self.taskStates[task.state]++;
// console.log ('task.className, task.state\n', task, task.state, task.isReady ());
if (task.isReady ()) {
self.logTask (task, 'started');
task.run ();
// sync task support
if (!task.isReady()) {
self.taskStates[task.stateNames.ready]--;
self.taskStates[task.state]++;
}
}
});
var taskStateNames = taskClass.prototype.stateNames;
if (this.taskStates[taskStateNames.ready] || this.taskStates[taskStateNames.running]) {
// it is save to continue, wait for running/ready task
console.log ('have running tasks');
self.isIdle = true;
return;
} else if (self.haveCompletedTasks) {
console.log ('have completed tasks');
// stack will be happy
if ($isClientSide) {
setTimeout (function () {self.run ();}, 0);
} else if ($isServerSide) {
process.nextTick (function () {self.run ()});
}
self.isIdle = true;
return;
}
self.stopped = new Date().getTime();
var scarceTaskMessage = 'unsatisfied requirements: ';
// TODO: display scarce tasks unsatisfied requirements
if (this.taskStates[taskStateNames.scarce]) {
self.tasks.map (function (task) {
if (task.state != taskStateNames.scarce && task.state != taskStateNames.skipped)
return;
if (task.important) {
task.failed ("important task didn't started");
self.taskStates[taskStateNames.scarce]--;
self.taskStates[task.state]++;
self.failed = true;
scarceTaskMessage += '(important)';
}
if (task.state == taskStateNames.scarce)
scarceTaskMessage += (task.logTitle) + ' => ' + task.unsatisfiedRequirements.join (', ') + '; ';
});
self.log (scarceTaskMessage);
}
if (self.verbose) {
var requestDump = '???';
try {
requestDump = JSON.stringify (self.request)
} catch (e) {
if ((""+e).match (/circular/))
requestDump = 'CIRCULAR'
else
requestDump = e
};
}
if (this.failed) {
// workflow stopped and failed
self.emit ('failed', self);
self.log (this.stage + ' failed in ' + (self.stopped - self.started) + 'ms; ' + this.taskStates[taskStateNames.failed]+' tasks of ' + self.tasks.length);
} else {
// workflow stopped and not failed
self.emit ('completed', self);
self.log (this.stage + ' complete in ' + (self.stopped - self.started) + 'ms');
}
self.isIdle = true;
},
stageMarker: {prepare: "()", workflow: "[]", presentation: "<>"},
log: function (msg) {
// if (this.quiet || process.quiet) return;
var toLog = [
timestamp (),
this.stageMarker[this.stage][0] + this.coloredId + this.stageMarker[this.stage][1]
];
for (var i = 0, len = arguments.length; i < len; ++i) {
toLog.push (arguments[i]);
}
// TODO: also check for bad clients (like ie9)
if ($isPhoneGap) {
toLog.shift();
toLog = [toLog.join (' ')];
}
console.log.apply (console, toLog);
},
logTask: function (task, msg) {
this.log (task.logTitle, "("+task.state+")", msg);
},
logTaskError: function (task, msg, options) {
// TODO: fix by using console.error
this.log(task.logTitle, "("+task.state+") \x1B[0;31m" + msg, options || '', "\x1B[0m");
},
logError: function (task, msg, options) {
// TODO: fix by using console.error
this.log(" \x1B[0;31m" + msg, options || '', "\x1B[0m");
},
addEventListenersToTask: function (task) {
var self = this;
task.subscribed = 1;
// loggers
task.on ('log', function (message) {
self.logTask (task, message);
});
task.on ('warn', function (message) {
self.logTaskError (task, message);
});
task.on ('error', function (e) {
self.error = e;
self.logTaskError (task, 'error: ', e);// + '\n' + arguments[0].stack);
});
// states
task.on ('skip', function () {
// if (task.important) {
// self.failed = true;
// return self.logTaskError (task, 'error ' + arguments[0]);// + '\n' + arguments[0].stack);
// }
self.logTask (task, 'task skipped');
if (self.isIdle)
self.run ();
});
task.on ('cancel', function () {
self.logTaskError (task, 'canceled, retries = ' + task.retries);
self.failed = true;
if (self.isIdle)
self.run ();
});
task.on ('complete', function (t, result) {
if (result) {
if (t.produce || t.$set) {
common.pathToVal (self, t.produce || t.$set, result);
} else if (t.$mergeWith) {
common.pathToVal (self, t.$mergeWith, result, common.mergeObjects);
}
}
self.logTask (task, 'task completed');
if (self.isIdle)
self.run ();
else
self.haveCompletedTasks = true;
});
}
});
});