-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
121 lines (106 loc) · 3.17 KB
/
task.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
/**
* created by maple 2017-01-10
*/
'use strict';
const EventEmitter = require('events').EventEmitter;
const Tigger = require('./lib/trigger');
const ts = require('./lib/timestamp');
module.exports = class Task extends EventEmitter {
constructor (heartbeatCheckTime, taskExpireTime, defaultTime) {
super();
if (typeof heartbeatCheckTime === 'object') {
const {
hbCheckTime: _heartbeatCheckTime,
expireTime: _taskExpireTime,
defaultTime: _defaultTime
} = heartbeatCheckTime;
heartbeatCheckTime = _heartbeatCheckTime;
taskExpireTime = _taskExpireTime;
defaultTime = _defaultTime;
}
// heartbeatCheckTime
this.heartbeatCheckTime = heartbeatCheckTime || 10000;
// taskExpireTime
this.taskExpireTime = taskExpireTime || 20000;
// task name list
this.taskNames = [];
// task list
this.tasks = [];
// task intervalTime list
this.taskIntervalTimes = [];
// task heartbeatTime list
this.heartbeatTimes = [];
// task tigger list
this.tiggers = [];
// task options
this.taskOptions = [];
this.defaultTime = defaultTime;
}
_startTasks () {
const self = this;
// start tasks
self.taskNames.forEach((taskName, index) => {
const taskIntervalTime = self.taskIntervalTimes[index];
const task = self.tasks[index];
const t = new Tigger(taskIntervalTime);
const options = self.taskOptions[index];
t.on('done', () => {
self.heartbeatTimes[index] = ts.get();
let beginTime;
if (options.timing) {
beginTime = ts.getMs();
}
task(() => {
if (beginTime && options.timing) {
self.emit('log', `【 TASK 】${taskName} RUN TIME ${ts.getMs() - beginTime} ms`);
}
});
self.emit('log', `【 TASK 】${taskName} START RUN at time ${ts.toLocaleString()}`);
});
t.start();
self.tiggers[index] = t;
});
}
run () {
// start check tasks
this._startTasks();
this.emit('start');
}
stop () {
const tiggers = this.tiggers;
try {
for (const t of tiggers) {
if (!t || typeof t.stop !== 'function') continue;
t.stop();
t.removeAllListeners();
}
} catch (e) {
this.emit('error', e);
}
this.emit('stop');
}
addTask (taskName, task, taskIntervalTime, options) {
if (typeof taskIntervalTime === 'object') {
options = taskIntervalTime;
taskIntervalTime = null;
}
// 默认间隔时间
if (!taskIntervalTime) {
taskIntervalTime = this.defaultTime;
}
if (!taskName || !parseInt(taskIntervalTime)) {
return this.emit('error', new Error('illegal taskName & intervalTime'));
}
if (!task || typeof task !== 'function') {
return this.emit('error', new Error('task should be function'));
}
if (this.taskNames.indexOf(taskName) > -1) {
return this.emit('error', new Error(`taskName ${taskName} is existed!`));
}
this.taskNames.push(taskName);
this.tasks.push(task);
this.taskIntervalTimes.push(parseInt(taskIntervalTime));
this.heartbeatTimes.push(null);
this.taskOptions.push(options || {});
}
};