(params: AsyncExecutionParams)
| 258 | } |
| 259 | |
| 260 | async function handleAsyncExecution(params: AsyncExecutionParams): Promise<NextResponse> { |
| 261 | const { requestId, workflowId, userId, workspaceId, input, triggerType, executionId, callChain } = |
| 262 | params |
| 263 | const asyncLogger = logger.withMetadata({ |
| 264 | requestId, |
| 265 | workflowId, |
| 266 | workspaceId, |
| 267 | userId, |
| 268 | executionId, |
| 269 | }) |
| 270 | |
| 271 | const correlation = { |
| 272 | executionId, |
| 273 | requestId, |
| 274 | source: 'workflow' as const, |
| 275 | workflowId, |
| 276 | triggerType, |
| 277 | } |
| 278 | |
| 279 | const payload: WorkflowExecutionPayload = { |
| 280 | workflowId, |
| 281 | userId, |
| 282 | workspaceId, |
| 283 | input, |
| 284 | triggerType, |
| 285 | executionId, |
| 286 | requestId, |
| 287 | correlation, |
| 288 | callChain, |
| 289 | executionMode: 'async', |
| 290 | } |
| 291 | |
| 292 | try { |
| 293 | const jobQueue = await getJobQueue() |
| 294 | const jobId = await jobQueue.enqueue('workflow-execution', payload, { |
| 295 | metadata: { workflowId, workspaceId, userId, correlation }, |
| 296 | }) |
| 297 | |
| 298 | asyncLogger.info('Queued async workflow execution', { jobId }) |
| 299 | |
| 300 | if (shouldExecuteInline()) { |
| 301 | void (async () => { |
| 302 | try { |
| 303 | await jobQueue.startJob(jobId) |
| 304 | const output = await executeWorkflowJob(payload) |
| 305 | await jobQueue.completeJob(jobId, output) |
| 306 | } catch (error) { |
| 307 | const errorMessage = toError(error).message |
| 308 | asyncLogger.error('Async workflow execution failed', { |
| 309 | jobId, |
| 310 | error: errorMessage, |
| 311 | }) |
| 312 | // Release the reserved slot in case the job never reached |
| 313 | // executeWorkflowJob (e.g. startJob threw) and thus never finalized a |
| 314 | // LoggingSession to free it. Idempotent: a no-op when the job already |
| 315 | // finalized and released. |
| 316 | await releaseExecutionSlot(executionId) |
| 317 | try { |
no test coverage detected