Skip to content

Commit

Permalink
feat(core): add life cycle methods to the new tasks runner
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Feb 2, 2020
1 parent 6e069c4 commit e70f911
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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';
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
Expand All @@ -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);
}
});
Expand Down
15 changes: 15 additions & 0 deletions packages/workspace/src/tasks-runner/tasks-runner-v2.ts
Expand Up @@ -15,19 +15,34 @@ export interface RemoteCache {
store: (hash: string, cacheDirectory: string) => Promise<boolean>;
}

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<DefaultTasksRunnerOptions> = (
tasks: Task[],
options: DefaultTasksRunnerOptions,
context: { target: string; projectGraph: ProjectGraph; nxJson: NxJson }
): Observable<TaskCompleteEvent> => {
if (!options.lifeCycle) {
options.lifeCycle = new NoopLifeCycle();
}

return new Observable(subscriber => {
runAllTasks(tasks, options, context)
.then(data => data.forEach(d => subscriber.next(d)))
Expand Down

0 comments on commit e70f911

Please sign in to comment.