Skip to content

Commit

Permalink
fix(core): add flag when db is disabled for task history
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Sep 23, 2024
1 parent 341306a commit 062a838
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
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

0 comments on commit 062a838

Please sign in to comment.