Skip to content

Commit

Permalink
chore(core): create get cache function (nrwl#27779)
Browse files Browse the repository at this point in the history
## Current Behavior
<!-- This is the behavior we have today -->

The orchestrator creates the cache.

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

The cache file has a function which the orchestrator can use to get the
cache.

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

Fixes #
  • Loading branch information
FrozenPandaz authored and ZackDeRose committed Sep 6, 2024
1 parent 7dda5d0 commit 396042b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 17 additions & 2 deletions packages/nx/src/tasks-runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { spawn } from 'child_process';
import { cacheDir } from '../utils/cache-directory';
import { Task } from '../config/task-graph';
import { machineId } from 'node-machine-id';
import { NxCache } from '../native';
import { NxCache, CachedResult as NativeCacheResult } from '../native';
import { getDbConnection } from '../utils/db-connection';
import { isNxCloudUsed } from '../utils/nx-cloud-utils';
import { readNxJson } from '../config/nx-json';
Expand All @@ -26,6 +26,17 @@ export type CachedResult = {
};
export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult };

export function getCache(options: DefaultTasksRunnerOptions) {
return process.env.NX_DB_CACHE === 'true'
? new DbCache({
// Remove this in Nx 21
nxCloudRemoteCache: isNxCloudUsed(readNxJson())
? options.remoteCache
: null,
})
: new Cache(options);
}

export class DbCache {
private cache = new NxCache(workspaceRoot, cacheDir, getDbConnection());
private remoteCache: RemoteCacheV2 | null;
Expand Down Expand Up @@ -56,7 +67,7 @@ export class DbCache {
);

if (res) {
this.cache.applyRemoteCacheResults(task.hash, res);
this.applyRemoteCacheResults(task.hash, res);

return {
...res,
Expand All @@ -70,6 +81,10 @@ export class DbCache {
}
}

private applyRemoteCacheResults(hash: string, res: NativeCacheResult) {
return this.cache.applyRemoteCacheResults(hash, res);
}

async put(
task: Task,
terminalOutput: string | null,
Expand Down
14 changes: 2 additions & 12 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 { Cache, DbCache } from './cache';
import { getCache } from './cache';
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { TaskStatus } from './tasks-runner';
import {
Expand All @@ -31,19 +31,9 @@ import {
import { workspaceRoot } from '../utils/workspace-root';
import { output } from '../utils/output';
import { combineOptionsForExecutor } from '../utils/params';
import { isNxCloudUsed } from '../utils/nx-cloud-utils';
import { readNxJson } from '../config/nx-json';

export class TaskOrchestrator {
private cache =
process.env.NX_DB_CACHE === 'true'
? new DbCache({
// Remove this in Nx 21
nxCloudRemoteCache: isNxCloudUsed(readNxJson())
? this.options.remoteCache
: null,
})
: new Cache(this.options);
private cache = getCache(this.options);
private forkedProcessTaskRunner = new ForkedProcessTaskRunner(this.options);

private tasksSchedule = new TasksSchedule(
Expand Down

0 comments on commit 396042b

Please sign in to comment.