-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworkflow.js
177 lines (147 loc) · 4.23 KB
/
workflow.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
'use strict'
const BatchJob = require('./batch-job')
const BatchJobFactory = require('./batch-job-factory')
const hasha = require('hasha')
const shortid = require('shortid')
/**
* Abstract base class for running simple workflows comprised of a sequence of
* individual batch jobs.
*
* TODO: this should eventually be replaced by a more robust solution like
* Apache Kafka.
*/
class Workflow extends BatchJob {
static type = 'workflow'
static getPipelineHash = (pipeline) => {
const input = pipeline.map((job) => job.type).join(',')
return hasha(input, { encoding: 'hex', algorithm: 'md5' })
}
constructor(data, opts) {
const { pipeline } = data.params
if (!pipeline) {
throw new Error(
`Workflow "${data.type}" missing required param "pipeline"`
)
}
const type = data.type || Workflow.type
const id =
data.id ||
`${type}:${Workflow.getPipelineHash(pipeline)}:${shortid.generate()}`
if (
!Array.isArray(pipeline) ||
pipeline.length < 2 ||
!pipeline.every((job) => job.type) ||
!pipeline
.slice(1)
.every((job) => job.connect && Object.keys(job.connect).length >= 1)
) {
throw new Error(`Workflow "${id}" invalid value for param "pipeline"`)
}
super(
{
state: {
jobIndex: 0
},
...data,
type,
id
},
opts
)
}
async _run() {
const { pipeline, ...sharedParams } = this.params
let { jobIndex = 0 } = this.state
let job
while (jobIndex < pipeline.length) {
const jobConfig = pipeline[jobIndex]
if (!jobConfig) {
throw new Error(`Workflow "${this.id}" invalid current job ${jobIndex}`)
}
if (!this.state.pipeline) {
this.state.pipeline = []
}
const jobId = this.state.pipeline[jobIndex]
let jobData
if (jobId) {
jobData = await this._context.db.get(jobId)
} else {
const connectParams = {}
// connect the current job's params to the results of a previous job
// in the pipeline
if (jobIndex > 0 && jobConfig.connect) {
for (const key of Object.keys(jobConfig.connect)) {
const label = jobConfig.connect[key]
let found = false
for (let i = 0; i < jobIndex; ++i) {
const pipelineJobConfig = pipeline[i]
if (pipelineJobConfig.label === label) {
const connectedJobId = this.state.pipeline[i]
const connectedJobData = await this._context.db.get(
connectedJobId
)
if (connectedJobData) {
connectParams[key] = connectedJobData.results
if (connectParams[key]) {
found = true
}
}
break
}
}
if (!found) {
throw new Error(
`Workflow "${this.id}" invalid job ${jobIndex} unable to resolve connected data for "${key}" and label "${label}"`
)
}
}
}
jobData = {
type: jobConfig.type,
params: {
...sharedParams,
...jobConfig.params,
...connectParams
}
}
}
job = BatchJobFactory.deserialize(jobData, this._context)
if (job.status === 'active') {
if (!jobId) {
this.state.pipeline[jobIndex] = job.id
await job.save()
await this._update()
}
this._logger.debug('>>>', this.id, 'job', {
jobIndex,
id: job.id,
status: job.status,
results: job.results.length
})
await job.run()
this._logger.debug('<<<', this.id, 'job', {
jobIndex,
id: job.id,
status: job.status,
results: job.results.length
})
this._logger.debug()
}
if (job.status === 'done') {
this.state.jobIndex = ++jobIndex
await this._update()
} else {
return {
status: job.status,
error: job.error
}
}
}
if (job) {
return {
results: job.results
}
}
}
}
module.exports = Workflow