-
Notifications
You must be signed in to change notification settings - Fork 587
/
Operation.ts
346 lines (298 loc) · 12 KB
/
Operation.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { InternalError, type ITerminal } from '@rushstack/node-core-library';
import { Stopwatch } from './Stopwatch';
import type {
IOperationRunner,
IOperationRunnerContext,
IOperationState,
IOperationStates
} from './IOperationRunner';
import type { OperationError } from './OperationError';
import { OperationStatus } from './OperationStatus';
/**
* Options for constructing a new Operation.
* @beta
*/
export interface IOperationOptions {
/**
* The name of this operation, for logging.
*/
name?: string | undefined;
/**
* The group that this operation belongs to. Will be used for logging and duration tracking.
*/
groupName?: string | undefined;
/**
* When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of
* running the operation.
*/
runner?: IOperationRunner | undefined;
/**
* The weight used by the scheduler to determine order of execution.
*/
weight?: number | undefined;
}
/**
* Information provided to `executeAsync` by the `OperationExecutionManager`.
*
* @beta
*/
export interface IExecuteOperationContext extends Omit<IOperationRunnerContext, 'isFirstRun' | 'requestRun'> {
/**
* Function to invoke before execution of an operation, for logging.
*/
beforeExecute(operation: Operation, state: IOperationState): void;
/**
* Function to invoke after execution of an operation, for logging.
*/
afterExecute(operation: Operation, state: IOperationState): void;
/**
* Function used to schedule the concurrency-limited execution of an operation.
*
* Will return OperationStatus.Aborted if execution is aborted before the task executes.
*/
queueWork(workFn: () => Promise<OperationStatus>, priority: number): Promise<OperationStatus>;
/**
* A callback to the overarching orchestrator to request that the operation be invoked again.
* Used in watch mode to signal that inputs have changed.
*/
requestRun?: (requestor?: string) => void;
/**
* Terminal to write output to.
*/
terminal: ITerminal;
}
/**
* The `Operation` class is a node in the dependency graph of work that needs to be scheduled by the
* `OperationExecutionManager`. Each `Operation` has a `runner` member of type `IOperationRunner`, whose
* implementation manages the actual process of running a single operation.
*
* The graph of `Operation` instances will be cloned into a separate execution graph after processing.
*
* @beta
*/
export class Operation implements IOperationStates {
/**
* A set of all dependencies which must be executed before this operation is complete.
*/
public readonly dependencies: Set<Operation> = new Set<Operation>();
/**
* A set of all operations that wait for this operation.
*/
public readonly consumers: Set<Operation> = new Set<Operation>();
/**
* If specified, the name of a grouping to which this Operation belongs, for logging start and end times.
*/
public readonly groupName: string | undefined;
/**
* The name of this operation, for logging.
*/
public readonly name: string | undefined;
/**
* When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of
* running the operation.
*/
public runner: IOperationRunner | undefined = undefined;
/**
* This number represents how far away this Operation is from the furthest "root" operation (i.e.
* an operation with no consumers). This helps us to calculate the critical path (i.e. the
* longest chain of projects which must be executed in order, thereby limiting execution speed
* of the entire operation tree.
*
* This number is calculated via a memoized depth-first search, and when choosing the next
* operation to execute, the operation with the highest criticalPathLength is chosen.
*
* Example:
* (0) A
* \\
* (1) B C (0) (applications)
* \\ /|\\
* \\ / | \\
* (2) D | X (1) (utilities)
* | / \\
* |/ \\
* (2) Y Z (2) (other utilities)
*
* All roots (A & C) have a criticalPathLength of 0.
* B has a score of 1, since A depends on it.
* D has a score of 2, since we look at the longest chain (e.g D-\>B-\>A is longer than D-\>C)
* X has a score of 1, since the only package which depends on it is A
* Z has a score of 2, since only X depends on it, and X has a score of 1
* Y has a score of 2, since the chain Y-\>X-\>C is longer than Y-\>C
*
* The algorithm is implemented in AsyncOperationQueue.ts as calculateCriticalPathLength()
*/
public criticalPathLength: number | undefined = undefined;
/**
* The weight for this operation. This scalar is the contribution of this operation to the
* `criticalPathLength` calculation above. Modify to indicate the following:
* - `weight` === 1: indicates that this operation has an average duration
* - `weight` > 1: indicates that this operation takes longer than average and so the scheduler
* should try to favor starting it over other, shorter operations. An example might be an operation that
* bundles an entire application and runs whole-program optimization.
* - `weight` < 1: indicates that this operation takes less time than average and so the scheduler
* should favor other, longer operations over it. An example might be an operation to unpack a cached
* output, or an operation using NullOperationRunner, which might use a value of 0.
*/
public weight: number;
/**
* The state of this operation the previous time a manager was invoked.
*/
public lastState: IOperationState | undefined = undefined;
/**
* The current state of this operation
*/
public state: IOperationState | undefined = undefined;
/**
* A cached execution promise for the current OperationExecutionManager invocation of this operation.
*/
private _promise: Promise<OperationStatus> | undefined = undefined;
/**
* If true, then a run of this operation is currently wanted.
* This is used to track state from the `requestRun` callback passed to the runner.
*/
private _runPending: boolean = true;
public constructor(options?: IOperationOptions) {
this.groupName = options?.groupName;
this.runner = options?.runner;
this.weight = options?.weight || 1;
this.name = options?.name;
}
public addDependency(dependency: Operation): void {
this.dependencies.add(dependency);
dependency.consumers.add(this);
}
public deleteDependency(dependency: Operation): void {
this.dependencies.delete(dependency);
dependency.consumers.delete(this);
}
public reset(): void {
// Reset operation state
this.lastState = this.state;
this.state = {
status: this.dependencies.size > 0 ? OperationStatus.Waiting : OperationStatus.Ready,
hasBeenRun: this.lastState?.hasBeenRun ?? false,
error: undefined,
stopwatch: new Stopwatch()
};
this._promise = undefined;
this._runPending = true;
}
/**
* @internal
*/
public async _executeAsync(context: IExecuteOperationContext): Promise<OperationStatus> {
const { state } = this;
if (!state) {
throw new Error(`Operation state has not been initialized.`);
}
if (!this._promise) {
this._promise = this._executeInnerAsync(context, state);
}
return this._promise;
}
private async _executeInnerAsync(
context: IExecuteOperationContext,
rawState: IOperationState
): Promise<OperationStatus> {
const state: IOperationState = rawState;
const { runner } = this;
const dependencyResults: PromiseSettledResult<OperationStatus>[] = await Promise.allSettled(
Array.from(this.dependencies, (dependency: Operation) => dependency._executeAsync(context))
);
const { abortSignal, requestRun, queueWork } = context;
if (abortSignal.aborted) {
state.status = OperationStatus.Aborted;
return state.status;
}
for (const result of dependencyResults) {
if (
result.status === 'rejected' ||
result.value === OperationStatus.Blocked ||
result.value === OperationStatus.Failure
) {
state.status = OperationStatus.Blocked;
return state.status;
}
}
state.status = OperationStatus.Ready;
const innerContext: IOperationRunnerContext = {
abortSignal,
isFirstRun: !state.hasBeenRun,
requestRun: requestRun
? () => {
switch (this.state?.status) {
case OperationStatus.Waiting:
case OperationStatus.Ready:
case OperationStatus.Executing:
// If current status has not yet resolved to a fixed value,
// re-executing this operation does not require a full rerun
// of the operation graph. Simply mark that a run is requested.
// This variable is on the Operation instead of the
// containing closure to deal with scenarios in which
// the runner hangs on to an old copy of the callback.
this._runPending = true;
return;
case OperationStatus.Blocked:
case OperationStatus.Aborted:
case OperationStatus.Failure:
case OperationStatus.NoOp:
case OperationStatus.Success:
// The requestRun callback is assumed to remain constant
// throughout the lifetime of the process, so it is safe
// to capture here.
return requestRun(this.name);
default:
// This line is here to enforce exhaustiveness
const currentStatus: undefined = this.state?.status;
throw new InternalError(`Unexpected status: ${currentStatus}`);
}
}
: undefined
};
// eslint-disable-next-line require-atomic-updates
state.status = await queueWork(async (): Promise<OperationStatus> => {
// Redundant variable to satisfy require-atomic-updates
const innerState: IOperationState = state;
if (abortSignal.aborted) {
innerState.status = OperationStatus.Aborted;
return innerState.status;
}
context.beforeExecute(this, innerState);
innerState.stopwatch.start();
innerState.status = OperationStatus.Executing;
// Mark that the operation has been started at least once.
innerState.hasBeenRun = true;
while (this._runPending) {
this._runPending = false;
try {
// We don't support aborting in the middle of a runner's execution.
innerState.status = runner ? await runner.executeAsync(innerContext) : OperationStatus.NoOp;
} catch (error) {
innerState.status = OperationStatus.Failure;
innerState.error = error as OperationError;
}
// Since runner.executeAsync is async, a change could have occurred that requires re-execution
// This operation is still active, so can re-execute immediately, rather than forcing a whole
// new execution pass.
// As currently written, this does mean that if a job is scheduled with higher priority while
// this operation is still executing, it will still wait for this retry. This may not be desired
// and if it becomes a problem, the retry loop will need to be moved outside of the `queueWork` call.
// This introduces complexity regarding tracking of timing and start/end logging, however.
if (this._runPending) {
if (abortSignal.aborted) {
innerState.status = OperationStatus.Aborted;
break;
} else {
context.terminal.writeLine(`Immediate rerun requested. Executing.`);
}
}
}
state.stopwatch.stop();
context.afterExecute(this, state);
return state.status;
}, /* priority */ this.criticalPathLength ?? 0);
return state.status;
}
}