UNPKG

1.54 kBJavaScriptView Raw
1var EventEmitter = require ('events').EventEmitter,
2 util = require ('util'),
3 flow = require ('../flow');
4
5var timeri = module.exports = function (config) {
6 var self = this;
7
8 // here we must define what timer events we must watch:
9 // 1. at precise time - unix time (precise: 1223312333221312) ???
10 // 2. once after timeout, in milliseconds (timeout: 2000)
11 // 3. using interval, in milliseconds (every: 1000)
12
13 this.flows = config.workflows || config.dataflows || config.flows;
14
15 this.ready ();
16}
17
18util.inherits (timeri, EventEmitter);
19
20timeri.prototype.ready = function () {
21
22 var self = this;
23
24 self.flows.map(function (flowParams) {
25
26 var dfCycle = {};
27
28 dfCycle.run = function (rerun) {
29
30 var wf = new flow (
31 util.extend (true, {}, flowParams),
32 {
33 timestamp: Date.now()
34 }
35 );
36
37 if (rerun) {
38
39 wf.on ('completed', function () {
40 dfCycle.end();
41 });
42
43 wf.on ('failed', function () {
44 dfCycle.end();
45 });
46
47 }
48
49 wf.runDelayed ();
50
51 };
52
53 dfCycle.end = function() {
54
55 setTimeout(function() {
56 dfCycle.run(true);
57 }, flowParams.interval);
58
59 };
60
61 if (flowParams.interval) {
62
63 if (flowParams.startRun) {
64
65 if (flowParams.delay) {
66
67 setTimeout (function() {
68 dfCycle.run(true);
69 }, flowParams.delay);
70
71 } else {
72
73 dfCycle.run(true);
74
75 }
76
77 } else {
78 dfCycle.end();
79 }
80
81 } else if (flowParams.timeout) {
82
83 setTimeout (function() {
84
85 dfCycle.run(false);
86
87 }, flowParams.timeout);
88
89 }
90 });
91
92 self.emit ('ready', this);
93}