Skip to content

Commit

Permalink
refactor(graph): make sure all tasks are included in process results
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 4, 2019
1 parent e10ea41 commit 91afd59
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 119 deletions.
9 changes: 4 additions & 5 deletions garden-service/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function publishModules(
forceBuild: boolean,
allowDirty: boolean,
): Promise<TaskResults> {
for (const module of modules) {
const tasks = modules.map(module => {
const version = module.version

if (version.dirtyTimestamp && !allowDirty) {
Expand All @@ -91,9 +91,8 @@ export async function publishModules(
)
}

const task = new PublishTask({ garden, log, module, forceBuild })
await garden.addTask(task)
}
return new PublishTask({ garden, log, module, forceBuild })
})

return await garden.processTasks()
return await garden.processTasks(tasks)
}
3 changes: 1 addition & 2 deletions garden-service/src/commands/run/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ export class RunModuleCommand extends Command<Args, Opts> {
await garden.actions.prepareEnvironment({ log })

const pushTask = new PushTask({ garden, log, module, force: opts["force-build"] })
await garden.addTask(pushTask)
await garden.processTasks()
await garden.processTasks([pushTask])

const command = args.command || []

Expand Down
3 changes: 1 addition & 2 deletions garden-service/src/commands/run/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export class RunServiceCommand extends Command<Args, Opts> {
await garden.actions.prepareEnvironment({ log })

const pushTask = new PushTask({ garden, log, module, force: opts["force-build"] })
await garden.addTask(pushTask)
await garden.processTasks()
await garden.processTasks([pushTask])

const dependencies = await graph.getServices(module.serviceDependencyNames)
const runtimeContext = await prepareRuntimeContext(garden, graph, module, dependencies)
Expand Down
4 changes: 1 addition & 3 deletions garden-service/src/commands/run/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export class RunTaskCommand extends Command<Args, Opts> {
await garden.actions.prepareEnvironment({ log })

const taskTask = await TaskTask.factory({ garden, graph, task, log, force: true, forceBuild: opts["force-build"] })
await garden.addTask(taskTask)

const result = (await garden.processTasks())[taskTask.getBaseKey()]
const result = (await garden.processTasks([taskTask]))[taskTask.getBaseKey()]

if (!result.error) {
log.info("")
Expand Down
3 changes: 1 addition & 2 deletions garden-service/src/commands/run/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ export class RunTestCommand extends Command<Args, Opts> {
await garden.actions.prepareEnvironment({ log })

const pushTask = new PushTask({ garden, log, module, force: opts["force-build"] })
await garden.addTask(pushTask)
await garden.processTasks()
await garden.processTasks([pushTask])

const interactive = opts.interactive
const deps = await graph.getDependencies("test", testConfig.name, false)
Expand Down
8 changes: 2 additions & 6 deletions garden-service/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,8 @@ export class Garden {
return this.buildDir.clear()
}

async addTask(task: BaseTask) {
await this.taskGraph.addTask(task)
}

async processTasks(): Promise<TaskResults> {
return this.taskGraph.processTasks()
async processTasks(tasks: BaseTask[]): Promise<TaskResults> {
return this.taskGraph.process(tasks)
}

/**
Expand Down
17 changes: 7 additions & 10 deletions garden-service/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Bluebird = require("bluebird")
import chalk from "chalk"
import { padEnd, keyBy } from "lodash"
import { padEnd, keyBy, flatten } from "lodash"

import { Module } from "./types/module"
import { Service } from "./types/service"
Expand Down Expand Up @@ -85,10 +85,7 @@ export async function processModules(
log.info(divider)
}

for (const module of modules) {
const tasks = await handler(graph, module)
await Bluebird.map(tasks, t => garden.addTask(t))
}
const tasks: BaseTask[] = flatten(await Bluebird.map(modules, module => handler(graph, module)))

if (watch && !!logFooter) {
garden.events.on("taskGraphProcessing", () => {
Expand All @@ -100,7 +97,7 @@ export async function processModules(
})
}

const results = await garden.processTasks()
const results = await garden.processTasks(tasks)

if (!watch) {
return {
Expand Down Expand Up @@ -164,11 +161,11 @@ export async function processModules(
// Make sure the modules' versions are up to date.
const changedModules = await graph.getModules(changedModuleNames)

await Bluebird.map(changedModules, async (m) => {
const moduleTasks = flatten(await Bluebird.map(changedModules, async (m) => {
modulesByName[m.name] = m
return Bluebird.map(changeHandler!(graph, m), (task) => garden.addTask(task))
})
await garden.processTasks()
return changeHandler!(graph, m)
}))
await garden.processTasks(moduleTasks)
})
})

Expand Down
36 changes: 18 additions & 18 deletions garden-service/src/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,14 @@ export class TaskGraph {
this.logEntryMap = {}
}

async addTask(task: BaseTask): Promise<void> {
return this.opQueue.add(() => this.addTaskInternal(task))
}

async processTasks(): Promise<TaskResults> {
return this.opQueue.add(() => this.processTasksInternal())
async process(tasks: BaseTask[]): Promise<TaskResults> {
return this.opQueue.add(() => this.processTasksInternal(tasks))
}

/**
* Rebuilds the dependency relationships between the TaskNodes in this.index, and updates this.roots accordingly.
*/
private async rebuild() {
private rebuild() {
const taskNodes = this.index.getNodes()

// this.taskDependencyCache will already have been populated at this point (happens in addTaskInternal).
Expand All @@ -95,9 +91,9 @@ export class TaskGraph {
this.roots.setNodes(newRootNodes)
}

private async addTaskInternal(task: BaseTask) {
private async addTask(task: BaseTask) {
await this.addNodeWithDependencies(task)
await this.rebuild()
this.rebuild()
if (this.index.getNode(task)) {
this.garden.events.emit("taskPending", {
addedAt: new Date(),
Expand Down Expand Up @@ -131,7 +127,11 @@ export class TaskGraph {
/**
* Process the graph until it's complete.
*/
private async processTasksInternal(): Promise<TaskResults> {
private async processTasksInternal(tasks: BaseTask[]): Promise<TaskResults> {
for (const task of tasks) {
await this.addTask(task)
}

this.log.silly("")
this.log.silly("TaskGraph: this.index before processing")
this.log.silly("---------------------------------------")
Expand All @@ -155,7 +155,7 @@ export class TaskGraph {
.slice(0, _this.concurrency - this.inProgress.length)

batch.forEach(n => this.inProgress.addNode(n))
await this.rebuild()
this.rebuild()

this.initLogging()

Expand Down Expand Up @@ -185,13 +185,13 @@ export class TaskGraph {
result.error = error
this.garden.events.emit("taskError", result)
this.logTaskError(node, error)
await this.cancelDependants(node)
this.cancelDependants(node)
} finally {
results[baseKey] = result
this.resultCache.put(baseKey, task.version.versionString, result)
}
} finally {
await this.completeTask(node, !result.error)
this.completeTask(node, !result.error)
}

return loop()
Expand All @@ -200,7 +200,7 @@ export class TaskGraph {

await loop()

await this.rebuild()
this.rebuild()

return results
}
Expand All @@ -225,14 +225,14 @@ export class TaskGraph {
}
}

private async completeTask(node: TaskNode, success: boolean) {
private completeTask(node: TaskNode, success: boolean) {
if (node.getDependencies().length > 0) {
throw new TaskGraphError(`Task ${node.getKey()} still has unprocessed dependencies`)
}

this.remove(node)
this.logTaskComplete(node, success)
await this.rebuild()
this.rebuild()
}

private remove(node: TaskNode) {
Expand All @@ -241,12 +241,12 @@ export class TaskGraph {
}

// Recursively remove node's dependants, without removing node.
private async cancelDependants(node: TaskNode) {
private cancelDependants(node: TaskNode) {
for (const dependant of this.getDependants(node)) {
this.logTaskComplete(dependant, false)
this.remove(dependant)
}
await this.rebuild()
this.rebuild()
}

private getDependants(node: TaskNode): TaskNode[] {
Expand Down
20 changes: 8 additions & 12 deletions garden-service/test/unit/src/build-dir.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Bluebird from "bluebird"
const nodetree = require("nodetree")
import { join } from "path"
import { pathExists, readdir } from "fs-extra"
Expand Down Expand Up @@ -69,17 +68,14 @@ describe("BuildDir", () => {
await garden.clearBuilds()
const graph = await garden.getConfigGraph()
const modules = await graph.getModules()

await Bluebird.map(modules, async (module) => {
return garden.addTask(new BuildTask({
garden,
log,
module,
force: true,
}))
})

await garden.processTasks()
const tasks = modules.map(module => new BuildTask({
garden,
log,
module,
force: true,
}))

await garden.processTasks(tasks)

const buildDirD = await garden.buildDir.buildPath("module-d")
const buildDirE = await garden.buildDir.buildPath("module-e")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ describe("Helm common functions", () => {

async function buildModules() {
const modules = await graph.getModules()
for (const module of modules) {
await garden.addTask(new BuildTask({ garden, log, module, force: false }))
}
const results = await garden.processTasks()
const tasks = modules.map(module => new BuildTask({ garden, log, module, force: false }))
const results = await garden.processTasks(tasks)

const err = first(Object.values(results).map(r => r.error))

Expand Down
Loading

0 comments on commit 91afd59

Please sign in to comment.