(input: StartWorkflowInput)
| 489 | } |
| 490 | |
| 491 | async startWorkflow(input: StartWorkflowInput): Promise<StartNamedWorkflowResult> { |
| 492 | const createdRun = await this.createWorkflowRun(input); |
| 493 | const runId = createdRun.id; |
| 494 | await this.notifyRunStatusChanged(createdRun); |
| 495 | await input.onRunCreated?.({ runId, status: "pending", result: null, run: createdRun }); |
| 496 | if (isAbortSignalAborted(input.abortSignal)) { |
| 497 | await this.interruptRun({ workspaceId: input.workspaceId, runId }); |
| 498 | throw new Error(`Workflow run interrupted: ${runId}`); |
| 499 | } |
| 500 | |
| 501 | const runnerAbortController = new AbortController(); |
| 502 | let unregisterRunnerAbort: () => void = () => undefined; |
| 503 | const abortInterrupt = this.interruptRunOnAbort( |
| 504 | input.workspaceId, |
| 505 | runId, |
| 506 | input.abortSignal, |
| 507 | runnerAbortController |
| 508 | ); |
| 509 | try { |
| 510 | const runner = await this.createRunner(runId); |
| 511 | const result = await runner.run(runId, { |
| 512 | abortSignal: runnerAbortController.signal, |
| 513 | ...(input.backgroundOnMessageQueued !== undefined |
| 514 | ? { backgroundOnMessageQueued: input.backgroundOnMessageQueued } |
| 515 | : {}), |
| 516 | onLeaseAcquired: () => { |
| 517 | unregisterRunnerAbort = this.registerActiveRunnerAbortController( |
| 518 | runId, |
| 519 | runnerAbortController |
| 520 | ); |
| 521 | }, |
| 522 | }); |
| 523 | await this.notifyRunStatusChanged(createdRun, "completed"); |
| 524 | return { runId, status: "completed", result }; |
| 525 | } catch (error) { |
| 526 | if (error instanceof WorkflowRunBackgroundedError) { |
| 527 | await this.runStore.setAttentionPolicy(runId, "notify_on_terminal"); |
| 528 | await this.notifyRunStatusChanged(createdRun, "backgrounded"); |
| 529 | if (!runnerAbortController.signal.aborted) { |
| 530 | void this.runInBackground(runId, "Backgrounded workflow run failed:", { |
| 531 | projectTrusted: input.projectTrusted, |
| 532 | }).catch(() => undefined); |
| 533 | } |
| 534 | return { runId, status: "backgrounded", result: null }; |
| 535 | } |
| 536 | await this.notifyLatestRunStatus(runId); |
| 537 | throw error; |
| 538 | } finally { |
| 539 | abortInterrupt.remove(); |
| 540 | await abortInterrupt.wait(); |
| 541 | unregisterRunnerAbort(); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | private async resumeCrashRecoveredRun(input: { |
| 546 | runId: string; |
no test coverage detected