diff --git a/garden-service/src/task-graph.ts b/garden-service/src/task-graph.ts index 0f23a3c8fa..044fa6fb07 100644 --- a/garden-service/src/task-graph.ts +++ b/garden-service/src/task-graph.ts @@ -96,13 +96,15 @@ export class TaskGraph { } private async addTaskInternal(task: BaseTask) { - this.garden.events.emit("taskPending", { - addedAt: new Date(), - key: task.getKey(), - version: task.version, - }) await this.addNodeWithDependencies(task) await this.rebuild() + if (this.index.getNode(task)) { + this.garden.events.emit("taskPending", { + addedAt: new Date(), + key: task.getKey(), + version: task.version, + }) + } } private getNode(task: BaseTask): TaskNode | null { diff --git a/garden-service/test/src/task-graph.ts b/garden-service/test/src/task-graph.ts index fe78637833..a21927f2c1 100644 --- a/garden-service/test/src/task-graph.ts +++ b/garden-service/test/src/task-graph.ts @@ -133,6 +133,28 @@ describe("task-graph", () => { ]) }) + it.only("should not emit a taskPending event when adding a task with a cached result", async () => { + const now = freezeTime() + + const garden = await getGarden() + const graph = new TaskGraph(garden, garden.log) + + const task = new TestTask(garden, "a", false) + await graph.addTask(task) + const result = await graph.processTasks() + + // repeatedTask has the same baseKey and version as task, so its result is already cached + const repeatedTask = new TestTask(garden, "a", false) + await graph.addTask(repeatedTask) + + expect(garden.events.log).to.eql([ + { name: "taskPending", payload: { addedAt: now, key: task.getKey(), version: task.version } }, + { name: "taskGraphProcessing", payload: { startedAt: now } }, + { name: "taskComplete", payload: result["a"] }, + { name: "taskGraphComplete", payload: { completedAt: now } }, + ]) + }) + it("should emit events when processing and completing a task", async () => { const now = freezeTime()