Run.startAsync()
.startAsync() 方法启动一个工作流运行,而无需等待其完成。它会立即返回 runId,允许工作流在后台执行。这对于长时间运行的工作流、计划任务,或者当你想避免在工作流完成时被阻塞的情况非常有用。
🌐 The .startAsync() method starts a workflow run without waiting for completion. It returns immediately with the runId, allowing the workflow to execute in the background. This is useful for long-running workflows, scheduled tasks, or when you want to avoid blocking on workflow completion.
使用示例Direct link to 使用示例
🌐 Usage example
const run = await workflow.createRun();
// Fire-and-forget - returns immediately
const { runId } = await run.startAsync({
inputData: {
value: "initial data",
},
});
// Optionally poll for completion later
const result = await workflow.getWorkflowRunExecutionResult(runId);
参数Direct link to 参数
🌐 Parameters
inputData?:
z.infer<TInput>
Input data that matches the workflow's input schema
requestContext?:
RequestContext
Request Context data to use during workflow execution
initialState?:
z.infer<TState>
Initial state to use for the workflow execution
tracingOptions?:
TracingOptions
Options for Tracing configuration.
metadata?:
Record<string, any>
Metadata to add to the root trace span. Useful for adding custom attributes like user IDs, session IDs, or feature flags.
traceId?:
string
Trace ID to use for this execution (1-32 hexadecimal characters). If provided, this trace will be part of the specified trace.
outputOptions?:
OutputOptions
Options for output configuration.
includeState?:
boolean
Whether to include the workflow run state in the result.
返回Direct link to 返回
🌐 Returns
runId:
string
The unique identifier for this workflow run. Use this to check status or retrieve results later.
何时使用 startAsync()Direct link to 何时使用 startAsync()
🌐 When to use startAsync()
在以下情况下使用 startAsync() 而不是 start():
🌐 Use startAsync() instead of start() when:
- 长时间运行的工作流:工作流可能需要几分钟或几个小时才能完成
- 定时/cron 触发器:你希望触发工作流而不阻塞调度器
- 避免轮询失败:在 Inngest 工作流中,
start()会轮询完成状态,这可能失败并导致重试。startAsync()可以避免这个问题 - 后台处理:你希望将工作排队并异步处理结果
检查工作流程状态Direct link to 检查工作流程状态
🌐 Checking workflow status
调用 startAsync() 后,你可以使用以下方法检查工作流状态:
🌐 After calling startAsync(), you can check the workflow status using:
// Get the execution result (including step outputs)
const result = await workflow.getWorkflowRunExecutionResult(runId);
if (result?.status === 'success') {
console.log('Workflow completed:', result.steps);
} else if (result?.status === 'failed') {
console.log('Workflow failed:', result.error);
} else if (result?.status === 'running') {
console.log('Workflow still running...');
}
相关Direct link to 相关
🌐 Related
- Run.start() - 启动工作流并等待完成
- 工作流程概览
- Workflow.createRun()