/
tasks-schedule.ts
172 lines (147 loc) · 4.67 KB
/
tasks-schedule.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 { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import {
calculateReverseDeps,
getCustomHasher,
getExecutorForTask,
getExecutorNameForTask,
removeTasksFromTaskGraph,
} from './utils';
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { Hasher } from '@nrwl/workspace/src/core/hasher/hasher';
export interface Batch {
executorName: string;
taskGraph: TaskGraph;
}
export class TasksSchedule {
private notScheduledTaskGraph = this.taskGraph;
private reverseTaskDeps = calculateReverseDeps(this.taskGraph);
private scheduledBatches: Batch[] = [];
private scheduledTasks: string[] = [];
private completedTasks = new Set<string>();
constructor(
private readonly hasher: Hasher,
private taskGraph: TaskGraph,
private workspace: Workspaces,
private options: DefaultTasksRunnerOptions,
private readonly projectGraph: ProjectGraph
) {}
public async scheduleNextTasks() {
if (process.env.NX_BATCH_MODE === 'true') {
this.scheduleBatches();
}
for (let root of this.notScheduledTaskGraph.roots) {
if (this.canBeScheduled(root)) {
await this.scheduleTask(root);
}
}
}
public hasTasks() {
return (
this.scheduledBatches.length +
this.scheduledTasks.length +
Object.keys(this.notScheduledTaskGraph.tasks).length !==
0
);
}
public complete(taskIds: string[]) {
for (const taskId of taskIds) {
this.completedTasks.add(taskId);
}
this.notScheduledTaskGraph = removeTasksFromTaskGraph(
this.notScheduledTaskGraph,
taskIds
);
}
public nextTask() {
if (this.scheduledTasks.length > 0) {
return this.taskGraph.tasks[this.scheduledTasks.shift()];
} else {
return null;
}
}
public nextBatch(): Batch {
return this.scheduledBatches.length > 0
? this.scheduledBatches.shift()
: null;
}
private async scheduleTask(taskId: string) {
const task = this.taskGraph.tasks[taskId];
await this.hashTask(task);
this.notScheduledTaskGraph = removeTasksFromTaskGraph(
this.notScheduledTaskGraph,
[taskId]
);
this.scheduledTasks.push(taskId);
// TODO vsavkin: remove the if statement after Nx 14 is out
if (this.options.lifeCycle.scheduleTask) {
this.options.lifeCycle.scheduleTask(task);
}
}
private scheduleBatches() {
const batchMap: Record<string, TaskGraph> = {};
for (const root of this.notScheduledTaskGraph.roots) {
const rootTask = this.notScheduledTaskGraph.tasks[root];
const executorName = getExecutorNameForTask(rootTask, this.workspace);
this.processTaskForBatches(batchMap, rootTask, executorName, true);
}
for (const [executorName, taskGraph] of Object.entries(batchMap)) {
this.scheduleBatch({ executorName, taskGraph });
}
}
private scheduleBatch({ executorName, taskGraph }: Batch) {
// Create a new task graph without the tasks that are being scheduled as part of this batch
this.notScheduledTaskGraph = removeTasksFromTaskGraph(
this.notScheduledTaskGraph,
Object.keys(taskGraph.tasks)
);
this.scheduledBatches.push({ executorName, taskGraph });
}
private processTaskForBatches(
batches: Record<string, TaskGraph>,
task: Task,
rootExecutorName: string,
isRoot: boolean
) {
const { batchImplementationFactory } = getExecutorForTask(
task,
this.workspace
);
const executorName = getExecutorNameForTask(task, this.workspace);
if (rootExecutorName !== executorName) {
return;
}
if (!batchImplementationFactory) {
return;
}
const batch = (batches[rootExecutorName] =
batches[rootExecutorName] ??
({
tasks: {},
dependencies: {},
roots: [],
} as TaskGraph));
batch.tasks[task.id] = task;
batch.dependencies[task.id] = this.taskGraph.dependencies[task.id];
if (isRoot) {
batch.roots.push(task.id);
}
for (const dep of this.reverseTaskDeps[task.id]) {
const depTask = this.taskGraph.tasks[dep];
this.processTaskForBatches(batches, depTask, rootExecutorName, false);
}
}
private canBeScheduled(taskId: string) {
return this.taskGraph.dependencies[taskId].every((id) =>
this.completedTasks.has(id)
);
}
private async hashTask(task: Task) {
const customHasher = getCustomHasher(task, this.workspace);
const { value, details } = await (customHasher
? customHasher(task, this.taskGraph, this.hasher, this.projectGraph)
: this.hasher.hashTaskWithDepsAndContext(task));
task.hash = value;
task.hashDetails = details;
}
}