/
task-orchestrator.ts
172 lines (153 loc) · 4.54 KB
/
task-orchestrator.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
import { Cache, TaskWithCachedResult } from './cache';
import { cliCommand } from '../core/file-utils';
import { ProjectGraph } from '../core/project-graph';
import { AffectedEventType, Task } from './tasks-runner';
import { getCommand, getOutputs } from './utils';
import { fork, spawn } from 'child_process';
import { DefaultTasksRunnerOptions } from './tasks-runner-v2';
import { output } from '../utils/output';
import * as path from 'path';
import { appRootPath } from '../utils/app-root';
export class TaskOrchestrator {
workspaceRoot = appRootPath;
cache = new Cache(this.options);
cli = cliCommand();
constructor(
private readonly projectGraph: ProjectGraph,
private readonly options: DefaultTasksRunnerOptions
) {}
async run(tasksInStage: Task[]) {
const { cached, rest } = await this.splitTasksIntoCachedAndNotCached(
tasksInStage
);
const r1 = await this.applyCachedResults(cached);
const r2 = await this.runRest(rest);
this.cache.removeOldCacheRecords();
return [...r1, ...r2];
}
private async runRest(tasks: Task[]) {
const left = [...tasks];
const res = [];
const that = this;
function takeFromQueue() {
if (left.length > 0) {
const task = left.pop();
return that
.forkProcess(task)
.then(code => {
res.push({
task,
success: code === 0,
type: AffectedEventType.TaskComplete
});
})
.then(takeFromQueue)
.catch(takeFromQueue);
} else {
return Promise.resolve(null);
}
}
const wait = [];
// initial seeding
const maxParallel = this.options.parallel
? this.options.maxParallel || 3
: 1;
for (let i = 0; i < maxParallel; ++i) {
wait.push(takeFromQueue());
}
await Promise.all(wait);
return res;
}
private async splitTasksIntoCachedAndNotCached(
tasks: Task[]
): Promise<{ cached: TaskWithCachedResult[]; rest: Task[] }> {
const cached: TaskWithCachedResult[] = [];
const rest: Task[] = [];
await Promise.all(
tasks.map(async task => {
const cachedResult = await this.cache.get(task);
if (cachedResult) {
cached.push({ task, cachedResult });
} else {
rest.push(task);
}
})
);
return { cached, rest };
}
private applyCachedResults(tasks: TaskWithCachedResult[]) {
tasks.forEach(t => {
this.options.lifeCycle.startTask(t.task);
output.note({ title: `Cached Output:` });
process.stdout.write(t.cachedResult.terminalOutput);
const outputs = getOutputs(this.projectGraph.nodes, t.task);
this.cache.copyFilesFromCache(t.cachedResult, outputs);
this.options.lifeCycle.endTask(t.task, 0);
});
return tasks.reduce((m, c) => {
m.push({
task: c.task,
type: AffectedEventType.TaskCacheRead,
success: true
});
return m;
}, []);
}
private forkProcess(task: Task) {
const taskOutputs = getOutputs(this.projectGraph.nodes, task);
const outputPath = this.cache.temporaryOutputPath(task);
return new Promise((res, rej) => {
try {
this.options.lifeCycle.startTask(task);
const env = { ...process.env };
if (outputPath) {
env.NX_TERMINAL_OUTPUT_PATH = outputPath;
if (this.options.captureStderr) {
env.NX_TERMINAL_CAPTURE_STDERR = 'true';
}
}
const p = fork(this.getCommand(), this.getCommandArgs(task), {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env
});
p.on('close', code => {
if (outputPath && code === 0) {
this.cache.put(task, outputPath, taskOutputs).then(() => {
this.options.lifeCycle.endTask(task, code);
res(code);
});
} else {
this.options.lifeCycle.endTask(task, code);
res(code);
}
});
} catch (e) {
console.error(e);
rej(e);
}
});
}
private getCommand() {
return path.join(
this.workspaceRoot,
'node_modules',
'@nrwl',
'cli',
'lib',
'run-cli.js'
);
}
private getCommandArgs(task: Task) {
const args = Object.entries(task.overrides || {}).map(
([prop, value]) => `--${prop}=${value}`
);
const config = task.target.configuration
? `:${task.target.configuration}`
: '';
return [
'run',
`${task.target.project}:${task.target.target}${config}`,
...args
];
}
}