(
runId: string,
temporalRunId?: string,
auth?: AuthContext | null,
)
| 1062 | } |
| 1063 | |
| 1064 | async getRunStatus( |
| 1065 | runId: string, |
| 1066 | temporalRunId?: string, |
| 1067 | auth?: AuthContext | null, |
| 1068 | ): Promise<WorkflowRunStatusPayload> { |
| 1069 | this.logger.log( |
| 1070 | `Fetching status for workflow run ${runId} (temporalRunId=${temporalRunId ?? 'latest'})`, |
| 1071 | ); |
| 1072 | const { organizationId, run } = await this.requireRunAccess(runId, auth); |
| 1073 | |
| 1074 | let temporalStatus: Awaited<ReturnType<typeof this.temporalService.describeWorkflow>>; |
| 1075 | let completedActions = 0; |
| 1076 | let failedActions = 0; |
| 1077 | let startedActions = 0; |
| 1078 | |
| 1079 | // Pre-fetch trace event counts for status inference |
| 1080 | if (run.totalActions && run.totalActions > 0) { |
| 1081 | [completedActions, failedActions, startedActions] = await Promise.all([ |
| 1082 | this.traceRepository.countByType(runId, 'NODE_COMPLETED', organizationId), |
| 1083 | this.traceRepository.countByType(runId, 'NODE_FAILED', organizationId), |
| 1084 | this.traceRepository.countByType(runId, 'NODE_STARTED', organizationId), |
| 1085 | ]); |
| 1086 | } |
| 1087 | |
| 1088 | try { |
| 1089 | temporalStatus = await this.temporalService.describeWorkflow({ |
| 1090 | workflowId: runId, |
| 1091 | runId: temporalRunId, |
| 1092 | }); |
| 1093 | } catch (error) { |
| 1094 | // If Temporal can't find the workflow, infer status from trace events |
| 1095 | if (this.isNotFoundError(error)) { |
| 1096 | const inferredStatus = this.inferStatusFromTraceEvents({ |
| 1097 | runId, |
| 1098 | totalActions: run.totalActions ?? 0, |
| 1099 | completedActions, |
| 1100 | failedActions, |
| 1101 | startedActions, |
| 1102 | }); |
| 1103 | |
| 1104 | this.logger.log( |
| 1105 | `Workflow ${runId} not found in Temporal, inferred status: ${inferredStatus} ` + |
| 1106 | `(started=${startedActions}, completed=${completedActions}, failed=${failedActions}, total=${run.totalActions})`, |
| 1107 | ); |
| 1108 | |
| 1109 | temporalStatus = { |
| 1110 | workflowId: runId, |
| 1111 | runId: temporalRunId ?? runId, |
| 1112 | // Cast to WorkflowExecutionStatusName - normalizeStatus handles mapping |
| 1113 | status: inferredStatus as unknown as typeof temporalStatus.status, |
| 1114 | startTime: run.createdAt.toISOString(), |
| 1115 | // Only set closeTime for terminal states that actually ran |
| 1116 | closeTime: ['COMPLETED', 'FAILED'].includes(inferredStatus) |
| 1117 | ? new Date().toISOString() |
| 1118 | : undefined, |
| 1119 | historyLength: 0, |
| 1120 | taskQueue: '', |
| 1121 | }; |
no test coverage detected