-
Notifications
You must be signed in to change notification settings - Fork 11
/
task.js
460 lines (396 loc) · 11.9 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
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
'use strict'
const stringify = require('json-stable-stringify')
const defaultConfig = require('./config.js').defaultState
const { hash, tab } = require('../utils.js')
const chalk = require('chalk')
const _ = require('lodash')
// Actions
const CHECK_RESUMABLE = 'task/check-resumable'
const RESOLVE_OUTPUT = 'task/resolve-output'
const RESOLVE_INPUT = 'task/resolve-input'
const CREATE_ACTION = 'task/create-action'
const TASK_LOG = 'task/log'
const SET_VALIDATION = 'task/set-validation'
// Statuses
const {
STATUS_CREATED,
CHECKING_RESUMABLE,
RESOLVING_INPUT
} = require('../constants/task-status-types.js')
const initTask = (task) => {
// null values NEED to be set
// except for container, resolvedInput, resolvedOuput, action
// TODO a "deepObjectAssign", so that properties like params can be extended
const defaultTask = {
threads: defaultConfig.threads,
container: defaultConfig.container,
resume: defaultConfig.resume,
uid: null,
hashes: {
input: null,
output: null,
params: null
},
name: 'Unnamed Task',
dir: null,
input: null,
output: null,
action: null,
status: STATUS_CREATED,
created: null,
validated: false,
params: {}
}
task = Object.assign({}, defaultTask, task, { created: Date.now() })
const hashes = {
input: hash(stringify(task.input)),
output: hash(stringify(task.output)),
params: hash(stringify(task.params))
}
const uid = hash(hashes.input + hashes.output + hashes.params)
Object.assign(task, { hashes, uid })
return task
}
// This reducer cannot have a default state is it is created based on the
// contents of the action. This is somewhat unconvential redux, but all actions
// beyond tasks/create act normal.
const reducer = (state, action) => {
switch (action.type) {
case CHECK_RESUMABLE:
return checkResumableHandler(state, action)
break
case RESOLVE_OUTPUT:
return resolveOutputHandler(state, action)
break
case RESOLVE_INPUT:
return resolveInputHandler(state, action)
break
case CREATE_ACTION:
return createActionHandler(state, action)
break
case TASK_LOG:
// TODO store logs inside state
// TODO automatic logging nesting
console.log(action.content)
return state
case SET_VALIDATION:
return Object.assign({}, state, { validated: action.value })
default:
// TODO second half of reconsider.
// This is where we end up when proxied by tasks reducer
// state will be the task from the tasks/create action ready to have a uid
// created for it.
return initTask(state)
}
}
function checkResumableHandler(state, action) {
const { resume } = state
// TODO handle force-on and force-off as well
if (resume === 'off') {
// No resuming
return Object.assign({}, state, { status: RESOLVING_INPUT })
} else if (resume === 'on') {
// Going to initiate resume attempt
return Object.assign({}, state, { status: CHECKING_RESUMABLE })
} else {
throw new Error('task resume was an unexpected value or undefined')
return state
}
}
reducer.checkResumable = (uid) => (dispatch) => new Promise((resolve, reject) => {
dispatch({
type: CHECK_RESUMABLE,
uid
})
resolve(uid)
})
function resolveOutputHandler(state, { resolvedOutput }) {
return Object.assign({}, state, { resolvedOutput })
}
reducer.resolveOutput = (task) => (dispatch) => new Promise((resolve, reject) => {
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(1) + 'Resolving output patterns to files'
})
const applicator = require('../applicator.js')
const matchToFs = require('../matchers/match-to-fs.js')
const { output } = task
applicator(output, (item) => matchToFs(item, null))
.then((resolvedOutput) => {
dispatch({
type: RESOLVE_OUTPUT,
uid: task.uid,
resolvedOutput
})
resolve(task.uid)
})
.catch(err => {
// Not an actual error: one of the output files was not resolved
console.log(tab(2) + err)
return Promise.resolve()
})
.then(() => resolve(task.uid))
})
reducer.runValidators = (task, mode) => (dispatch) => new Promise((resolve, reject) => {
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(1) + 'Running validations'
})
const { resolvedOutput } = task
const applicator = require('../applicator.js')
const {
isNonNull,
matchHash,
matchTime
} = require('../validators/')
// TODO promise-ify other validators
// validators.matchTime
const validArr = [
applicator(resolvedOutput, isNonNull)
]
if (mode === 'before') {
validArr.push(applicator(resolvedOutput, matchHash))
validArr.push(applicator(resolvedOutput, matchTime))
}
const validations = Promise.all(validArr)
// TODO dispatch something to store validations output
validations
.then(() => {
if (task.status === CHECKING_RESUMABLE) {
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(1) + 'All validations successful'
})
}
dispatch({
type: SET_VALIDATION,
uid: task.uid,
value: true
})
if (mode === 'after') {
const checksummer = require('../checksummer.js')
return applicator(resolvedOutput, checksummer)
.then(() => console.log(' ' + 'Wrote output(s) time+hash to waterwheel.json'))
}
})
.catch((err) => {
dispatch({
type: SET_VALIDATION,
uid: task.uid,
value: false
})
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(2) + err
})
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(1) + 'Could not resolve output before running'
})
})
.then(() => resolve(task.uid))
})
function resolveInputHandler (state, { resolvedInput }) {
return Object.assign({}, state, { resolvedInput })
}
reducer.resolveInput = (task, outputDump, trajectory) => (dispatch) => new Promise((resolve, reject) => {
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(1) + 'Resolving input'
})
// TODO handle from task lineage
const applicator = require('../applicator.js')
const matchToFs = require('../matchers/match-to-fs.js')
const { input } = task
if (_.isNull(input)) {
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(1) + 'Ignoring input as is null'
})
return resolve(task.uid)
}
if (!outputDump) {
applicator(input, (item) => matchToFs(item, null))
.then((resolvedInput) => {
dispatch({
type: TASK_LOG,
uid: task.uid,
content: tab(2) + `Resolved ${chalk.magenta(JSON.stringify(resolvedInput, null, 2))} from ${chalk.blue(JSON.stringify(input, null, 2))}`
})
dispatch({
type: RESOLVE_INPUT,
uid: task.uid,
resolvedInput
})
resolve(task.uid)
})
.catch(err => {
console.log(tab(2) + err)
reject('Input could not be resolved. Exiting early.')
})
} else {
const minimatch = require('minimatch')
const matchToDump = (item) => new Promise((resolve, reject) => {
let currentPlace = outputDump
for (const trajection of trajectory) {
currentPlace = currentPlace[trajection]
for (const key in currentPlace.outputs) {
const val = currentPlace.outputs[key]
// TODO splitting to last piece b/c *.foo patterns do not work
// change this, or figure out edge cases
const doer = (val) => new Promise((resolve, reject) => {
console.log(val, item)
if (minimatch(val.split('/').pop(), item)) {
console.log(tab(2) + `${chalk.magenta(item)} matched to ${chalk.blue(val)}`)
resolve(val)
} else {
// this feels like it will happen too much...
reject()
}
})
applicator(val, doer)
.then(val => resolve(val))
.catch(err => reject('Did not make a match: ', err))
}
}
})
applicator(input, matchToDump)
.then((resolvedInput) => {
dispatch({
type: RESOLVE_INPUT,
uid: task.uid,
resolvedInput
})
resolve(task.uid)
})
.catch(err => console.log(err))
}
})
// TODO better name than 'action'
function createActionHandler (state, { action }) {
return Object.assign({}, state, { action })
}
reducer.createAction = (task, actionCreator) => (dispatch) => new Promise((resolve, reject) => {
const resolvedProps = Object.assign({}, task, { input: task.resolvedInput })
const action = actionCreator(resolvedProps)
dispatch({
type: CREATE_ACTION,
uid: task.uid,
action
})
resolve(task.uid)
})
reducer.setDuplex = (task, stream) => (dispatch) => new Promise((resolve, reject) => {
const { isChildProcess } = require('../utils.js')
const isStream = require('isstream')
const { isWritable, isReadable, isDuplex } = isStream
const action = task.action
const actionType = (() => {
if (isChildProcess(action)) {
return 'process'
} else if (action instanceof Promise) {
return 'promise'
} else if (typeof(action) === 'function') {
// assume action returns function of signature fn(err, data)
return 'curried-callback'
} else if (isStream(action)) {
return 'stream'
} else {
return 'unkown'
}
})()
// 5. running the action
switch(actionType) {
case 'process':
stream.setReadable(action)
console.log(tab(1) + 'Creating a child process')
break
case 'promise':
if (output && output.stream) {
if (output.stream === 'object') {
console.log(tab(1) + 'Creating a Readable for object from promise')
// TODO do this earlier
stream = duplexify.obj(null, null)
stream.end()
stream.setReadable(StreamFromPromise.obj(action))
}
} else {
console.log(tab(1) + 'Creating a Readable for buffer/string from promise')
stream.setReadable(StreamFromPromise(action))
}
break
case 'curried-callback':
stream.setReadable(new StreamFromCallback(action))
break
case 'stream':
if (isReadable(action) && !isWritable(action)) {
stream.setReadable(action)
// console.log('got a readable')
} else if (isWritable(action) && !isReadable(action)) {
stream.setWritable(action)
// console.log('got a writable')
} else {
// Duplex
console.log('elsed to make a duplex')
// TODO sketchy
// const duplexify = require('duplexify')
// stream = duplexify.obj(null, null)
// console.log(action)
stream.setReadable(action)
// Initiale flowing mode
action.on('data', () => {})
}
// console.log(tab(1) + 'Creating a stream from stream')
break
default:
// TODO this is a hack to make join work for now
// stream = new Duplex()
console.log('Bad action type')
}
// TODO dispatch something..
resolve(task.uid)
})
reducer.catchTask = (task, stream) => (dispatch) => new Promise((resolve, reject) => {
// IDEA: create stream._output on end, finish, events, then make
// a close by calling stream.destroy()
// (but wont work for child process)?
stream.on('finish', function (chunk) {
console.log(' ' + 'Stream has finished')
// stream._output = task.resolvedOutput
// stream.destroy()
resolve(task.uid)
if (chunk) {
console.log('Last chunk ' + chunk)
}
})
stream.on('end', function(chunk) {
console.log(' ' + 'Received end internally')
resolve(task.uid)
})
stream.on('close', () => {
console.log(' '+ 'Received close internally')
resolve(task.uid)
})
stream.on('data', function(data) {
if (actionType === 'promise') {
this._resolved = data
}
})
// TODO resolve(uid)
})
reducer.taskLog = (uid, content) => (dispatch) => new Promise((resolve, reject) => {
dispatch({
type: TASK_LOG,
uid,
content
})
resolve(uid)
})
module.exports = reducer