Skip to content

Commit

Permalink
fix(core): move getDetails to top (#28158)
Browse files Browse the repository at this point in the history
<!-- 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
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #28035
  • Loading branch information
xiongemi authored Sep 27, 2024
1 parent 153451f commit 1a4959f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
21 changes: 9 additions & 12 deletions packages/nx/src/hasher/hash-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { getInputs, TaskHasher } from './task-hasher';
import { ProjectGraph } from '../config/project-graph';
import { NxJsonConfiguration } from '../config/nx-json';
import { readNxJson } from '../config/nx-json';
import { IS_WASM, TaskDetails } from '../native';
import { HashedTask, IS_WASM, TaskDetails } from '../native';
import { getDbConnection } from '../utils/db-connection';

let taskDetails: TaskDetails;

function getTaskDetails() {
export function getTaskDetails(): TaskDetails | null {
// TODO: Remove when wasm supports sqlite
if (process.env.NX_DISABLE_DB === 'true' || IS_WASM) {
return null;
Expand All @@ -25,12 +25,11 @@ export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks(
hasher: TaskHasher,
projectGraph: ProjectGraph,
taskGraph: TaskGraph,
nxJson: NxJsonConfiguration
nxJson: NxJsonConfiguration,
tasksDetails: TaskDetails | null
) {
performance.mark('hashMultipleTasks:start');

const taskDetails = getTaskDetails();

const tasks = Object.values(taskGraph.tasks);
const tasksWithHashers = await Promise.all(
tasks.map(async (task) => {
Expand Down Expand Up @@ -58,9 +57,8 @@ export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks(
tasksToHash[i].hash = hashes[i].value;
tasksToHash[i].hashDetails = hashes[i].details;
}
// TODO: Remove if when sqlite is always on
if (taskDetails) {
taskDetails.recordTaskDetails(
if (tasksDetails?.recordTaskDetails) {
tasksDetails.recordTaskDetails(
tasksToHash.map((task) => ({
hash: task.hash,
project: task.target.project,
Expand All @@ -83,11 +81,11 @@ export async function hashTask(
projectGraph: ProjectGraph,
taskGraph: TaskGraph,
task: Task,
env: NodeJS.ProcessEnv
env: NodeJS.ProcessEnv,
taskDetails: TaskDetails | null
) {
performance.mark('hashSingleTask:start');

const taskDetails = getTaskDetails();
const customHasher = getCustomHasher(task, projectGraph);
const projectsConfigurations =
readProjectsConfigurationFromProjectGraph(projectGraph);
Expand All @@ -106,8 +104,7 @@ export async function hashTask(
task.hash = value;
task.hashDetails = details;

// TODO: Remove if when wasm supports sqlite
if (taskDetails) {
if (taskDetails?.recordTaskDetails) {
taskDetails.recordTaskDetails([
{
hash: task.hash,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/tasks-runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult };
export function getCache(
nxJson: NxJsonConfiguration,
options: DefaultTasksRunnerOptions
) {
): DbCache | Cache {
return process.env.NX_DISABLE_DB !== 'true' &&
(nxJson.enableDbCache === true || process.env.NX_DB_CACHE === 'true')
? new DbCache({
Expand Down
11 changes: 9 additions & 2 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { Task, TaskGraph } from '../config/task-graph';
import { TargetDependencyConfig } from '../config/workspace-json-project-json';
import { daemonClient } from '../daemon/client/client';
import { createTaskHasher } from '../hasher/create-task-hasher';
import { hashTasksThatDoNotDependOnOutputsOfOtherTasks } from '../hasher/hash-task';
import {
getTaskDetails,
hashTasksThatDoNotDependOnOutputsOfOtherTasks,
} from '../hasher/hash-task';
import { IS_WASM } from '../native';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import { NxArgs } from '../utils/command-line-utils';
Expand Down Expand Up @@ -589,6 +592,9 @@ export async function invokeTasksRunner({
}): Promise<{ [id: string]: TaskResult }> {
setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);

// this needs to be done before we start to run the tasks
const taskDetails = getTaskDetails();

const { tasksRunner, runnerOptions } = getRunner(nxArgs, nxJson);

let hasher = createTaskHasher(projectGraph, nxJson, runnerOptions);
Expand All @@ -601,7 +607,8 @@ export async function invokeTasksRunner({
hasher,
projectGraph,
taskGraph,
nxJson
nxJson,
taskDetails
);
const taskResultsLifecycle = new TaskResultsLifeCycle();
const compositedLifeCycle: LifeCycle = new CompositeLifeCycle([
Expand Down
14 changes: 9 additions & 5 deletions packages/nx/src/tasks-runner/task-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { writeFileSync } from 'fs';
import { TaskHasher } from '../hasher/task-hasher';
import runCommandsImpl from '../executors/run-commands/run-commands.impl';
import { ForkedProcessTaskRunner } from './forked-process-task-runner';
import { getCache } from './cache';
import { Cache, DbCache, getCache } from './cache';
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { TaskStatus } from './tasks-runner';
import {
Expand All @@ -22,7 +22,7 @@ import { TaskMetadata } from './life-cycle';
import { ProjectGraph } from '../config/project-graph';
import { Task, TaskGraph } from '../config/task-graph';
import { DaemonClient } from '../daemon/client/client';
import { hashTask } from '../hasher/hash-task';
import { getTaskDetails, hashTask } from '../hasher/hash-task';
import {
getEnvVariablesForBatchProcess,
getEnvVariablesForTask,
Expand All @@ -32,9 +32,11 @@ import { workspaceRoot } from '../utils/workspace-root';
import { output } from '../utils/output';
import { combineOptionsForExecutor } from '../utils/params';
import { NxJsonConfiguration } from '../config/nx-json';
import type { TaskDetails } from '../native';

export class TaskOrchestrator {
private cache = getCache(this.nxJson, this.options);
private taskDetails: TaskDetails | null = getTaskDetails();
private cache: DbCache | Cache = getCache(this.nxJson, this.options);
private forkedProcessTaskRunner = new ForkedProcessTaskRunner(this.options);

private tasksSchedule = new TasksSchedule(
Expand Down Expand Up @@ -163,7 +165,8 @@ export class TaskOrchestrator {
this.projectGraph,
this.taskGraph,
task,
taskSpecificEnv
taskSpecificEnv,
this.taskDetails
);
}

Expand All @@ -181,7 +184,8 @@ export class TaskOrchestrator {
this.projectGraph,
this.taskGraph,
task,
this.batchEnv
this.batchEnv,
this.taskDetails
);
}
await this.options.lifeCycle.scheduleTask(task);
Expand Down

0 comments on commit 1a4959f

Please sign in to comment.