Skip to content

Commit 05227d7

Browse files
authored
feat(core): add id, start and end time to lifecycle hooks (#32583)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior Pre and post hooks lack corellation Id which might make it difficult to culculate stats for task using pre and post hooks. Additionally, post hook does not expose the duration or time span for the task. Using pre/post hook might not be precise enough. ## Expected Behavior Hooks expose the unique taskId so we can corellate pre hook of a task to the post hook of the same task. Post hook should expose start and end time of the task run. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Resolves discussion [31076](#31076)
1 parent 65a216b commit 05227d7

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

packages/nx/src/command-line/release/publish.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ProjectGraph,
88
ProjectGraphProjectNode,
99
} from '../../config/project-graph';
10+
import { hashArray } from '../../native';
1011
import { createProjectFileMapUsingProjectGraph } from '../../project-graph/file-map-utils';
1112
import {
1213
runPostTasksExecution,
@@ -272,11 +273,14 @@ async function runPublishOnProjects(
272273
].join('\n')}\n`
273274
);
274275
}
276+
const id = hashArray([...process.argv, Date.now().toString()]);
275277
await runPreTasksExecution({
278+
id,
276279
workspaceRoot,
277280
nxJsonConfiguration: nxJson,
278281
argv: process.argv,
279282
});
283+
const startTime = Date.now();
280284

281285
/**
282286
* Run the relevant nx-release-publish executor on each of the selected projects.
@@ -299,6 +303,7 @@ async function runPublishOnProjects(
299303
{},
300304
extraOptions
301305
);
306+
const endTime = Date.now();
302307

303308
const publishProjectsResult: PublishProjectsResult = {};
304309
for (const taskData of Object.values(taskResults)) {
@@ -307,10 +312,13 @@ async function runPublishOnProjects(
307312
};
308313
}
309314
await runPostTasksExecution({
315+
id,
310316
taskResults,
311317
workspaceRoot,
312318
nxJsonConfiguration: nxJson,
313319
argv: process.argv,
320+
startTime,
321+
endTime,
314322
});
315323

316324
return publishProjectsResult;

packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ProjectGraph } from '../../config/project-graph';
2-
import { readNxJson, type PluginConfiguration } from '../../config/nx-json';
2+
import { type PluginConfiguration } from '../../config/nx-json';
33
import {
44
AggregateCreateNodesError,
55
isAggregateCreateNodesError,
@@ -15,7 +15,6 @@ import type {
1515
PreTasksExecutionContext,
1616
ProjectsMetadata,
1717
} from './public-api';
18-
import { createNodesFromFiles } from './utils';
1918
import { isIsolationEnabled } from './isolation/enabled';
2019
import { isDaemonEnabled } from '../../daemon/client/client';
2120

packages/nx/src/project-graph/plugins/public-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,19 @@ export type NxPluginV2<TOptions = unknown> = {
190190
};
191191

192192
export type PreTasksExecutionContext = {
193+
readonly id: string;
193194
readonly workspaceRoot: string;
194195
readonly nxJsonConfiguration: NxJsonConfiguration;
195196
readonly argv: string[];
196197
};
197198
export type PostTasksExecutionContext = {
199+
readonly id: string;
198200
readonly workspaceRoot: string;
199201
readonly nxJsonConfiguration: NxJsonConfiguration;
200202
readonly taskResults: TaskResults;
201203
readonly argv: string[];
204+
readonly startTime: number;
205+
readonly endTime: number;
202206
};
203207

204208
export type PreTasksExecution<TOptions = unknown> = (

packages/nx/src/tasks-runner/life-cycle.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ export interface TaskMetadata {
2222
groupId: number;
2323
}
2424

25-
interface RustRunningTask extends RunningTask {
26-
getResults(): Promise<{ code: number; terminalOutput: string }>;
27-
28-
onExit(cb: (code: number, terminalOutput: string) => void): void;
29-
30-
kill(signal?: NodeJS.Signals): Promise<void> | void;
31-
}
32-
3325
export interface LifeCycle {
3426
startCommand?(parallel?: number): void | Promise<void>;
3527

packages/nx/src/tasks-runner/run-command.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
getTaskDetails,
1818
hashTasksThatDoNotDependOnOutputsOfOtherTasks,
1919
} from '../hasher/hash-task';
20-
import { logDebug, RunMode } from '../native';
20+
import { hashArray, logDebug, RunMode } from '../native';
2121
import {
2222
runPostTasksExecution,
2323
runPreTasksExecution,
@@ -133,8 +133,6 @@ async function getTerminalOutputLifeCycle(
133133
const isRunOne = initiatingProject != null;
134134

135135
const pinnedTasks: string[] = [];
136-
const taskText = tasks.length === 1 ? 'task' : 'tasks';
137-
const projectText = projectNames.length === 1 ? 'project' : 'projects';
138136
let titleText = '';
139137

140138
if (isRunOne) {
@@ -439,12 +437,15 @@ export async function runCommand(
439437
const status = await handleErrors(
440438
process.env.NX_VERBOSE_LOGGING === 'true',
441439
async () => {
440+
const id = hashArray([...process.argv, Date.now().toString()]);
442441
await runPreTasksExecution({
442+
id,
443443
workspaceRoot,
444444
nxJsonConfiguration: nxJson,
445445
argv: process.argv,
446446
});
447447

448+
const startTime = Date.now();
448449
const { taskResults, completed } = await runCommandForTasks(
449450
projectsToRun,
450451
currentProjectGraph,
@@ -461,6 +462,7 @@ export async function runCommand(
461462
extraTargetDependencies,
462463
extraOptions
463464
);
465+
const endTime = Date.now();
464466

465467
const exitCode = !completed
466468
? signalToCode('SIGINT')
@@ -472,10 +474,13 @@ export async function runCommand(
472474
: 0;
473475

474476
await runPostTasksExecution({
477+
id,
475478
taskResults,
476479
workspaceRoot,
477480
nxJsonConfiguration: nxJson,
478481
argv: process.argv,
482+
startTime,
483+
endTime,
479484
});
480485

481486
return exitCode;

0 commit comments

Comments
 (0)