Skip to content

Commit

Permalink
feat(workflows): add duration to step events
Browse files Browse the repository at this point in the history
  • Loading branch information
thsig authored and edvald committed Jun 26, 2020
1 parent f8854c9 commit 07d8059
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
16 changes: 11 additions & 5 deletions garden-service/src/commands/run/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ConfigurationError, FilesystemError } from "../../exceptions"
import { posix, join } from "path"
import { ensureDir, writeFile } from "fs-extra"
import Bluebird from "bluebird"
import { splitStream } from "../../util/util"
import { splitStream, getDurationMsec } from "../../util/util"
import execa, { ExecaError } from "execa"
import { LogLevel } from "../../logger/log-node"

Expand Down Expand Up @@ -107,6 +107,8 @@ export class RunWorkflowCommand extends Command<Args, {}> {
stepName,
})

const stepStartedAt = new Date()

try {
if (step.command) {
step.command = resolveTemplateStrings(step.command, stepTemplateContext)
Expand All @@ -132,11 +134,11 @@ export class RunWorkflowCommand extends Command<Args, {}> {
footerLog: stepFooterLog,
})
} else {
garden.events.emit("workflowStepError", { index })
garden.events.emit("workflowStepError", getStepEndEvent(index, stepStartedAt))
throw new ConfigurationError(`Workflow steps must specify either a command or a script.`, { step })
}
} catch (err) {
garden.events.emit("workflowStepError", { index })
garden.events.emit("workflowStepError", getStepEndEvent(index, stepStartedAt))
printStepDuration({
log: outerLog,
stepIndex: index,
Expand All @@ -160,12 +162,12 @@ export class RunWorkflowCommand extends Command<Args, {}> {
}

if (stepResult.errors) {
garden.events.emit("workflowStepError", { index })
garden.events.emit("workflowStepError", getStepEndEvent(index, stepStartedAt))
logErrors(outerLog, stepResult.errors, index, steps.length, step.description)
return { result, errors: stepResult.errors }
}

garden.events.emit("workflowStepComplete", { index })
garden.events.emit("workflowStepComplete", getStepEndEvent(index, stepStartedAt))
printStepDuration({
log: outerLog,
stepIndex: index,
Expand Down Expand Up @@ -401,3 +403,7 @@ export async function runStepScript({ garden, log, step }: RunStepParams): Promi
})
}
}

function getStepEndEvent(index: number, startedAt: Date) {
return { index, durationMsec: getDurationMsec(startedAt, new Date()) }
}
2 changes: 2 additions & 0 deletions garden-service/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ export interface Events extends LoggerEvents {
}
workflowStepComplete: {
index: number
durationMsec: number
}
workflowStepError: {
index: number
durationMsec: number
}
}

Expand Down
31 changes: 19 additions & 12 deletions garden-service/test/unit/src/commands/run/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,19 @@ describe("RunWorkflowCommand", () => {

await cmd.action({ ..._defaultParams, args: { workflow: "workflow-a" } })

const workflowEvents = getWorkflowEvents(_garden)
expect(workflowEvents).to.eql([
{ name: "workflowStepProcessing", payload: { index: 0 } },
{ name: "workflowStepComplete", payload: { index: 0 } },
{ name: "workflowStepProcessing", payload: { index: 1 } },
{ name: "workflowStepComplete", payload: { index: 1 } },
])
const we = getWorkflowEvents(_garden)

expect(we[0]).to.eql({ name: "workflowStepProcessing", payload: { index: 0 } })

expect(we[1].name).to.eql("workflowStepComplete")
expect(we[1].payload.index).to.eql(0)
expect(we[1].payload.durationMsec).to.gte(0)

expect(we[2]).to.eql({ name: "workflowStepProcessing", payload: { index: 1 } })

expect(we[3].name).to.eql("workflowStepComplete")
expect(we[3].payload.index).to.eql(1)
expect(we[3].payload.durationMsec).to.gte(0)
})

function filterLogEntries(entries: LogEntry[], msgRegex: RegExp): LogEntry[] {
Expand Down Expand Up @@ -309,11 +315,12 @@ describe("RunWorkflowCommand", () => {
})
expect(testModuleLog.length).to.eql(0)

const workflowEvents = getWorkflowEvents(_garden)
expect(workflowEvents).to.eql([
{ name: "workflowStepProcessing", payload: { index: 0 } },
{ name: "workflowStepError", payload: { index: 0 } },
])
const we = getWorkflowEvents(_garden)

expect(we[0]).to.eql({ name: "workflowStepProcessing", payload: { index: 0 } })
expect(we[1].name).to.eql("workflowStepError")
expect(we[1].payload.index).to.eql(0)
expect(we[1].payload.durationMsec).to.gte(0)
})

it("should write a file with string data ahead of the run, before resolving providers", async () => {
Expand Down

0 comments on commit 07d8059

Please sign in to comment.