Skip to content

Commit

Permalink
improvement(server): add some useful fields to ws event payload (#4727)
Browse files Browse the repository at this point in the history
* improvement(server): add some useful fields to ws event payload
* test(server): fix failing tests

---------

Co-authored-by: Vladimir Vagaytsev <[email protected]>
  • Loading branch information
eysi09 and vvagaytsev authored Jul 3, 2023
1 parent 78669da commit 3c2022c
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 36 deletions.
53 changes: 48 additions & 5 deletions core/src/actions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,17 @@ export function getRelativeActionConfigPath(projectRoot: string, action: Action)
return relPath.startsWith("..") ? relPath : "." + sep + relPath
}

export async function getDeployStatusPayloads(router: ActionRouter, graph: ResolvedConfigGraph, log: Log) {
export async function getDeployStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getDeploys()

return fromPairs(
Expand All @@ -159,14 +169,25 @@ export async function getDeployStatusPayloads(router: ActionRouter, graph: Resol
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<DeployStatusForEventPayload>

return [action.name, payload]
})
)
}

export async function getBuildStatusPayloads(router: ActionRouter, graph: ResolvedConfigGraph, log: Log) {
export async function getBuildStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getBuilds()

return fromPairs(
Expand All @@ -181,14 +202,25 @@ export async function getBuildStatusPayloads(router: ActionRouter, graph: Resolv
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<BuildStatusForEventPayload>

return [action.name, payload]
})
)
}

export async function getTestStatusPayloads(router: ActionRouter, graph: ResolvedConfigGraph, log: Log) {
export async function getTestStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getTests()

return fromPairs(
Expand All @@ -202,13 +234,24 @@ export async function getTestStatusPayloads(router: ActionRouter, graph: Resolve
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<RunStatusForEventPayload>
return [action.name, payload]
})
)
}

export async function getRunStatusPayloads(router: ActionRouter, graph: ResolvedConfigGraph, log: Log) {
export async function getRunStatusPayloads({
router,
graph,
log,
sessionId,
}: {
router: ActionRouter
graph: ResolvedConfigGraph
log: Log
sessionId: string
}) {
const actions = graph.getRuns()

return fromPairs(
Expand All @@ -223,10 +266,10 @@ export async function getRunStatusPayloads(router: ActionRouter, graph: Resolved
startedAt,
force: false,
action,
sessionId,
}) as ActionStatusPayload<RunStatusForEventPayload>

return [action.name, payload]
})
)
}

4 changes: 4 additions & 0 deletions core/src/events/action-status-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ interface ActionStatusPayloadBase {
startedAt: string
state: ActionStateForEvent
force: boolean
/**
* The session ID for the command run the action belongs to.
*/
sessionId: string
}

