From e70f91170f06a5778d5ef13ba2bd98d4b906240e Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 29 Jan 2020 11:13:53 -0500 Subject: [PATCH] feat(core): add life cycle methods to the new tasks runner --- .../src/tasks-runner/task-orchestrator.ts | 9 ++++++++- .../workspace/src/tasks-runner/tasks-runner-v2.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/workspace/src/tasks-runner/task-orchestrator.ts b/packages/workspace/src/tasks-runner/task-orchestrator.ts index 9afca4d53cac5..1ce3af60fbc80 100644 --- a/packages/workspace/src/tasks-runner/task-orchestrator.ts +++ b/packages/workspace/src/tasks-runner/task-orchestrator.ts @@ -1,6 +1,5 @@ import { Cache, TaskWithCachedResult } from './cache'; import { cliCommand } from '../core/file-utils'; -import { NxJson } from '../core/shared-interfaces'; import { ProjectGraph } from '../core/project-graph'; import { AffectedEventType, Task } from './tasks-runner'; import { getCommand, getOutputs } from './utils'; @@ -88,10 +87,14 @@ export class TaskOrchestrator { private applyCachedResults(tasks: TaskWithCachedResult[]) { tasks.forEach(t => { + this.options.lifeCycle.startTask(t.task); + output.note({ title: `Cached Output:` }); process.stdout.write(t.cachedResult.terminalOutput); const outputs = getOutputs(this.projectGraph.nodes, t.task); this.cache.copyFilesFromCache(t.cachedResult, outputs); + + this.options.lifeCycle.endTask(t.task, 0); }); return tasks.reduce((m, c) => { @@ -109,6 +112,8 @@ export class TaskOrchestrator { const outputPath = this.cache.temporaryOutputPath(task); return new Promise((res, rej) => { try { + this.options.lifeCycle.startTask(task); + const env = { ...process.env }; if (outputPath) { env.NX_TERMINAL_OUTPUT_PATH = outputPath; @@ -120,9 +125,11 @@ export class TaskOrchestrator { p.on('close', code => { if (outputPath && code === 0) { this.cache.put(task, outputPath, taskOutputs).then(() => { + this.options.lifeCycle.endTask(task, code); res(code); }); } else { + this.options.lifeCycle.endTask(task, code); res(code); } }); diff --git a/packages/workspace/src/tasks-runner/tasks-runner-v2.ts b/packages/workspace/src/tasks-runner/tasks-runner-v2.ts index 0acae9ac647c7..3cd710f9ee27f 100644 --- a/packages/workspace/src/tasks-runner/tasks-runner-v2.ts +++ b/packages/workspace/src/tasks-runner/tasks-runner-v2.ts @@ -15,12 +15,23 @@ export interface RemoteCache { store: (hash: string, cacheDirectory: string) => Promise; } +export interface LifeCycle { + startTask(task: Task): void; + endTask(task: Task, code: number): void; +} + +class NoopLifeCycle implements LifeCycle { + startTask(task: Task): void {} + endTask(task: Task, code: number): void {} +} + export interface DefaultTasksRunnerOptions { parallel?: boolean; maxParallel?: number; cacheableOperations?: string[]; cacheDirectory?: string; remoteCache?: RemoteCache; + lifeCycle?: LifeCycle; } export const tasksRunnerV2: TasksRunner = ( @@ -28,6 +39,10 @@ export const tasksRunnerV2: TasksRunner = ( options: DefaultTasksRunnerOptions, context: { target: string; projectGraph: ProjectGraph; nxJson: NxJson } ): Observable => { + if (!options.lifeCycle) { + options.lifeCycle = new NoopLifeCycle(); + } + return new Observable(subscriber => { runAllTasks(tasks, options, context) .then(data => data.forEach(d => subscriber.next(d)))