-
Notifications
You must be signed in to change notification settings - Fork 4
/
job-runner.js
197 lines (151 loc) · 4.84 KB
/
job-runner.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
'use strict';
var assert = require('chai').assert;
var Stopwatch = require('statman-stopwatch');
var E = require('linq');
//
// A higher level manager for tasks. Runs tasks and coordinates error handling and logging.
//
var JobRunner = function (taskRunner, log, callbacks) {
var self = this;
assert.isObject(taskRunner);
assert.isFunction(log.info);
assert.isObject(callbacks);
if (callbacks.unhandledExceptionCallback) {
assert.isFunction(callbacks.unhandledException);
}
//
// Add a task.
//
self.addTask = function (task) {
assert.isObject(task);
taskrunner.addTask(task);
};
//
// Get a task by name, throws exception if task doesn't exist.
//
self.getTask = function (taskName) {
assert.isString(taskName);
return taskRunner.getTask(taskName);
};
var taskStarted = function (taskName) {
var promise;
if (callbacks.taskStarted) {
promise = callbacks.taskStarted({
name: taskName
});
}
if (!promise) {
promise = Promise.resolve();
}
return promise;
};
var taskSuccess = function (taskName, stopwatch) {
var promise;
if (callbacks.taskSuccess) {
var elapsedTimeMins = stopwatch.read()/1000.0/60.0;
promise = callbacks.taskSuccess({ name: taskName }, elapsedTimeMins);
}
if (!promise) {
promise = Promise.resolve();
}
return promise;
};
var taskFailed = function (taskName, err, stopwatch) {
var promise;
if (callbacks.taskFailure) {
var elapsedTimeMins = stopwatch.read()/1000.0/60.0;
promise = callbacks.taskFailure({ name: taskName }, elapsedTimeMins, err);
}
if (!promise) {
promise = Promise.resolve();
}
return promise;
};
var taskDone = function (taskName) {
var promise;
if (callbacks.taskDone) {
promise = callbacks.taskDone({ name: taskName });
}
if (!promise) {
promise = Promise.resolve();
}
return promise;
};
//
// Run a named task with a particular config.
//
self.runTask = function (taskName, config, configOverride) {
configOverride = configOverride || {};
assert.isString(taskName);
assert.isObject(config);
assert.isObject(configOverride);
var stopwatch = new Stopwatch();
stopwatch.start();
var uncaughtExceptionCount = 0;
var uncaughtExceptionHandler = function (err) {
++uncaughtExceptionCount;
if (callbacks.unhandledException) {
callbacks.unhandledException(err);
}
else {
log.error("Unhandled exception occurred.");
log.error(err && err.stack || err);
}
};
process.on('uncaughtException', uncaughtExceptionHandler);
return taskStarted(taskName)
.then(function () {
return taskRunner.runTask(taskName, config, configOverride);
})
.then(function () {
if (uncaughtExceptionCount > 0) {
throw new Error(' Unhandled exceptions (' + uncaughtExceptionCount + ') occurred while running task ' + taskName);
};
})
.then(function () {
stopwatch.stop();
process.removeListener('uncaughtException', uncaughtExceptionHandler);
return taskSuccess(taskName, stopwatch);
})
.then(function () {
return taskDone(taskName);
})
.catch(function (err) {
stopwatch.stop();
process.removeListener('uncaughtException', uncaughtExceptionHandler);
return taskFailed(taskName, err, stopwatch)
.then(function () {
throw err;
});
})
.catch(function (err) {
return taskDone(taskName)
.then(function () {
throw err;
})
})
;
};
//
// List registered tasks.
//
self.listTasks = function () {
taskRunner.listTasks();
};
//
// Resolve dependencies for all tasks.
//
self.resolveAllDependencies = function (config) {
assert.isObject(config);
return taskRunner.resolveAllDependencies(config);
};
//
// Resolve dependencies for a particular task.
//
self.resolveDependencies = function (taskName, config) {
assert.isString(taskName);
assert.isObject(config);
return taskRunner.resolveDependencies(taskName, config);
};
};
module.exports = JobRunner;