Skip to content

Commit

Permalink
improvement(server): also send sessions events over ws
Browse files Browse the repository at this point in the history
Before this change, we only POSTed session events over http but did not
send over ws.

It's actually useful to do that as well so that we can better display
currently running commands on the live page.

NOTE: Also reverted a change to the "commandStart" event from the
following commit: a1742a6 since we're
now reading this data via "commandInfo" events
eysi09 committed Jul 12, 2023
1 parent bc4a60a commit 55fdeee
Showing 3 changed files with 29 additions and 13 deletions.
9 changes: 5 additions & 4 deletions core/src/commands/base.ts
Original file line number Diff line number Diff line change
@@ -372,7 +372,7 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =
try {
if (cloudSession && this.streamEvents) {
log.silly(`Connecting Garden instance events to Cloud API`)
cloudEventStream.emit("commandInfo", {
garden.events.emit("commandInfo", {
...commandInfo,
environmentName: garden.environmentName,
environmentId: cloudSession.environmentId,
@@ -384,6 +384,7 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =
vcsBranch: garden.vcsInfo.branch,
vcsCommitHash: garden.vcsInfo.commitHash,
vcsOriginUrl: garden.vcsInfo.originUrl,
sessionId: garden.sessionId,
})
}

@@ -421,9 +422,9 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =
)

if (allErrors.length > 0) {
cloudEventStream.emit("sessionFailed", {})
garden.events.emit("sessionFailed", {})
} else {
cloudEventStream.emit("sessionCompleted", {})
garden.events.emit("sessionCompleted", {})
}
} catch (err) {
analytics?.trackCommandResult(
@@ -433,7 +434,7 @@ export abstract class Command<A extends Parameters = {}, O extends Parameters =
1,
parentSessionId || undefined
)
cloudEventStream.emit("sessionFailed", {})
garden.events.emit("sessionFailed", {})
throw err
} finally {
if (parentSessionId) {
29 changes: 23 additions & 6 deletions core/src/events/events.ts
Original file line number Diff line number Diff line change
@@ -165,6 +165,7 @@ export interface CommandInfoPayload extends CommandInfo {
vcsBranch: string
vcsCommitHash: string
vcsOriginUrl: string
sessionId: string
}

export function toGraphResultEventPayload(result: GraphResult): GraphResultEventPayload {
@@ -292,8 +293,16 @@ export type ActionStatusEventName = PickFromUnion<
EventName,
"buildStatus" | "deployStatus" | "testStatus" | "runStatus"
>
type GraphEventName = Extract<EventName, "taskCancelled" | "taskComplete" | "taskError" | "taskProcessing">
type ConfigEventName = Extract<EventName, "configChanged" | "configsScanned" | "autocompleterUpdated">
type PipedWsEventName = Extract<
EventName,
| "commandInfo"
| "configChanged"
| "configsScanned"
| "autocompleterUpdated"
| "sessionCancelled"
| "sessionCompleted"
| "sessionFailed"
>

// These are the events we POST over https via the BufferedEventStream
const pipedEventNamesSet = new Set<EventName>([
@@ -329,14 +338,22 @@ const actionStatusEventNames = new Set<ActionStatusEventName>([
"runStatus",
"testStatus",
])
const configEventNames = new Set<ConfigEventName>(["configsScanned", "configChanged", "autocompleterUpdated"])
const pipedWsEventNamesSet = new Set<PipedWsEventName>([
"commandInfo",
"configsScanned",
"configChanged",
"autocompleterUpdated",
"sessionCompleted",
"sessionFailed",
"sessionCancelled",
])

const isPipedEvent = (name: string, _payload: any): _payload is Events[EventName] => {
return pipedEventNamesSet.has(<any>name)
}

const isConfigEvent = (name: string, _payload: any): _payload is Events[ConfigEventName] => {
return configEventNames.has(<any>name)
const isPipedWsEvent = (name: string, _payload: any): _payload is Events[PipedWsEventName] => {
return pipedWsEventNamesSet.has(<any>name)
}

const isActionStatusEvent = (name: string, _payload: any): _payload is Events[ActionStatusEventName] => {
@@ -349,7 +366,7 @@ export function shouldStreamWsEvent(name: string, payload: any) {
if (gardenKey && isActionStatusEvent(name, payload)) {
return true
}
if (isConfigEvent(name, payload)) {
if (isPipedWsEvent(name, payload)) {
return true
}

4 changes: 1 addition & 3 deletions core/src/server/server.ts
Original file line number Diff line number Diff line change
@@ -687,14 +687,12 @@ export class GardenServer extends EventEmitter {
command
.prepare(prepareParams)
.then(() => {
if (!isInternal) {
if (persistent) {
send("commandStart", {
...commandResponseBase,
args,
opts,
})
}
if (persistent) {
this.activePersistentRequests[requestId] = { command, connection }

command.subscribe((data: any) => {

0 comments on commit 55fdeee

Please sign in to comment.