type ActionIncompleteState = PickFromUnion<ActionStateForEvent, "getting-status" | "unknown">
Expand Down
19 changes: 15 additions & 4 deletions core/src/events/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export function makeActionStatusPayloadBase({
force,
operation,
startedAt,
sessionId,
}: {
action: Action
force: boolean
operation: "getStatus" | "process"
startedAt: string
sessionId: string
}) {
return {
actionName: action.name,
Expand All @@ -74,6 +76,7 @@ export function makeActionStatusPayloadBase({
actionKind: action.kind.toLowerCase(),
actionUid: action.uid,
moduleName: action.moduleName(),
sessionId,
startedAt,
operation,
force,
Expand All @@ -90,12 +93,14 @@ export function makeActionGetStatusPayload({
action,
force,
startedAt,
sessionId,
}: {
action: Action
force: boolean
startedAt: string
sessionId: string
}) {
const payloadAttrs = makeActionStatusPayloadBase({ action, force, startedAt, operation: "getStatus" })
const payloadAttrs = makeActionStatusPayloadBase({ action, force, startedAt, sessionId, operation: "getStatus" })

const payload = {
...payloadAttrs,
Expand All @@ -115,12 +120,14 @@ export function makeActionProcessingPayload({
action,
force,
startedAt,
sessionId,
}: {
action: Action
force: boolean
startedAt: string
sessionId: string
}) {
const payloadAttrs = makeActionStatusPayloadBase({ action, force, startedAt, operation: "process" })
const payloadAttrs = makeActionStatusPayloadBase({ action, force, startedAt, sessionId, operation: "process" })
const actionKind = action.kind.toLowerCase() as Lowercase<Action["kind"]>

const payload = {
Expand Down Expand Up @@ -153,14 +160,16 @@ export function makeActionCompletePayload<
force,
operation,
startedAt,
sessionId,
}: {
result: R
action: Action
force: boolean
operation: "getStatus" | "process"
startedAt: string
sessionId: string
}) {
const payloadAttrs = makeActionStatusPayloadBase({ action, force, operation, startedAt })
const payloadAttrs = makeActionStatusPayloadBase({ action, force, operation, startedAt, sessionId })
const actionKind = action.kind.toLowerCase() as Lowercase<Action["kind"]>

// Map the result state to one of the allowed "complete" states.
Expand Down Expand Up @@ -231,13 +240,15 @@ export function makeActionFailedPayload({
force,
operation,
startedAt,
sessionId,
}: {
action: Action
force: boolean
operation: "getStatus" | "process"
startedAt: string
sessionId: string
}) {
const payloadAttrs = makeActionStatusPayloadBase({ action, force, operation, startedAt })
const payloadAttrs = makeActionStatusPayloadBase({ action, force, operation, startedAt, sessionId })

const payload = {
...payloadAttrs,
Expand Down
21 changes: 13 additions & 8 deletions core/src/server/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import { ActionStatusPayload } from "../events/action-status-events"
import { BuildStatusForEventPayload } from "../plugin/handlers/Build/get-status"
import { DeployStatusForEventPayload } from "../types/service"
import { RunStatusForEventPayload } from "../plugin/plugin"
import { getBuildStatusPayloads, getDeployStatusPayloads, getRunStatusPayloads, getTestStatusPayloads } from "../actions/helpers"
import {
getBuildStatusPayloads,
getDeployStatusPayloads,
getRunStatusPayloads,
getTestStatusPayloads,
} from "../actions/helpers"

export interface CommandMap {
[key: string]: {
Expand Down Expand Up @@ -225,7 +230,7 @@ export class _GetDeployStatusCommand extends ConsoleCommand {
const router = await garden.getActionRouter()
const graph = await garden.getResolvedConfigGraph({ log, emit: true })
const deployActions = graph.getDeploys({ includeDisabled: false }).sort((a, b) => (a.name > b.name ? 1 : -1))
const deployStatuses = await getDeployStatusPayloads(router, graph, log)
const deployStatuses = await getDeployStatusPayloads({ router, graph, log, sessionId: garden.sessionId })

const commandLog = log.createLog({ fixLevel: LogLevel.silly })
const syncStatuses = await getSyncStatuses({ garden, graph, deployActions, log: commandLog, skipDetail: true })
Expand All @@ -238,7 +243,7 @@ export class _GetDeployStatusCommand extends ConsoleCommand {
return acc
}, {} as { [key: string]: { deployStatus: ActionStatusPayload<DeployStatusForEventPayload>; syncStatus: GetSyncStatusResult } })

return { result: { actions} }
return { result: { actions } }
}
}

Expand All @@ -263,15 +268,15 @@ export class _GetActionStatusesCommand extends ConsoleCommand {
async action({ garden, log }: CommandParams): Promise<CommandResult<GetActionStatusesCommandResult>> {
const router = await garden.getActionRouter()
const graph = await garden.getResolvedConfigGraph({ log, emit: true })
const sessionId = garden.sessionId

const actions = await Bluebird.props({
build: getBuildStatusPayloads(router, graph, log),
deploy: getDeployStatusPayloads(router, graph, log),
test: getTestStatusPayloads(router, graph, log),
run: getRunStatusPayloads(router, graph, log),
build: getBuildStatusPayloads({ router, graph, log, sessionId }),
deploy: getDeployStatusPayloads({ router, graph, log, sessionId }),
test: getTestStatusPayloads({ router, graph, log, sessionId }),
run: getRunStatusPayloads({ router, graph, log, sessionId }),
})


return { result: { actions } }
}
}
Expand Down
21 changes: 10 additions & 11 deletions core/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,15 @@ export class GardenServer extends EventEmitter {
const isInternal = internal || skipLogsForCommands.includes(command.getFullName())
const requestLog = this.log.createLog({ name: "garden-server" })
const cmdNameStr = chalk.bold.white(command.getFullName())
const commandSessionId = requestId

if (skipAnalyticsForCommands.includes(command.getFullName())) {
command.enableAnalytics = false
}

const commandResponseBase = {
requestId,
sessionId: commandSessionId,
command: command.getFullName(),
persistent,
commandRequest: request.command,
Expand All @@ -577,7 +580,6 @@ export class GardenServer extends EventEmitter {
if (!isInternal) {
send("commandStart", {
...commandResponseBase,
requestId,
args,
opts,
})
Expand All @@ -588,7 +590,6 @@ export class GardenServer extends EventEmitter {
command.subscribe((data: any) => {
send("commandOutput", {
...commandResponseBase,
requestId,
data: sanitizeValue(data),
})
})
Expand All @@ -601,7 +602,7 @@ export class GardenServer extends EventEmitter {
return command.run({
...prepareParams,
garden,
sessionId: requestId,
sessionId: commandSessionId,
parentSessionId: this.sessionId,
overrideLogLevel: internal ? LogLevel.silly : undefined,
})
Expand Down Expand Up @@ -640,7 +641,6 @@ export class GardenServer extends EventEmitter {
"commandResult",
sanitizeValue({
...commandResponseBase,
requestId,
result,
errors,
})
Expand Down Expand Up @@ -766,6 +766,8 @@ export class GardenServer extends EventEmitter {
}

interface CommandResponseBase {
requestId: string
sessionId: string
command: string
/**
* The command string originally requested by the caller if applicable.
Expand All @@ -776,23 +778,20 @@ interface CommandResponseBase {

interface ServerWebsocketMessages {
commandOutput: CommandResponseBase & {
requestId: string
data: string
}
commandResult: CommandResponseBase & {
requestId: string
result: CommandResult<any>
errors?: GardenError[]
}
commandStatus: {
requestId: string
status: "active" | "not found"
}
commandStart: CommandResponseBase & {
requestId: string
args: object
opts: object
}
commandStatus: {
requestId: string
status: "active" | "not found"
}
error: {
requestId?: string
message: string
Expand Down
Loading

0 comments on commit 3c2022c

Please sign in to comment.