-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.ts
359 lines (302 loc) · 10.1 KB
/
model.ts
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
import { ensureOnlyKeys} from './utils.ts'
import { info, trace, error, debug, subproc } from "./log.ts";
//////////////////// In-Mem Model Objects /////////////////////////
export enum StepState {
none = 'none',
waitingForRun = 'waiting-for-run',
waitingForDep = 'waiting-for-dependency',
depFailed = 'dependency-failed',
succeeded = 'succeeded',
running = 'running',
failed = 'failed',
disabled = 'disabled'
}
export function parseStepState(s: string) {
switch (s) {
case 'none': return StepState.none;
case 'waiting-for-run': return StepState.waitingForRun;
case 'waiting-for-dependency': return StepState.waitingForDep;
case 'dependency-failed': return StepState.depFailed;
case 'succeeded': return StepState.succeeded;
case 'running': return StepState.running;
case 'failed': return StepState.failed;
case 'disabled': return StepState.disabled;
default: throw new Error(`invalid step state ${s}`);
}
}
export enum LogMode {
file = 'file',
console = 'console',
}
export class Step {
constructor(name: string, stepToml: StepToml, state?: StepStateJson) {
this.name = name;
this.env = new Map(Object.entries(stepToml.env ?? {}));
this.cmd = stepToml.cmd;
this.cwd = stepToml.cwd;
this.desc = stepToml.desc;
this.deps = [];
this.tags = stepToml.tags ?? []
ensureOnlyKeys(stepToml, [ "desc", "env", "cmd", "deps", "cwd", "log", "tags" ], `in step ${name}`);
if (stepToml.log == undefined) {
this.logMode = LogMode.file;
} else if (stepToml.log == LogMode.console) {
this.logMode = LogMode.console;
} else if (stepToml.log == LogMode.file) {
this.logMode = LogMode.file;
} else {
throw new Error(`invalid log mode ${stepToml.log} in step ${name}`);
}
if (state) {
this.state = state.state;
this.runs = (state.runs ?? []).map((run) => StepRun.fromJson(run));
this.prevState = state.prevState;
} else {
this.state = StepState.none;
this.runs = [];
}
this.isDirty = this.state == StepState.none;
}
public name: string;
public env: Map<string, string>;
public cmd: string;
public cwd: string;
public isDirty: boolean;
public state: StepState;
public prevState?: StepState;
public runs: StepRun[];
public desc: string | undefined;
public logMode : LogMode;
public tags: string[]
public deps: Step[] = [];
public ancestors: Set<Step> = new Set();
public descendants: Set<Step> = new Set();
public directDescendants: Set<Step> = new Set();
public clean() : void {
debug(`cleaning ${this.name}`);
if (this.isDirty) {
debug(` REALLY cleaning ${this.name}`);
for (let dep of this.deps) {
debug(` cleaning dep ${this.name}=>${dep.name}`);
dep.clean();
}
let depStates = this.deps.map((dep) => dep.state);
debug("clean", this.name, depStates);
if (this.state == StepState.failed) {
debug(" nothing to do (failed)");
} else if (this.state == StepState.disabled) {
debug(" nothing to do (disabled)");
} else if (this.state == StepState.running) {
debug(" nothing to do (running)");
} else if (this.state == StepState.succeeded) {
debug(" nothing to do (succeeded)");
} else if (depStates.includes(StepState.failed) ||
depStates.includes(StepState.depFailed)) {
debug(" case 1 (depFailed)");
this.transition(StepState.depFailed);
} else if (depStates.includes(StepState.running) ||
depStates.includes(StepState.waitingForDep) ||
depStates.includes(StepState.waitingForRun) ||
depStates.includes(StepState.disabled)) {
debug(" case 2 (waitingForDep)");
this.transition(StepState.waitingForDep);
} else if (depStates.length == 0 && this.state == StepState.none) {
debug(" case 3 (waitingForRun)");
this.transition(StepState.waitingForRun);
} else if (depStates.length == 0) {
debug(" case 4 (no deps, no change)");
this.transition(this.state);
} else if (depStates.every(s => s == StepState.succeeded)) {
debug(" case 5 (all deps success)");
this.transition(StepState.waitingForRun);
} else {
debug(" fallthrough");
}
this.isDirty = false;
}
}
public dirty() {
this.isDirty = true;
}
public transition(state: StepState) {
if (state == this.state) return;
trace(`[${this.name}] ${this.state} => ${state}`);
this.prevState = this.state;
this.state = state;
this.dirty();
}
}
function ancestors(step: Step) : Set<Step> {
function accum(acc: Set<Step>, step: Step) : Set<Step> {
for (let d of step.deps) {
if (!(acc.has(d))) {
acc.add(d);
accum(acc, d);
}
}
return acc;
}
return accum(new Set(), step);
}
export class Flow {
public env: Map<string, string>;
public steps: Array<Step>
public finalSteps: Array<Step>
public initialSteps: Array<Step>
constructor(flowToml: FlowToml, stateJson: StateJson) {
ensureOnlyKeys(flowToml, [ "env", "steps" ], `at toplevel`);
this.env = new Map(Object.entries(flowToml.env));
this.steps = Object.entries(flowToml.steps).map(([name,stepToml]) => new Step(name, stepToml, stateJson.steps[name]));
let stepsByName = new Map(this.steps.map((step) => [step.name, step]));
let errors = [];
let cyclicalDepErrors: Array<string> = [];
for (let step of this.steps) {
let stepToml = flowToml.steps[step.name]!;
step.deps = [];
if (stepToml.deps && !Array.isArray(stepToml.deps)) {
errors.push(`step.deps must be an array in step ${step.name}`);
} else {
for (let stepName of stepToml.deps ?? []) {
let dep = stepsByName.get(stepName);
if (dep) {
step.deps.push(dep);
} else {
errors.push(`Dependency not found: ${step.name} => ${stepName}`);
}
}
}
}
function checkCycles(step: Step, seen: Set<Step> = new Set()) {
if (seen.has(step)) {
cyclicalDepErrors.push(`${[...seen, step].map((s) => s.name).join(" => ")}`);
return;
}
seen.add(step);
for (let dep of step.deps) {
checkCycles(dep, seen);
}
seen.delete(step);
}
for (let step of this.steps) {
checkCycles(step);
}
if (cyclicalDepErrors.length > 0) {
errors.push(`Cyclical dependencies found: ${cyclicalDepErrors.join(", ")}`);
}
if (errors.length > 0) {
throw new Error(errors.join("\n"));
}
for (let step of this.steps) {
step.ancestors = ancestors(step);
}
for (let step of this.steps) {
step.descendants = new Set(this.steps.filter((s) => s.ancestors.has(step)));
}
for (let step of this.steps) {
step.directDescendants = new Set(this.steps.filter((s) => s.deps.includes(step)));
}
this.initialSteps = this.steps.filter((step) => step.ancestors.size == 0);
this.finalSteps = this.steps.filter((step) => step.descendants.size == 0);
}
public clean(): void {
for (let step of this.steps) {
step.clean();
}
}
public dirty(): void {
for (let step of this.steps) {
step.dirty();
}
}
/**
* Update the state in `stateJson` to reflect the current state of the world.
*
* This is done as an update as opposed to a `toJson` so that we don't disturb information
* about missing states. Users of the system may comment out parts of their `.toml`
* configuration temporarily and we shouldn't lose track of their runs when they do that.
*
* XXX: when steps reappear, do we need to reset it to StepState.none?
*/
public update(stateJson: StateJson) {
for (let step of this.steps) {
stateJson.steps[step.name] = {
runs: step.runs.map((run) => run.toJson()),
state: step.state,
prevState: step.prevState,
};
}
}
public resolveSteps(stepNames: Array<string>) : Array<Step> {
const steps = [];
for (let stepName of stepNames) {
let found = this.steps.filter(x => x.name == stepName || x.tags.includes(stepName));
for (let step of found) {
steps.push(step);
}
if (found.length == 0) {
throw new Error(`Error: couldn't find any steps for ${stepName}`);
}
}
return steps;
}
}
export class StepRun {
constructor(startTimestamp: Date, endTimestamp?: Date, exitCode?: number, logFile?: string) {
this.startTimestamp = startTimestamp;
this.endTimestamp = endTimestamp;
this.exitCode = exitCode;
this.logFile = logFile;
}
public startTimestamp: Date;
public endTimestamp?: Date;
public exitCode?: number;
public logFile?: string;
public get durationMs(): number | undefined {
return this.endTimestamp ? (this.endTimestamp.getTime() - this.startTimestamp.getTime()) : undefined;
}
public toJson(): StepRunJson {
return {
startTimestamp: this.startTimestamp.toISOString(),
endTimestamp: this.endTimestamp?.toISOString(),
exitCode: this.exitCode,
logFile: this.logFile,
};
}
public static fromJson(json: StepRunJson): StepRun {
return new StepRun(
new Date(json.startTimestamp),
json.endTimestamp ? new Date(json.endTimestamp) : undefined,
json.exitCode,
json.logFile,
);
}
}
//////////////////// On-Disk Formats //////////////////////
export interface StepRunJson {
startTimestamp: string; // ISO8660 Date
endTimestamp?: string; // ISO8660 Date
exitCode?: number;
logFile?: string;
}
export interface StepStateJson {
state: StepState;
prevState?: StepState;
runs: StepRunJson[];
}
export interface StateJson {
version: number;
steps: { [key: string]: StepStateJson };
}
export interface FlowToml {
env: { [key: string]: string };
steps: { [key: string]: StepToml };
}
export interface StepToml {
env: { [key: string]: string };
cmd: string;
cwd: string;
desc?: string;
deps: string[];
tags?: string[];
log: string;
}