-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·311 lines (261 loc) · 11 KB
/
index.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
var debug = require('debug')('workflow:common')
var _ = require('lodash')
var path = require('path')
var handlebars = require('handlebars')
var fs = require('fs')
var async = require('async')
var util = require('util')
var taskTypeCache = {};
var DEFAULT_TASK_PATH = "/src/tasks/"
var taskPath
var resolvers = {};
var settings = {
logger: console
}
var interpolate = require('./src/interpolation').interpolate
var worksmith
function wfLoader(wf) {
if ("string" === typeof wf) {
wf = path.resolve(wf)
debug("loading workflow file: %s", wf)
wf = require(wf);
}
if(Array.isArray(wf)) {
wf = {
task: "sequence",
items: wf
}
}
return worksmith.define(wf);
}
worksmith = wfLoader
function getStepName(step) {
if (step.name) { return step.name }
var name = step.name || step.task;
if ("function" === typeof name) {
name = "<Anonymous>" + name.name
}
return step.name = name;
}
var workflow = {
use: function(ns, taskLibrary) {
resolvers[ns] = taskLibrary;
},
createAdapter: function(object) {
return function getType(name) {
var method = object[name];
return function define(step) {
return function build(context) {
return function execute(done) {
var args = (context.get(step.arguments) || [])
var result = method.apply(object, args)
done(undefined, result);
}
}
}
}
},
configure: function(options) {
_.assignIn(settings, options);
},
hasLogLevel: function(level) {
return settings.logger[level] !== undefined
},
log: function() {
var level = arguments[0]
settings.logger[level].apply(settings.logger, Array.prototype.slice.call(arguments,1))
},
discoverTaskType: function(taskType) {
var processRelativePath = path.join(process.cwd(), taskPath, taskType + ".js");
return fs.existsSync(processRelativePath) ? processRelativePath : "./tasks/" + taskType + ".js";
},
getTaskType: function(taskType) {
if (taskType.indexOf("/") > -1) {
var taskSpec = taskType.split("/");
var ns = taskSpec[0];
if (resolvers[ns]) {
return resolvers[ns](taskSpec[1]);
}
}
var taskFile = taskTypeCache[taskType] || (taskTypeCache[taskType] = workflow.discoverTaskType(taskType))
return require(taskFile);
},
getWorkflow: function(task) {
if ("string" === typeof task) return workflow.getTaskType(task);
return task;
},
define: function (workflowDefinition) {
debug("defining: %s", getStepName(workflowDefinition))
taskPath = workflowDefinition.taskPath || DEFAULT_TASK_PATH
var WorkflowType = workflow.getWorkflow(workflowDefinition.task)
var wfInstance = WorkflowType.call(wfLoader, workflowDefinition)
function checkCondition(context) {
if (!("condition" in workflowDefinition)) {
return true;
}
with(context) {
if (eval(workflowDefinition.condition)) {
return true;
}
return false;
}
}
function initializeContext(context) {
if (!context.initialized) {
context.get = function(name, interpolate) {
return workflow.readValue(name, this, interpolate)
},
context.set = function(name, value) {
return workflow.setValue(this, name, value)
}
context.initialized = true;
}
}
function getArgumentsFromAnnotations(context, execute, build) {
var args = [];
//TODO: this line is reallly just interim. annotations should be merged or something
var annotations = execute.annotations || build.annotations || WorkflowType.annotations;
annotations = annotations || {};
execute.inject && (annotations.inject = execute.inject);
annotations.inject && annotations.inject.forEach(function(injectable) {
if ("string" === typeof injectable) {
injectable = {
name: injectable,
interpolationPolicy: true,
}
}
var arg;
switch(injectable.name[0]) {
case '@': arg = context.get(injectable.name); break;
default: arg = context.get(workflowDefinition[injectable.name],injectable.interpolationPolicy); break;
}
args.push(arg)
})
return args;
}
return function build(context) {
if (arguments.length == 2) {
return build(arguments[0])(arguments[1])
}
initializeContext(context)
var decorated = wfInstance(context)
debug("preparing: %s", getStepName(workflowDefinition))
var markWorkflowTerminate = function(done) {
context.completionStack = context.completionStack || []
context.completionStack.push(done)
if (!context.workflowTerminate) {
context.workflowTerminate = done
}
}
return function execute(done) {
var wfStartTime = new Date().getTime()
if (!checkCondition(context))
return done()
markWorkflowTerminate(done)
function invokeDecorated(err, res, next) {
function createExecutionThisContext() {
return {
workflow: {
terminate: function(err, res, next) {
context.originalTerminate = done
done = context.workflowTerminate
next(err, res)
},
terminateParent: function(err, res, next, step) {
context.originalTerminate = done
done = context.completionStack[context.completionStack.length - (step || 2)]
next(err, res)
}
}
}
}
var args = getArgumentsFromAnnotations(context, decorated, wfInstance)
args.push(next)
try {
decorated.apply(createExecutionThisContext(), args)
} catch(ex) {
next(ex)
}
}
function onError(err, res, next) {
if (!err) { return next(err, res) }
var errorWfDef = context.get(workflowDefinition.onError);
var errorWf = workflow.define(errorWfDef);
context.error = err;
errorWf(context, function(errHandlerErr, errRes) {
if (errorWfDef.handleError) err = errHandlerErr;
next(err, res);
})
}
function onComplete(err, res, next) {
var finallyDef = context.get(workflowDefinition.finally);
var finallyWf = workflow.define(finallyDef);
finallyWf(context, function(finErr, finRes) {
next(err, res)
})
}
function logErrors(err, result, next) {
if (err) {
debug("error in workflow %s, error is %o", getStepName(workflowDefinition), err.message || err)
//make logging errors a configation options
//if (!err.supressMessage) {
// worksmith.log("error",util.format("Error in WF <%s>, error is:<%s> ", getStepName(workflowDefinition), err.message),err)
//}
}
next(err, result)
}
function setWorkflowResultTo(err, result, next) {
if (err) { return next(err, result) }
process.env.WSDEBUGPARAMS && debug("...result is", result)
context.set(workflowDefinition.resultTo, result)
next(err, result)
}
function buildUpMicroworkflow() {
var tasks = [invokeDecorated];
workflowDefinition.onError && tasks.push(onError)
workflowDefinition.finally && tasks.push(onComplete)
workflowDefinition.resultTo && tasks.push(setWorkflowResultTo)
tasks.push(logErrors)
return tasks;
}
var tasks = buildUpMicroworkflow();
function executeNextThunkOrComplete(err, res) {
var thunk = tasks.shift();
if (thunk) {
return thunk( err, res, executeNextThunkOrComplete)
}
debug("Finished executing WF %s", getStepName(workflowDefinition))
if (context.$$$stats) {
context.$$$stats.push(getStepName(workflowDefinition) + " execution time: " + (new Date().getTime() - wfStartTime) +"ms")
}
var originalDone = context.originalTerminate || done;
var donePosition = context.completionStack.indexOf(originalDone);
context.completionStack.splice(donePosition, 1)
setImmediate(function() {
done(err, res, context)
})
}
debug("Executing WF %s", getStepName(workflowDefinition))
return executeNextThunkOrComplete()
}
}
},
readValue: function(pathOrValue, context, interpolationPolicy) {
return interpolate(context, pathOrValue, interpolationPolicy)
},
setValue: function (object, path, value) {
var parts = path.split('.');
path = path.replace(/\[/g, ".").replace(/\]/g, "")
var part;
while (part = parts.shift()) {
if (parts.length) {
object[part] = object[part] || {};
object = object[part];
} else {
object[part] = value;
}
}
}
}
_.assignIn(wfLoader, workflow);
module.exports = wfLoader;