Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | import { ElMessage } from 'element-plus' import { onUnmounted, ref } from 'vue' import * as tasksApi from '../../api/tasks' import * as workflowsApi from '../../api/workflows' import { useExecutionStore } from '../../stores/executionStore' import { useWorkflowStore } from '../../stores/workflowStore' import { useWorkflowPersistence } from '../persistence/useWorkflowPersistence' import type { Task } from '@/types/generated/Task' import { ERROR_MESSAGES, SUCCESS_MESSAGES, POLLING_TIMING, INFO_MESSAGES } from '@/constants' interface MonitorExecutionOptions { label?: string startPolling?: boolean notifyQueued?: boolean queuedMessage?: string } const formatQueuedMessage = (label: string) => `${label} execution started` function createExecutionMonitor() { const executionStore = useExecutionStore() const isExecuting = ref(false) const executionId = ref<string | null>(null) const pollingInterval = ref<number | null>(null) const executionError = ref<string | null>(null) const executionLabel = ref<string>('Workflow') const stopPolling = () => { if (pollingInterval.value) { clearInterval(pollingInterval.value) pollingInterval.value = null } } const startPolling = () => { stopPolling() pollingInterval.value = window.setInterval(async () => { if (!executionId.value) { stopPolling() return } try { const tasks: Task[] = await tasksApi.getExecutionStatus(executionId.value) executionStore.updateFromTasks(tasks) const allCompleted = tasks.every( (task: Task) => task.status === 'Completed' || task.status === 'Failed' ) const hasFailed = tasks.some((task: Task) => task.status === 'Failed') if (allCompleted) { stopPolling() isExecuting.value = false if (hasFailed) { const failedTasks = tasks.filter((task: Task) => task.status === 'Failed') const errorMsg = failedTasks[0]?.error || 'Unknown error' ElMessage.error(`${executionLabel.value} execution failed: ${errorMsg}`) executionError.value = errorMsg } else { ElMessage.success(SUCCESS_MESSAGES.EXECUTED(executionLabel.value)) } executionStore.endExecution() } } catch (error) { console.error('Polling error:', error) } }, POLLING_TIMING.EXECUTION_STATUS) } const monitorExecution = (id: string, options: MonitorExecutionOptions = {}) => { const { label = 'Workflow', startPolling: shouldStartPolling = true, notifyQueued = true, queuedMessage, } = options stopPolling() executionId.value = id executionLabel.value = label executionError.value = null isExecuting.value = true executionStore.startExecution(id) if (shouldStartPolling) { startPolling() } if (notifyQueued) { const message = queuedMessage ?? formatQueuedMessage(label) ElMessage.success(message) } } const cancelExecution = () => { stopPolling() isExecuting.value = false executionId.value = null ElMessage.info(INFO_MESSAGES.EXECUTION_CANCELLED) } const clearExecutionResults = () => { stopPolling() executionId.value = null executionError.value = null isExecuting.value = false executionStore.clearExecution() } return { isExecuting, executionId, executionError, monitorExecution, cancelExecution, clearExecutionResults, startPolling, stopPolling, } } type ExecutionMonitor = ReturnType<typeof createExecutionMonitor> let monitor: ExecutionMonitor | null = null let subscribers = 0 export function useExecutionMonitor() { if (!monitor) { monitor = createExecutionMonitor() } subscribers += 1 onUnmounted(() => { subscribers = Math.max(0, subscribers - 1) if (subscribers === 0) { monitor?.stopPolling() } }) return { isExecuting: monitor.isExecuting, executionId: monitor.executionId, executionError: monitor.executionError, monitorExecution: monitor.monitorExecution, cancelExecution: monitor.cancelExecution, clearExecutionResults: monitor.clearExecutionResults, startPolling: monitor.startPolling, stopPolling: monitor.stopPolling, } } export function useAsyncWorkflowExecution() { const workflowStore = useWorkflowStore() const { saveWorkflow } = useWorkflowPersistence() const executionMonitor = useExecutionMonitor() const startAsyncExecution = async () => { if (!workflowStore.currentWorkflowId) { const saveResult = await saveWorkflow(workflowStore.nodes, workflowStore.edges, { showMessage: false, meta: { name: workflowStore.currentWorkflowName || 'Untitled Workflow' }, }) if (!saveResult.success) { ElMessage.error(ERROR_MESSAGES.FAILED_TO_SAVE('workflow')) return { success: false, error: ERROR_MESSAGES.FAILED_TO_SAVE('workflow') } } } if (executionMonitor.isExecuting.value) { ElMessage.warning(ERROR_MESSAGES.ALREADY_EXECUTING) return { success: false, error: ERROR_MESSAGES.ALREADY_EXECUTING } } executionMonitor.isExecuting.value = true executionMonitor.executionError.value = null try { const { execution_id } = await workflowsApi.submitWorkflow( workflowStore.currentWorkflowId! ) executionMonitor.monitorExecution(execution_id, { label: 'Workflow', }) return { success: true, executionId: execution_id } } catch (error) { const errorMessage = error instanceof Error ? error.message : ERROR_MESSAGES.WORKFLOW_EXECUTION_FAILED executionMonitor.executionError.value = errorMessage executionMonitor.isExecuting.value = false ElMessage.error(`Execution failed: ${errorMessage}`) return { success: false, error: errorMessage } } } return { isExecuting: executionMonitor.isExecuting, executionId: executionMonitor.executionId, executionError: executionMonitor.executionError, startAsyncExecution, cancelExecution: executionMonitor.cancelExecution, clearExecutionResults: executionMonitor.clearExecutionResults, monitorExecution: executionMonitor.monitorExecution, } } |