Skip to content

Commit

Permalink
feat(cloud): emit session events on exit
Browse files Browse the repository at this point in the history
We now emit session end events when the command exits.
  • Loading branch information
thsig committed Oct 1, 2021
1 parent a506498 commit 04f58d5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
29 changes: 24 additions & 5 deletions core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dotenv = require("dotenv")
import { intersection, sortBy } from "lodash"
import { resolve, join } from "path"
import { getAllCommands } from "../commands/commands"
import { shutdown, sleep, getPackageVersion, uuidv4 } from "../util/util"
import { shutdown, sleep, getPackageVersion, uuidv4, registerCleanupFunction } from "../util/util"
import { Command, CommandResult, CommandGroup } from "../commands/base"
import { GardenError, PluginError, toGardenError, GardenBaseError } from "../exceptions"
import { Garden, GardenOpts, DummyGarden } from "../garden"
Expand Down Expand Up @@ -75,6 +75,8 @@ export class GardenCli {
private commands: { [key: string]: Command } = {}
private fileWritersInitialized: boolean = false
private plugins: GardenPluginCallback[]
private bufferedEventStream: BufferedEventStream | undefined
private sessionFinished = false

constructor({ plugins }: { plugins?: GardenPluginCallback[] } = {}) {
this.plugins = plugins || []
Expand Down Expand Up @@ -198,11 +200,19 @@ ${renderCommands(commands)}

// Init event & log streaming.
const sessionId = uuidv4()
const bufferedEventStream = new BufferedEventStream({
this.bufferedEventStream = new BufferedEventStream({
log,
enterpriseApi: enterpriseApi || undefined,
sessionId,
})

registerCleanupFunction("stream-session-cancelled-event", () => {
if (!this.sessionFinished) {
this.bufferedEventStream?.streamEvent("sessionCancelled", {})
this.bufferedEventStream?.flushAll()
}
})

const dashboardEventStream = new DashboardEventStream({ log, sessionId })

const commandInfo = {
Expand Down Expand Up @@ -286,7 +296,7 @@ ${renderCommands(commands)}

if (enterpriseApi) {
log.silly(`Connecting Garden instance to GE BufferedEventStream`)
bufferedEventStream.connect({
this.bufferedEventStream.connect({
garden,
streamEvents,
streamLogEntries,
Expand All @@ -297,7 +307,7 @@ ${renderCommands(commands)}
],
})
if (streamEvents) {
bufferedEventStream.streamEvent("commandInfo", commandInfo)
this.bufferedEventStream.streamEvent("commandInfo", commandInfo)
}
}

Expand Down Expand Up @@ -340,7 +350,6 @@ ${renderCommands(commands)}
throw err
} finally {
if (!result.restartRequired) {
await bufferedEventStream.close()
await dashboardEventStream.close()
await command.server?.close()
enterpriseApi?.close()
Expand Down Expand Up @@ -494,6 +503,16 @@ ${renderCommands(commands)}
logger.cleanup()
}

if (this.bufferedEventStream) {
if (code === 0) {
this.bufferedEventStream.streamEvent("sessionCompleted", {})
} else {
this.bufferedEventStream.streamEvent("sessionFailed", {})
}
await this.bufferedEventStream.close()
this.sessionFinished = true
}

return { argv, code, errors, result: commandResult?.result }
}
}
9 changes: 9 additions & 0 deletions core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export interface Events extends LoggerEvents {
}
receivedToken: AuthTokenResponse

// Session events - one of these is emitted when the command process ends
sessionCompleted: {} // Command exited with a 0 status
sessionFailed: {} // Command exited with a nonzero status
sessionCancelled: {} // Command exited because of an interrupt signal (e.g. CTRL-C)

// Watcher events
configAdded: {
path: string
Expand All @@ -125,6 +130,7 @@ export interface Events extends LoggerEvents {

// Command/project metadata events
commandInfo: CommandInfo

// Stack Graph events
stackGraph: RenderedActionGraph

Expand Down Expand Up @@ -269,6 +275,9 @@ export const pipedEventNames: EventName[] = [
"_restart",
"_test",
"_workflowRunRegistered",
"sessionCompleted",
"sessionFailed",
"sessionCancelled",
"configAdded",
"configRemoved",
"internalError",
Expand Down

0 comments on commit 04f58d5

Please sign in to comment.