@rushstack/operation-graph 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +12 -0
- package/dist/operation-graph.d.ts +572 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/lib/IOperationRunner.d.ts +85 -0
- package/lib/IOperationRunner.d.ts.map +1 -0
- package/lib/IOperationRunner.js +5 -0
- package/lib/IOperationRunner.js.map +1 -0
- package/lib/Operation.d.ts +155 -0
- package/lib/Operation.d.ts.map +1 -0
- package/lib/Operation.js +213 -0
- package/lib/Operation.js.map +1 -0
- package/lib/OperationError.d.ts +12 -0
- package/lib/OperationError.d.ts.map +1 -0
- package/lib/OperationError.js +29 -0
- package/lib/OperationError.js.map +1 -0
- package/lib/OperationExecutionManager.d.ts +46 -0
- package/lib/OperationExecutionManager.d.ts.map +1 -0
- package/lib/OperationExecutionManager.js +127 -0
- package/lib/OperationExecutionManager.js.map +1 -0
- package/lib/OperationGroupRecord.d.ts +25 -0
- package/lib/OperationGroupRecord.d.ts.map +1 -0
- package/lib/OperationGroupRecord.js +65 -0
- package/lib/OperationGroupRecord.js.map +1 -0
- package/lib/OperationStatus.d.ts +39 -0
- package/lib/OperationStatus.d.ts.map +1 -0
- package/lib/OperationStatus.js +45 -0
- package/lib/OperationStatus.js.map +1 -0
- package/lib/Stopwatch.d.ts +47 -0
- package/lib/Stopwatch.d.ts.map +1 -0
- package/lib/Stopwatch.js +97 -0
- package/lib/Stopwatch.js.map +1 -0
- package/lib/WatchLoop.d.ts +80 -0
- package/lib/WatchLoop.d.ts.map +1 -0
- package/lib/WatchLoop.js +191 -0
- package/lib/WatchLoop.js.map +1 -0
- package/lib/calculateCriticalPath.d.ts +22 -0
- package/lib/calculateCriticalPath.d.ts.map +1 -0
- package/lib/calculateCriticalPath.js +89 -0
- package/lib/calculateCriticalPath.js.map +1 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +20 -0
- package/lib/index.js.map +1 -0
- package/lib/protocol.types.d.ts +80 -0
- package/lib/protocol.types.d.ts.map +1 -0
- package/lib/protocol.types.js +5 -0
- package/lib/protocol.types.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
|
+
// See LICENSE in the project root for license information.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
//# sourceMappingURL=IOperationRunner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IOperationRunner.js","sourceRoot":"","sources":["../src/IOperationRunner.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport type { OperationStatus } from './OperationStatus';\nimport type { OperationError } from './OperationError';\n\nimport type { Stopwatch } from './Stopwatch';\n\n/**\n * Information passed to the executing `IOperationRunner`\n *\n * @beta\n */\nexport interface IOperationRunnerContext {\n /**\n * An abort signal for the overarching execution. Runners should do their best to gracefully abort\n * as soon as possible if the signal is aborted.\n */\n abortSignal: AbortSignal;\n\n /**\n * If this is the first time this operation has been executed.\n */\n isFirstRun: boolean;\n\n /**\n * A callback to the overarching orchestrator to request that the operation be invoked again.\n * Used in watch mode to signal that inputs have changed.\n */\n requestRun?: () => void;\n}\n\n/**\n * Interface contract for a single state of an operation.\n *\n * @beta\n */\nexport interface IOperationState {\n /**\n * The status code for the operation.\n */\n status: OperationStatus;\n /**\n * Whether the operation has been run at least once.\n */\n hasBeenRun: boolean;\n /**\n * The error, if the status is `OperationStatus.Failure`.\n */\n error: OperationError | undefined;\n /**\n * Timing information for the operation.\n */\n stopwatch: Stopwatch;\n}\n\n/**\n * Interface contract for the current and past state of an operation.\n *\n * @beta\n */\nexport interface IOperationStates {\n /**\n * The current state of the operation.\n */\n readonly state: Readonly<IOperationState> | undefined;\n /**\n * The previous state of the operation.\n */\n readonly lastState: Readonly<IOperationState> | undefined;\n}\n\n/**\n * The `Operation` class is a node in the dependency graph of work that needs to be scheduled by the\n * `OperationExecutionManager`. Each `Operation` has a `runner` member of type `IOperationRunner`, whose\n * implementation manages the actual process for running a single operation.\n *\n * @beta\n */\nexport interface IOperationRunner {\n /**\n * Name of the operation, for logging.\n */\n readonly name: string;\n\n /**\n * Indicates that this runner is architectural and should not be reported on.\n */\n silent: boolean;\n\n /**\n * Method to be executed for the operation.\n */\n executeAsync(context: IOperationRunnerContext): Promise<OperationStatus>;\n}\n"]}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ITerminal } from '@rushstack/node-core-library';
|
|
2
|
+
import type { IOperationRunner, IOperationRunnerContext, IOperationState, IOperationStates } from './IOperationRunner';
|
|
3
|
+
import { OperationStatus } from './OperationStatus';
|
|
4
|
+
/**
|
|
5
|
+
* Options for constructing a new Operation.
|
|
6
|
+
* @beta
|
|
7
|
+
*/
|
|
8
|
+
export interface IOperationOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The name of this operation, for logging.
|
|
11
|
+
*/
|
|
12
|
+
name?: string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The group that this operation belongs to. Will be used for logging and duration tracking.
|
|
15
|
+
*/
|
|
16
|
+
groupName?: string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of
|
|
19
|
+
* running the operation.
|
|
20
|
+
*/
|
|
21
|
+
runner?: IOperationRunner | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* The weight used by the scheduler to determine order of execution.
|
|
24
|
+
*/
|
|
25
|
+
weight?: number | undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Information provided to `executeAsync` by the `OperationExecutionManager`.
|
|
29
|
+
*
|
|
30
|
+
* @beta
|
|
31
|
+
*/
|
|
32
|
+
export interface IExecuteOperationContext extends Omit<IOperationRunnerContext, 'isFirstRun' | 'requestRun'> {
|
|
33
|
+
/**
|
|
34
|
+
* Function to invoke before execution of an operation, for logging.
|
|
35
|
+
*/
|
|
36
|
+
beforeExecute(operation: Operation, state: IOperationState): void;
|
|
37
|
+
/**
|
|
38
|
+
* Function to invoke after execution of an operation, for logging.
|
|
39
|
+
*/
|
|
40
|
+
afterExecute(operation: Operation, state: IOperationState): void;
|
|
41
|
+
/**
|
|
42
|
+
* Function used to schedule the concurrency-limited execution of an operation.
|
|
43
|
+
*/
|
|
44
|
+
queueWork<T>(workFn: () => Promise<T>, priority: number): Promise<T>;
|
|
45
|
+
/**
|
|
46
|
+
* A callback to the overarching orchestrator to request that the operation be invoked again.
|
|
47
|
+
* Used in watch mode to signal that inputs have changed.
|
|
48
|
+
*/
|
|
49
|
+
requestRun?: (requestor?: string) => void;
|
|
50
|
+
/**
|
|
51
|
+
* Terminal to write output to.
|
|
52
|
+
*/
|
|
53
|
+
terminal: ITerminal;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The `Operation` class is a node in the dependency graph of work that needs to be scheduled by the
|
|
57
|
+
* `OperationExecutionManager`. Each `Operation` has a `runner` member of type `IOperationRunner`, whose
|
|
58
|
+
* implementation manages the actual process of running a single operation.
|
|
59
|
+
*
|
|
60
|
+
* The graph of `Operation` instances will be cloned into a separate execution graph after processing.
|
|
61
|
+
*
|
|
62
|
+
* @beta
|
|
63
|
+
*/
|
|
64
|
+
export declare class Operation implements IOperationStates {
|
|
65
|
+
/**
|
|
66
|
+
* A set of all dependencies which must be executed before this operation is complete.
|
|
67
|
+
*/
|
|
68
|
+
readonly dependencies: Set<Operation>;
|
|
69
|
+
/**
|
|
70
|
+
* A set of all operations that wait for this operation.
|
|
71
|
+
*/
|
|
72
|
+
readonly consumers: Set<Operation>;
|
|
73
|
+
/**
|
|
74
|
+
* If specified, the name of a grouping to which this Operation belongs, for logging start and end times.
|
|
75
|
+
*/
|
|
76
|
+
readonly groupName: string | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* The name of this operation, for logging.
|
|
79
|
+
*/
|
|
80
|
+
readonly name: string | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of
|
|
83
|
+
* running the operation.
|
|
84
|
+
*/
|
|
85
|
+
runner: IOperationRunner | undefined;
|
|
86
|
+
/**
|
|
87
|
+
* This number represents how far away this Operation is from the furthest "root" operation (i.e.
|
|
88
|
+
* an operation with no consumers). This helps us to calculate the critical path (i.e. the
|
|
89
|
+
* longest chain of projects which must be executed in order, thereby limiting execution speed
|
|
90
|
+
* of the entire operation tree.
|
|
91
|
+
*
|
|
92
|
+
* This number is calculated via a memoized depth-first search, and when choosing the next
|
|
93
|
+
* operation to execute, the operation with the highest criticalPathLength is chosen.
|
|
94
|
+
*
|
|
95
|
+
* Example:
|
|
96
|
+
* (0) A
|
|
97
|
+
* \\
|
|
98
|
+
* (1) B C (0) (applications)
|
|
99
|
+
* \\ /|\\
|
|
100
|
+
* \\ / | \\
|
|
101
|
+
* (2) D | X (1) (utilities)
|
|
102
|
+
* | / \\
|
|
103
|
+
* |/ \\
|
|
104
|
+
* (2) Y Z (2) (other utilities)
|
|
105
|
+
*
|
|
106
|
+
* All roots (A & C) have a criticalPathLength of 0.
|
|
107
|
+
* B has a score of 1, since A depends on it.
|
|
108
|
+
* D has a score of 2, since we look at the longest chain (e.g D-\>B-\>A is longer than D-\>C)
|
|
109
|
+
* X has a score of 1, since the only package which depends on it is A
|
|
110
|
+
* Z has a score of 2, since only X depends on it, and X has a score of 1
|
|
111
|
+
* Y has a score of 2, since the chain Y-\>X-\>C is longer than Y-\>C
|
|
112
|
+
*
|
|
113
|
+
* The algorithm is implemented in AsyncOperationQueue.ts as calculateCriticalPathLength()
|
|
114
|
+
*/
|
|
115
|
+
criticalPathLength: number | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* The weight for this operation. This scalar is the contribution of this operation to the
|
|
118
|
+
* `criticalPathLength` calculation above. Modify to indicate the following:
|
|
119
|
+
* - `weight` === 1: indicates that this operation has an average duration
|
|
120
|
+
* - `weight` > 1: indicates that this operation takes longer than average and so the scheduler
|
|
121
|
+
* should try to favor starting it over other, shorter operations. An example might be an operation that
|
|
122
|
+
* bundles an entire application and runs whole-program optimization.
|
|
123
|
+
* - `weight` < 1: indicates that this operation takes less time than average and so the scheduler
|
|
124
|
+
* should favor other, longer operations over it. An example might be an operation to unpack a cached
|
|
125
|
+
* output, or an operation using NullOperationRunner, which might use a value of 0.
|
|
126
|
+
*/
|
|
127
|
+
weight: number;
|
|
128
|
+
/**
|
|
129
|
+
* The state of this operation the previous time a manager was invoked.
|
|
130
|
+
*/
|
|
131
|
+
lastState: IOperationState | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* The current state of this operation
|
|
134
|
+
*/
|
|
135
|
+
state: IOperationState | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* A cached execution promise for the current OperationExecutionManager invocation of this operation.
|
|
138
|
+
*/
|
|
139
|
+
private _promise;
|
|
140
|
+
/**
|
|
141
|
+
* If true, then a run of this operation is currently wanted.
|
|
142
|
+
* This is used to track state from the `requestRun` callback passed to the runner.
|
|
143
|
+
*/
|
|
144
|
+
private _runPending;
|
|
145
|
+
constructor(options?: IOperationOptions);
|
|
146
|
+
addDependency(dependency: Operation): void;
|
|
147
|
+
deleteDependency(dependency: Operation): void;
|
|
148
|
+
reset(): void;
|
|
149
|
+
/**
|
|
150
|
+
* @internal
|
|
151
|
+
*/
|
|
152
|
+
_executeAsync(context: IExecuteOperationContext): Promise<OperationStatus>;
|
|
153
|
+
private _executeInnerAsync;
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=Operation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Operation.d.ts","sourceRoot":"","sources":["../src/Operation.ts"],"names":[],"mappings":"AAGA,OAAO,EAAiB,S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E;;OAEG;IACH,SAAgB,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9C;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAEzC;;;OAGG;IACI,MAAM,EAAE,gBAAgB,GAAG,SAAS,CAAa;IAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACI,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAa;IAE1D;;;;;;;;;;OAUG;IACI,MAAM,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACI,SAAS,EAAE,eAAe,GAAG,SAAS,CAAa;IAE1D;;OAEG;IACI,KAAK,EAAE,eAAe,GAAG,SAAS,CAAa;IAEtD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAmD;IAEnE;;;OAGG;IACH,OAAO,CAAC,WAAW,CAAiB;gBAEjB,OAAO,CAAC,EAAE,iBAAiB;IAOvC,aAAa,CAAC,UAAU,EAAE,SAAS,GAAG,IAAI;IAK1C,gBAAgB,CAAC,UAAU,EAAE,SAAS,GAAG,IAAI;IAK7C,KAAK,IAAI,IAAI;IAepB;;OAEG;IACU,aAAa,CAAC,OAAO,EAAE,wBAAwB,GAAG,OAAO,CAAC,eAAe,CAAC;YAazE,kBAAkB;CAoHjC"}
|
package/lib/Operation.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
|
+
// See LICENSE in the project root for license information.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.Operation = void 0;
|
|
6
|
+
const node_core_library_1 = require("@rushstack/node-core-library");
|
|
7
|
+
const Stopwatch_1 = require("./Stopwatch");
|
|
8
|
+
const OperationStatus_1 = require("./OperationStatus");
|
|
9
|
+
/**
|
|
10
|
+
* The `Operation` class is a node in the dependency graph of work that needs to be scheduled by the
|
|
11
|
+
* `OperationExecutionManager`. Each `Operation` has a `runner` member of type `IOperationRunner`, whose
|
|
12
|
+
* implementation manages the actual process of running a single operation.
|
|
13
|
+
*
|
|
14
|
+
* The graph of `Operation` instances will be cloned into a separate execution graph after processing.
|
|
15
|
+
*
|
|
16
|
+
* @beta
|
|
17
|
+
*/
|
|
18
|
+
class Operation {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
/**
|
|
21
|
+
* A set of all dependencies which must be executed before this operation is complete.
|
|
22
|
+
*/
|
|
23
|
+
this.dependencies = new Set();
|
|
24
|
+
/**
|
|
25
|
+
* A set of all operations that wait for this operation.
|
|
26
|
+
*/
|
|
27
|
+
this.consumers = new Set();
|
|
28
|
+
/**
|
|
29
|
+
* When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of
|
|
30
|
+
* running the operation.
|
|
31
|
+
*/
|
|
32
|
+
this.runner = undefined;
|
|
33
|
+
/**
|
|
34
|
+
* This number represents how far away this Operation is from the furthest "root" operation (i.e.
|
|
35
|
+
* an operation with no consumers). This helps us to calculate the critical path (i.e. the
|
|
36
|
+
* longest chain of projects which must be executed in order, thereby limiting execution speed
|
|
37
|
+
* of the entire operation tree.
|
|
38
|
+
*
|
|
39
|
+
* This number is calculated via a memoized depth-first search, and when choosing the next
|
|
40
|
+
* operation to execute, the operation with the highest criticalPathLength is chosen.
|
|
41
|
+
*
|
|
42
|
+
* Example:
|
|
43
|
+
* (0) A
|
|
44
|
+
* \\
|
|
45
|
+
* (1) B C (0) (applications)
|
|
46
|
+
* \\ /|\\
|
|
47
|
+
* \\ / | \\
|
|
48
|
+
* (2) D | X (1) (utilities)
|
|
49
|
+
* | / \\
|
|
50
|
+
* |/ \\
|
|
51
|
+
* (2) Y Z (2) (other utilities)
|
|
52
|
+
*
|
|
53
|
+
* All roots (A & C) have a criticalPathLength of 0.
|
|
54
|
+
* B has a score of 1, since A depends on it.
|
|
55
|
+
* D has a score of 2, since we look at the longest chain (e.g D-\>B-\>A is longer than D-\>C)
|
|
56
|
+
* X has a score of 1, since the only package which depends on it is A
|
|
57
|
+
* Z has a score of 2, since only X depends on it, and X has a score of 1
|
|
58
|
+
* Y has a score of 2, since the chain Y-\>X-\>C is longer than Y-\>C
|
|
59
|
+
*
|
|
60
|
+
* The algorithm is implemented in AsyncOperationQueue.ts as calculateCriticalPathLength()
|
|
61
|
+
*/
|
|
62
|
+
this.criticalPathLength = undefined;
|
|
63
|
+
/**
|
|
64
|
+
* The state of this operation the previous time a manager was invoked.
|
|
65
|
+
*/
|
|
66
|
+
this.lastState = undefined;
|
|
67
|
+
/**
|
|
68
|
+
* The current state of this operation
|
|
69
|
+
*/
|
|
70
|
+
this.state = undefined;
|
|
71
|
+
/**
|
|
72
|
+
* A cached execution promise for the current OperationExecutionManager invocation of this operation.
|
|
73
|
+
*/
|
|
74
|
+
this._promise = undefined;
|
|
75
|
+
/**
|
|
76
|
+
* If true, then a run of this operation is currently wanted.
|
|
77
|
+
* This is used to track state from the `requestRun` callback passed to the runner.
|
|
78
|
+
*/
|
|
79
|
+
this._runPending = true;
|
|
80
|
+
this.groupName = options?.groupName;
|
|
81
|
+
this.runner = options?.runner;
|
|
82
|
+
this.weight = options?.weight || 1;
|
|
83
|
+
this.name = options?.name;
|
|
84
|
+
}
|
|
85
|
+
addDependency(dependency) {
|
|
86
|
+
this.dependencies.add(dependency);
|
|
87
|
+
dependency.consumers.add(this);
|
|
88
|
+
}
|
|
89
|
+
deleteDependency(dependency) {
|
|
90
|
+
this.dependencies.delete(dependency);
|
|
91
|
+
dependency.consumers.delete(this);
|
|
92
|
+
}
|
|
93
|
+
reset() {
|
|
94
|
+
// Reset operation state
|
|
95
|
+
this.lastState = this.state;
|
|
96
|
+
this.state = {
|
|
97
|
+
status: this.dependencies.size > 0 ? OperationStatus_1.OperationStatus.Waiting : OperationStatus_1.OperationStatus.Ready,
|
|
98
|
+
hasBeenRun: this.lastState?.hasBeenRun ?? false,
|
|
99
|
+
error: undefined,
|
|
100
|
+
stopwatch: new Stopwatch_1.Stopwatch()
|
|
101
|
+
};
|
|
102
|
+
this._promise = undefined;
|
|
103
|
+
this._runPending = true;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @internal
|
|
107
|
+
*/
|
|
108
|
+
async _executeAsync(context) {
|
|
109
|
+
const { state } = this;
|
|
110
|
+
if (!state) {
|
|
111
|
+
throw new Error(`Operation state has not been initialized.`);
|
|
112
|
+
}
|
|
113
|
+
if (!this._promise) {
|
|
114
|
+
this._promise = this._executeInnerAsync(context, state);
|
|
115
|
+
}
|
|
116
|
+
return this._promise;
|
|
117
|
+
}
|
|
118
|
+
async _executeInnerAsync(context, rawState) {
|
|
119
|
+
const state = rawState;
|
|
120
|
+
const { runner } = this;
|
|
121
|
+
const dependencyResults = await Promise.allSettled(Array.from(this.dependencies, (dependency) => dependency._executeAsync(context)));
|
|
122
|
+
const { abortSignal, requestRun, queueWork } = context;
|
|
123
|
+
if (abortSignal.aborted) {
|
|
124
|
+
state.status = OperationStatus_1.OperationStatus.Aborted;
|
|
125
|
+
return state.status;
|
|
126
|
+
}
|
|
127
|
+
for (const result of dependencyResults) {
|
|
128
|
+
if (result.status === 'rejected' ||
|
|
129
|
+
result.value === OperationStatus_1.OperationStatus.Blocked ||
|
|
130
|
+
result.value === OperationStatus_1.OperationStatus.Failure) {
|
|
131
|
+
state.status = OperationStatus_1.OperationStatus.Blocked;
|
|
132
|
+
return state.status;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
state.status = OperationStatus_1.OperationStatus.Ready;
|
|
136
|
+
const innerContext = {
|
|
137
|
+
abortSignal,
|
|
138
|
+
isFirstRun: !state.hasBeenRun,
|
|
139
|
+
requestRun: requestRun
|
|
140
|
+
? () => {
|
|
141
|
+
switch (this.state?.status) {
|
|
142
|
+
case OperationStatus_1.OperationStatus.Ready:
|
|
143
|
+
case OperationStatus_1.OperationStatus.Executing:
|
|
144
|
+
// If current status has not yet resolved to a fixed value,
|
|
145
|
+
// re-executing this operation does not require a full rerun
|
|
146
|
+
// of the operation graph. Simply mark that a run is requested.
|
|
147
|
+
// This variable is on the Operation instead of the
|
|
148
|
+
// containing closure to deal with scenarios in which
|
|
149
|
+
// the runner hangs on to an old copy of the callback.
|
|
150
|
+
this._runPending = true;
|
|
151
|
+
return;
|
|
152
|
+
case OperationStatus_1.OperationStatus.Blocked:
|
|
153
|
+
case OperationStatus_1.OperationStatus.Aborted:
|
|
154
|
+
case OperationStatus_1.OperationStatus.Failure:
|
|
155
|
+
case OperationStatus_1.OperationStatus.NoOp:
|
|
156
|
+
case OperationStatus_1.OperationStatus.Success:
|
|
157
|
+
// The requestRun callback is assumed to remain constant
|
|
158
|
+
// throughout the lifetime of the process, so it is safe
|
|
159
|
+
// to capture here.
|
|
160
|
+
return requestRun(this.name);
|
|
161
|
+
default:
|
|
162
|
+
throw new node_core_library_1.InternalError(`Unexpected status: ${this.state?.status}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
: undefined
|
|
166
|
+
};
|
|
167
|
+
await queueWork(async () => {
|
|
168
|
+
// Redundant variable to satisfy require-atomic-updates
|
|
169
|
+
const innerState = state;
|
|
170
|
+
if (abortSignal.aborted) {
|
|
171
|
+
innerState.status = OperationStatus_1.OperationStatus.Aborted;
|
|
172
|
+
return innerState.status;
|
|
173
|
+
}
|
|
174
|
+
context.beforeExecute(this, innerState);
|
|
175
|
+
innerState.stopwatch.start();
|
|
176
|
+
innerState.status = OperationStatus_1.OperationStatus.Executing;
|
|
177
|
+
// Mark that the operation has been started at least once.
|
|
178
|
+
innerState.hasBeenRun = true;
|
|
179
|
+
while (this._runPending) {
|
|
180
|
+
this._runPending = false;
|
|
181
|
+
try {
|
|
182
|
+
// We don't support aborting in the middle of a runner's execution.
|
|
183
|
+
innerState.status = runner ? await runner.executeAsync(innerContext) : OperationStatus_1.OperationStatus.NoOp;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
innerState.status = OperationStatus_1.OperationStatus.Failure;
|
|
187
|
+
innerState.error = error;
|
|
188
|
+
}
|
|
189
|
+
// Since runner.executeAsync is async, a change could have occurred that requires re-execution
|
|
190
|
+
// This operation is still active, so can re-execute immediately, rather than forcing a whole
|
|
191
|
+
// new execution pass.
|
|
192
|
+
// As currently written, this does mean that if a job is scheduled with higher priority while
|
|
193
|
+
// this operation is still executing, it will still wait for this retry. This may not be desired
|
|
194
|
+
// and if it becomes a problem, the retry loop will need to be moved outside of the `queueWork` call.
|
|
195
|
+
// This introduces complexity regarding tracking of timing and start/end logging, however.
|
|
196
|
+
if (this._runPending) {
|
|
197
|
+
if (abortSignal.aborted) {
|
|
198
|
+
innerState.status = OperationStatus_1.OperationStatus.Aborted;
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
context.terminal.writeLine(`Immediate rerun requested. Executing.`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
state.stopwatch.stop();
|
|
207
|
+
context.afterExecute(this, state);
|
|
208
|
+
}, /* priority */ this.criticalPathLength ?? 0);
|
|
209
|
+
return state.status;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
exports.Operation = Operation;
|
|
213
|
+
//# sourceMappingURL=Operation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Operation.js","sourceRoot":"","sources":["../src/Operation.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,oEAAwE;AAExE,2CAAwC;AAQxC,uDAAoD;AA8DpD;;;;;;;;GAQG;AACH,MAAa,SAAS;IAyFpB,YAAmB,OAA2B;QAxF9C;;WAEG;QACa,iBAAY,GAAmB,IAAI,GAAG,EAAa,CAAC;QACpE;;WAEG;QACa,cAAS,GAAmB,IAAI,GAAG,EAAa,CAAC;QAUjE;;;WAGG;QACI,WAAM,GAAiC,SAAS,CAAC;QAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA4BG;QACI,uBAAkB,GAAuB,SAAS,CAAC;QAe1D;;W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,GAAG,iCAAe,CAAC,SAAS,CAAC;YAC9C,0DAA0D;YAC1D,UAAU,CAAC,UAAU,GAAG,IAAI,CAAC;YAE7B,OAAO,IAAI,CAAC,WAAW,EAAE;gBACvB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;gBACzB,IAAI;oBACF,mEAAmE;oBACnE,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,iCAAe,CAAC,IAAI,CAAC;iBAC7F;gBAAC,OAAO,KAAK,EAAE;oBACd,UAAU,CAAC,MAAM,GAAG,iCAAe,CAAC,OAAO,CAAC;oBAC5C,UAAU,CAAC,KAAK,GAAG,KAAuB,CAAC;iBAC5C;gBAED,8FAA8F;gBAC9F,6FAA6F;gBAC7F,sBAAsB;gBAEtB,6FAA6F;gBAC7F,gGAAgG;gBAChG,qGAAqG;gBACrG,0FAA0F;gBAE1F,IAAI,IAAI,CAAC,WAAW,EAAE;oBACpB,IAAI,WAAW,CAAC,OAAO,EAAE;wBACvB,UAAU,CAAC,MAAM,GAAG,iCAAe,CAAC,OAAO,CAAC;wBAC5C,MAAM;qBACP;yBAAM;wBACL,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,uCAAuC,CAAC,CAAC;qBACrE;iBACF;aACF;YAED,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;YACvB,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACpC,CAAC,EAAE,cAAc,CAAC,IAAI,CAAC,kBAAkB,IAAI,CAAC,CAAC,CAAC;QAEhD,OAAO,KAAK,CAAC,MAAM,CAAC;IACtB,CAAC;CACF;AA7PD,8BA6PC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { InternalError, ITerminal } from '@rushstack/node-core-library';\n\nimport { Stopwatch } from './Stopwatch';\nimport type {\n IOperationRunner,\n IOperationRunnerContext,\n IOperationState,\n IOperationStates\n} from './IOperationRunner';\nimport type { OperationError } from './OperationError';\nimport { OperationStatus } from './OperationStatus';\n\n/**\n * Options for constructing a new Operation.\n * @beta\n */\nexport interface IOperationOptions {\n /**\n * The name of this operation, for logging.\n */\n name?: string | undefined;\n\n /**\n * The group that this operation belongs to. Will be used for logging and duration tracking.\n */\n groupName?: string | undefined;\n\n /**\n * When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of\n * running the operation.\n */\n runner?: IOperationRunner | undefined;\n\n /**\n * The weight used by the scheduler to determine order of execution.\n */\n weight?: number | undefined;\n}\n\n/**\n * Information provided to `executeAsync` by the `OperationExecutionManager`.\n *\n * @beta\n */\nexport interface IExecuteOperationContext extends Omit<IOperationRunnerContext, 'isFirstRun' | 'requestRun'> {\n /**\n * Function to invoke before execution of an operation, for logging.\n */\n beforeExecute(operation: Operation, state: IOperationState): void;\n\n /**\n * Function to invoke after execution of an operation, for logging.\n */\n afterExecute(operation: Operation, state: IOperationState): void;\n\n /**\n * Function used to schedule the concurrency-limited execution of an operation.\n */\n queueWork<T>(workFn: () => Promise<T>, priority: number): Promise<T>;\n\n /**\n * A callback to the overarching orchestrator to request that the operation be invoked again.\n * Used in watch mode to signal that inputs have changed.\n */\n requestRun?: (requestor?: string) => void;\n\n /**\n * Terminal to write output to.\n */\n terminal: ITerminal;\n}\n\n/**\n * The `Operation` class is a node in the dependency graph of work that needs to be scheduled by the\n * `OperationExecutionManager`. Each `Operation` has a `runner` member of type `IOperationRunner`, whose\n * implementation manages the actual process of running a single operation.\n *\n * The graph of `Operation` instances will be cloned into a separate execution graph after processing.\n *\n * @beta\n */\nexport class Operation implements IOperationStates {\n /**\n * A set of all dependencies which must be executed before this operation is complete.\n */\n public readonly dependencies: Set<Operation> = new Set<Operation>();\n /**\n * A set of all operations that wait for this operation.\n */\n public readonly consumers: Set<Operation> = new Set<Operation>();\n /**\n * If specified, the name of a grouping to which this Operation belongs, for logging start and end times.\n */\n public readonly groupName: string | undefined;\n /**\n * The name of this operation, for logging.\n */\n public readonly name: string | undefined;\n\n /**\n * When the scheduler is ready to process this `Operation`, the `runner` implements the actual work of\n * running the operation.\n */\n public runner: IOperationRunner | undefined = undefined;\n\n /**\n * This number represents how far away this Operation is from the furthest \"root\" operation (i.e.\n * an operation with no consumers). This helps us to calculate the critical path (i.e. the\n * longest chain of projects which must be executed in order, thereby limiting execution speed\n * of the entire operation tree.\n *\n * This number is calculated via a memoized depth-first search, and when choosing the next\n * operation to execute, the operation with the highest criticalPathLength is chosen.\n *\n * Example:\n * (0) A\n * \\\\\n * (1) B C (0) (applications)\n * \\\\ /|\\\\\n * \\\\ / | \\\\\n * (2) D | X (1) (utilities)\n * | / \\\\\n * |/ \\\\\n * (2) Y Z (2) (other utilities)\n *\n * All roots (A & C) have a criticalPathLength of 0.\n * B has a score of 1, since A depends on it.\n * D has a score of 2, since we look at the longest chain (e.g D-\\>B-\\>A is longer than D-\\>C)\n * X has a score of 1, since the only package which depends on it is A\n * Z has a score of 2, since only X depends on it, and X has a score of 1\n * Y has a score of 2, since the chain Y-\\>X-\\>C is longer than Y-\\>C\n *\n * The algorithm is implemented in AsyncOperationQueue.ts as calculateCriticalPathLength()\n */\n public criticalPathLength: number | undefined = undefined;\n\n /**\n * The weight for this operation. This scalar is the contribution of this operation to the\n * `criticalPathLength` calculation above. Modify to indicate the following:\n * - `weight` === 1: indicates that this operation has an average duration\n * - `weight` > 1: indicates that this operation takes longer than average and so the scheduler\n * should try to favor starting it over other, shorter operations. An example might be an operation that\n * bundles an entire application and runs whole-program optimization.\n * - `weight` < 1: indicates that this operation takes less time than average and so the scheduler\n * should favor other, longer operations over it. An example might be an operation to unpack a cached\n * output, or an operation using NullOperationRunner, which might use a value of 0.\n */\n public weight: number;\n\n /**\n * The state of this operation the previous time a manager was invoked.\n */\n public lastState: IOperationState | undefined = undefined;\n\n /**\n * The current state of this operation\n */\n public state: IOperationState | undefined = undefined;\n\n /**\n * A cached execution promise for the current OperationExecutionManager invocation of this operation.\n */\n private _promise: Promise<OperationStatus> | undefined = undefined;\n\n /**\n * If true, then a run of this operation is currently wanted.\n * This is used to track state from the `requestRun` callback passed to the runner.\n */\n private _runPending: boolean = true;\n\n public constructor(options?: IOperationOptions) {\n this.groupName = options?.groupName;\n this.runner = options?.runner;\n this.weight = options?.weight || 1;\n this.name = options?.name;\n }\n\n public addDependency(dependency: Operation): void {\n this.dependencies.add(dependency);\n dependency.consumers.add(this);\n }\n\n public deleteDependency(dependency: Operation): void {\n this.dependencies.delete(dependency);\n dependency.consumers.delete(this);\n }\n\n public reset(): void {\n // Reset operation state\n this.lastState = this.state;\n\n this.state = {\n status: this.dependencies.size > 0 ? OperationStatus.Waiting : OperationStatus.Ready,\n hasBeenRun: this.lastState?.hasBeenRun ?? false,\n error: undefined,\n stopwatch: new Stopwatch()\n };\n\n this._promise = undefined;\n this._runPending = true;\n }\n\n /**\n * @internal\n */\n public async _executeAsync(context: IExecuteOperationContext): Promise<OperationStatus> {\n const { state } = this;\n if (!state) {\n throw new Error(`Operation state has not been initialized.`);\n }\n\n if (!this._promise) {\n this._promise = this._executeInnerAsync(context, state);\n }\n\n return this._promise;\n }\n\n private async _executeInnerAsync(\n context: IExecuteOperationContext,\n rawState: IOperationState\n ): Promise<OperationStatus> {\n const state: IOperationState = rawState;\n const { runner } = this;\n\n const dependencyResults: PromiseSettledResult<OperationStatus>[] = await Promise.allSettled(\n Array.from(this.dependencies, (dependency: Operation) => dependency._executeAsync(context))\n );\n\n const { abortSignal, requestRun, queueWork } = context;\n\n if (abortSignal.aborted) {\n state.status = OperationStatus.Aborted;\n return state.status;\n }\n\n for (const result of dependencyResults) {\n if (\n result.status === 'rejected' ||\n result.value === OperationStatus.Blocked ||\n result.value === OperationStatus.Failure\n ) {\n state.status = OperationStatus.Blocked;\n return state.status;\n }\n }\n\n state.status = OperationStatus.Ready;\n\n const innerContext: IOperationRunnerContext = {\n abortSignal,\n isFirstRun: !state.hasBeenRun,\n requestRun: requestRun\n ? () => {\n switch (this.state?.status) {\n case OperationStatus.Ready:\n case OperationStatus.Executing:\n // If current status has not yet resolved to a fixed value,\n // re-executing this operation does not require a full rerun\n // of the operation graph. Simply mark that a run is requested.\n\n // This variable is on the Operation instead of the\n // containing closure to deal with scenarios in which\n // the runner hangs on to an old copy of the callback.\n this._runPending = true;\n return;\n\n case OperationStatus.Blocked:\n case OperationStatus.Aborted:\n case OperationStatus.Failure:\n case OperationStatus.NoOp:\n case OperationStatus.Success:\n // The requestRun callback is assumed to remain constant\n // throughout the lifetime of the process, so it is safe\n // to capture here.\n return requestRun(this.name);\n default:\n throw new InternalError(`Unexpected status: ${this.state?.status}`);\n }\n }\n : undefined\n };\n\n await queueWork(async () => {\n // Redundant variable to satisfy require-atomic-updates\n const innerState: IOperationState = state;\n\n if (abortSignal.aborted) {\n innerState.status = OperationStatus.Aborted;\n return innerState.status;\n }\n\n context.beforeExecute(this, innerState);\n\n innerState.stopwatch.start();\n innerState.status = OperationStatus.Executing;\n // Mark that the operation has been started at least once.\n innerState.hasBeenRun = true;\n\n while (this._runPending) {\n this._runPending = false;\n try {\n // We don't support aborting in the middle of a runner's execution.\n innerState.status = runner ? await runner.executeAsync(innerContext) : OperationStatus.NoOp;\n } catch (error) {\n innerState.status = OperationStatus.Failure;\n innerState.error = error as OperationError;\n }\n\n // Since runner.executeAsync is async, a change could have occurred that requires re-execution\n // This operation is still active, so can re-execute immediately, rather than forcing a whole\n // new execution pass.\n\n // As currently written, this does mean that if a job is scheduled with higher priority while\n // this operation is still executing, it will still wait for this retry. This may not be desired\n // and if it becomes a problem, the retry loop will need to be moved outside of the `queueWork` call.\n // This introduces complexity regarding tracking of timing and start/end logging, however.\n\n if (this._runPending) {\n if (abortSignal.aborted) {\n innerState.status = OperationStatus.Aborted;\n break;\n } else {\n context.terminal.writeLine(`Immediate rerun requested. Executing.`);\n }\n }\n }\n\n state.stopwatch.stop();\n context.afterExecute(this, state);\n }, /* priority */ this.criticalPathLength ?? 0);\n\n return state.status;\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encapsulates information about an error
|
|
3
|
+
*
|
|
4
|
+
* @beta
|
|
5
|
+
*/
|
|
6
|
+
export declare class OperationError extends Error {
|
|
7
|
+
protected _type: string;
|
|
8
|
+
constructor(type: string, message: string);
|
|
9
|
+
get message(): string;
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=OperationError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OperationError.d.ts","sourceRoot":"","sources":["../src/OperationError.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC;gBAEL,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAYhD,IAAW,OAAO,IAAI,MAAM,CAE3B;IAEM,QAAQ,IAAI,MAAM;CAG1B"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
|
+
// See LICENSE in the project root for license information.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.OperationError = void 0;
|
|
6
|
+
/**
|
|
7
|
+
* Encapsulates information about an error
|
|
8
|
+
*
|
|
9
|
+
* @beta
|
|
10
|
+
*/
|
|
11
|
+
class OperationError extends Error {
|
|
12
|
+
constructor(type, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
// Manually set the prototype, as we can no longer extend built-in classes like Error, Array, Map, etc.
|
|
15
|
+
// https://github.com/microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
|
|
16
|
+
//
|
|
17
|
+
// Note: the prototype must also be set on any classes which extend this one
|
|
18
|
+
Object.setPrototypeOf(this, OperationError.prototype);
|
|
19
|
+
this._type = type;
|
|
20
|
+
}
|
|
21
|
+
get message() {
|
|
22
|
+
return `[${this._type}] '${super.message}'`;
|
|
23
|
+
}
|
|
24
|
+
toString() {
|
|
25
|
+
return this.message;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.OperationError = OperationError;
|
|
29
|
+
//# sourceMappingURL=OperationError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OperationError.js","sourceRoot":"","sources":["../src/OperationError.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D;;;;GAIG;AACH,MAAa,cAAe,SAAQ,KAAK;IAGvC,YAAmB,IAAY,EAAE,OAAe;QAC9C,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,uGAAuG;QACvG,+IAA+I;QAC/I,EAAE;QACF,4EAA4E;QAC5E,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,cAAc,CAAC,SAAS,CAAC,CAAC;QAEtD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,IAAW,OAAO;QAChB,OAAO,IAAI,IAAI,CAAC,KAAK,MAAM,KAAK,CAAC,OAAO,GAAG,CAAC;IAC9C,CAAC;IAEM,QAAQ;QACb,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF;AAtBD,wCAsBC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * Encapsulates information about an error\n *\n * @beta\n */\nexport class OperationError extends Error {\n protected _type: string;\n\n public constructor(type: string, message: string) {\n super(message);\n\n // Manually set the prototype, as we can no longer extend built-in classes like Error, Array, Map, etc.\n // https://github.com/microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work\n //\n // Note: the prototype must also be set on any classes which extend this one\n Object.setPrototypeOf(this, OperationError.prototype);\n\n this._type = type;\n }\n\n public get message(): string {\n return `[${this._type}] '${super.message}'`;\n }\n\n public toString(): string {\n return this.message;\n }\n}\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { ITerminal } from '@rushstack/node-core-library';
|
|
3
|
+
import type { Operation } from './Operation';
|
|
4
|
+
import { OperationStatus } from './OperationStatus';
|
|
5
|
+
/**
|
|
6
|
+
* Options for the current run.
|
|
7
|
+
*
|
|
8
|
+
* @beta
|
|
9
|
+
*/
|
|
10
|
+
export interface IOperationExecutionOptions {
|
|
11
|
+
abortSignal: AbortSignal;
|
|
12
|
+
parallelism: number;
|
|
13
|
+
terminal: ITerminal;
|
|
14
|
+
requestRun?: (requestor?: string) => void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A class which manages the execution of a set of tasks with interdependencies.
|
|
18
|
+
* Initially, and at the end of each task execution, all unblocked tasks
|
|
19
|
+
* are added to a ready queue which is then executed. This is done continually until all
|
|
20
|
+
* tasks are complete, or prematurely fails if any of the tasks fail.
|
|
21
|
+
*
|
|
22
|
+
* @beta
|
|
23
|
+
*/
|
|
24
|
+
export declare class OperationExecutionManager {
|
|
25
|
+
/**
|
|
26
|
+
* The set of operations that will be executed
|
|
27
|
+
*/
|
|
28
|
+
private readonly _operations;
|
|
29
|
+
/**
|
|
30
|
+
* Group records are metadata-only entities used for tracking the start and end of a set of related tasks.
|
|
31
|
+
* This is the only extent to which the operation graph is aware of Heft phases.
|
|
32
|
+
*/
|
|
33
|
+
private readonly _groupRecordByName;
|
|
34
|
+
/**
|
|
35
|
+
* The total number of non-silent operations in the graph.
|
|
36
|
+
* Silent operations are generally used to simplify the construction of the graph.
|
|
37
|
+
*/
|
|
38
|
+
private readonly _trackedOperationCount;
|
|
39
|
+
constructor(operations: ReadonlySet<Operation>);
|
|
40
|
+
/**
|
|
41
|
+
* Executes all operations which have been registered, returning a promise which is resolved when all the
|
|
42
|
+
* operations are completed successfully, or rejects when any operation fails.
|
|
43
|
+
*/
|
|
44
|
+
executeAsync(executionOptions: IOperationExecutionOptions): Promise<OperationStatus>;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=OperationExecutionManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OperationExecutionManager.d.ts","sourceRoot":"","sources":["../src/OperationExecutionManager.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AAG9D,OAAO,KAAK,EAA4B,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGpD;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,WAAW,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,SAAS,CAAC;IAEpB,UAAU,CAAC,EAAE,CAAC,SAAS,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;CAC3C;AAED;;;;;;;GAOG;AACH,qBAAa,yBAAyB;IACpC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAoC;IACvE;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAS;gBAE7B,UAAU,EAAE,WAAW,CAAC,SAAS,CAAC;IAqCrD;;;OAGG;IACU,YAAY,CAAC,gBAAgB,EAAE,0BAA0B,GAAG,OAAO,CAAC,eAAe,CAAC;CA6FlG"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
3
|
+
// See LICENSE in the project root for license information.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.OperationExecutionManager = void 0;
|
|
6
|
+
const OperationGroupRecord_1 = require("./OperationGroupRecord");
|
|
7
|
+
const OperationStatus_1 = require("./OperationStatus");
|
|
8
|
+
const calculateCriticalPath_1 = require("./calculateCriticalPath");
|
|
9
|
+
/**
|
|
10
|
+
* A class which manages the execution of a set of tasks with interdependencies.
|
|
11
|
+
* Initially, and at the end of each task execution, all unblocked tasks
|
|
12
|
+
* are added to a ready queue which is then executed. This is done continually until all
|
|
13
|
+
* tasks are complete, or prematurely fails if any of the tasks fail.
|
|
14
|
+
*
|
|
15
|
+
* @beta
|
|
16
|
+
*/
|
|
17
|
+
class OperationExecutionManager {
|
|
18
|
+
constructor(operations) {
|
|
19
|
+
const groupRecordByName = new Map();
|
|
20
|
+
this._groupRecordByName = groupRecordByName;
|
|
21
|
+
let trackedOperationCount = 0;
|
|
22
|
+
for (const operation of operations) {
|
|
23
|
+
const { groupName } = operation;
|
|
24
|
+
let group = undefined;
|
|
25
|
+
if (groupName && !(group = groupRecordByName.get(groupName))) {
|
|
26
|
+
group = new OperationGroupRecord_1.OperationGroupRecord(groupName);
|
|
27
|
+
groupRecordByName.set(groupName, group);
|
|
28
|
+
}
|
|
29
|
+
group?.addOperation(operation);
|
|
30
|
+
if (!operation.runner?.silent) {
|
|
31
|
+
// Only count non-silent operations
|
|
32
|
+
trackedOperationCount++;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
this._trackedOperationCount = trackedOperationCount;
|
|
36
|
+
this._operations = (0, calculateCriticalPath_1.calculateCriticalPathLengths)(operations);
|
|
37
|
+
for (const consumer of operations) {
|
|
38
|
+
for (const dependency of consumer.dependencies) {
|
|
39
|
+
if (!operations.has(dependency)) {
|
|
40
|
+
throw new Error(`Operation ${JSON.stringify(consumer.name)} declares a dependency on operation ` +
|
|
41
|
+
`${JSON.stringify(dependency.name)} that is not in the set of operations to execute.`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Executes all operations which have been registered, returning a promise which is resolved when all the
|
|
48
|
+
* operations are completed successfully, or rejects when any operation fails.
|
|
49
|
+
*/
|
|
50
|
+
async executeAsync(executionOptions) {
|
|
51
|
+
let hasReportedFailures = false;
|
|
52
|
+
const { abortSignal, parallelism, terminal, requestRun } = executionOptions;
|
|
53
|
+
const startedGroups = new Set();
|
|
54
|
+
const finishedGroups = new Set();
|
|
55
|
+
const maxParallelism = Math.min(this._operations.length, parallelism);
|
|
56
|
+
const groupRecords = this._groupRecordByName;
|
|
57
|
+
for (const groupRecord of groupRecords.values()) {
|
|
58
|
+
groupRecord.reset();
|
|
59
|
+
}
|
|
60
|
+
for (const operation of this._operations) {
|
|
61
|
+
operation.reset();
|
|
62
|
+
}
|
|
63
|
+
terminal.writeVerboseLine(`Executing a maximum of ${maxParallelism} simultaneous tasks...`);
|
|
64
|
+
const executionContext = {
|
|
65
|
+
terminal,
|
|
66
|
+
abortSignal,
|
|
67
|
+
requestRun,
|
|
68
|
+
queueWork: (workFn, priority) => {
|
|
69
|
+
// TODO: Update to throttle parallelism
|
|
70
|
+
// Can just be a standard priority queue from async
|
|
71
|
+
return workFn();
|
|
72
|
+
},
|
|
73
|
+
beforeExecute: (operation) => {
|
|
74
|
+
// Initialize group if uninitialized and log the group name
|
|
75
|
+
const { groupName } = operation;
|
|
76
|
+
const groupRecord = groupName
|
|
77
|
+
? groupRecords.get(groupName)
|
|
78
|
+
: undefined;
|
|
79
|
+
if (groupRecord && !startedGroups.has(groupRecord)) {
|
|
80
|
+
startedGroups.add(groupRecord);
|
|
81
|
+
groupRecord.startTimer();
|
|
82
|
+
terminal.writeLine(` ---- ${groupRecord.name} started ---- `);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
afterExecute: (operation, state) => {
|
|
86
|
+
const { groupName } = operation;
|
|
87
|
+
const groupRecord = groupName
|
|
88
|
+
? groupRecords.get(groupName)
|
|
89
|
+
: undefined;
|
|
90
|
+
if (groupRecord) {
|
|
91
|
+
groupRecord.setOperationAsComplete(operation, state);
|
|
92
|
+
}
|
|
93
|
+
if (state.status === OperationStatus_1.OperationStatus.Failure) {
|
|
94
|
+
// This operation failed. Mark it as such and all reachable dependents as blocked.
|
|
95
|
+
// Failed operations get reported, even if silent.
|
|
96
|
+
// Generally speaking, silent operations shouldn't be able to fail, so this is a safety measure.
|
|
97
|
+
const message = state.error?.message;
|
|
98
|
+
if (message) {
|
|
99
|
+
terminal.writeErrorLine(message);
|
|
100
|
+
}
|
|
101
|
+
hasReportedFailures = true;
|
|
102
|
+
}
|
|
103
|
+
// Log out the group name and duration if it is the last operation in the group
|
|
104
|
+
if (groupRecord?.finished && !finishedGroups.has(groupRecord)) {
|
|
105
|
+
finishedGroups.add(groupRecord);
|
|
106
|
+
const finishedLoggingWord = groupRecord.hasFailures
|
|
107
|
+
? 'encountered an error'
|
|
108
|
+
: groupRecord.hasCancellations
|
|
109
|
+
? 'cancelled'
|
|
110
|
+
: 'finished';
|
|
111
|
+
terminal.writeLine(` ---- ${groupRecord.name} ${finishedLoggingWord} (${groupRecord.duration.toFixed(3)}s) ---- `);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
await Promise.all(this._operations.map((record) => record._executeAsync(executionContext)));
|
|
116
|
+
const finalStatus = this._trackedOperationCount === 0
|
|
117
|
+
? OperationStatus_1.OperationStatus.NoOp
|
|
118
|
+
: abortSignal.aborted
|
|
119
|
+
? OperationStatus_1.OperationStatus.Aborted
|
|
120
|
+
: hasReportedFailures
|
|
121
|
+
? OperationStatus_1.OperationStatus.Failure
|
|
122
|
+
: OperationStatus_1.OperationStatus.Success;
|
|
123
|
+
return finalStatus;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.OperationExecutionManager = OperationExecutionManager;
|
|
127
|
+
//# sourceMappingURL=OperationExecutionManager.js.map
|