Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): add flag when db is disabled for task history #28059

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Task } from '../../config/task-graph';
import { output } from '../../utils/output';
import { LifeCycle, TaskResult } from '../life-cycle';
import type { TaskRun as NativeTaskRun } from '../../native';
import { getTaskHistory } from '../../utils/task-history';
import { getTaskHistory, TaskHistory } from '../../utils/task-history';

interface TaskRun extends NativeTaskRun {
target: Task['target'];
Expand All @@ -12,7 +12,7 @@ interface TaskRun extends NativeTaskRun {
export class TaskHistoryLifeCycle implements LifeCycle {
private startTimings: Record<string, number> = {};
private taskRuns = new Map<string, TaskRun>();
private taskHistory = getTaskHistory();
private taskHistory: TaskHistory | null = getTaskHistory();

startTasks(tasks: Task[]): void {
for (let task of tasks) {
Expand All @@ -38,6 +38,9 @@ export class TaskHistoryLifeCycle implements LifeCycle {

async endCommand() {
const entries = Array.from(this.taskRuns);
if (!this.taskHistory) {
return;
}
await this.taskHistory.recordTaskRuns(entries.map(([_, v]) => v));
const flakyTasks = await this.taskHistory.getFlakyTasks(
entries.map(([hash]) => hash)
Expand Down
3 changes: 1 addition & 2 deletions packages/nx/src/tasks-runner/tasks-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export class TasksSchedule {
private notScheduledTaskGraph = this.taskGraph;
private reverseTaskDeps = calculateReverseDeps(this.taskGraph);
private reverseProjectGraph = reverse(this.projectGraph);
private taskHistory: TaskHistory =
process.env.NX_DISABLE_DB !== 'true' ? getTaskHistory() : null;
private taskHistory: TaskHistory | null = getTaskHistory();

private scheduledBatches: Batch[] = [];
private scheduledTasks: string[] = [];
Expand Down
10 changes: 7 additions & 3 deletions packages/nx/src/utils/task-history.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { daemonClient } from '../daemon/client/client';
import { isOnDaemon } from '../daemon/is-on-daemon';
import { NxTaskHistory, TaskRun, TaskTarget } from '../native';
import { IS_WASM, NxTaskHistory, TaskRun, TaskTarget } from '../native';
import { getDbConnection } from './db-connection';

export class TaskHistory {
Expand Down Expand Up @@ -39,9 +39,13 @@ let taskHistory: TaskHistory;

/**
* This function returns the singleton instance of TaskHistory
* @returns singleton instance of TaskHistory
* @returns singleton instance of TaskHistory, null if database is disabled or WASM is not enabled
*/
export function getTaskHistory(): TaskHistory {
export function getTaskHistory(): TaskHistory | null {
if (process.env.NX_DISABLE_DB === 'true' || !IS_WASM) {
return null;
}

if (!taskHistory) {
taskHistory = new TaskHistory();
}
Expand Down