> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Run.cancel() `.cancel()` メソッドは Workflow Run をキャンセルし、実行を停止してリソースをクリーンアップします。 このメソッドは実行中のすべてのステップを中止し、Workflow のステータスを 'canceled' に更新します。現在実行中の Workflow と、中断中または待機中の Workflow のどちらにも使用できます。 ## 使用例 ```typescript const run = await workflow.createRun() await run.cancel() // Returns: { message: 'Workflow run canceled' } ``` ## パラメーター **No parameters** (`void`): このメソッドはパラメーターを受け取りません ## 戻り値 **result** (`Promise<{ message: string }>`): キャンセルに成功すると { message: 'Workflow run canceled' } で解決される Promise ## キャンセルの仕組み 呼び出されると、Workflow は次の処理を行います。 1. **abort signal をトリガー** - 標準の Web API AbortSignal を使用して実行中のステップに通知します 2. **後続ステップの実行を防止** - 以降のステップは実行されません ## Abort signal の動作 `abortSignal` パラメーターを確認するステップは、キャンセルに応答できます。 - ステップは 'abort' イベントをリッスンできます: `abortSignal.addEventListener('abort', callback)` - ステップはすでに中止されているか確認できます: `if (abortSignal.aborted) { ... }` - タイムアウト、ネットワークリクエスト、長時間実行される処理のキャンセルに役立ちます 実行途中でキャンセルするには、ステップが abort signal を能動的に確認する必要があります。確認しない場合、現在のステップは完了まで実行されますが、後続のステップは実行されません。 ## その他の使用例 ### エラー発生時に Workflow をキャンセルする ```typescript const run = await workflow.createRun() try { const result = await run.start({ inputData: { value: 'initial data' } }) } catch (error) { await run.cancel() } ``` ### キャンセルに応答するステップを作成する ```typescript const step = createStep({ id: 'long-running-step', execute: async ({ inputData, abortSignal, abort }) => { const timeout = new Promise(resolve => { const timer = setTimeout(() => resolve('done'), 10000) // Clean up if canceled abortSignal.addEventListener('abort', () => { clearTimeout(timer) resolve('canceled') }) }) const result = await timeout // Check if aborted after async operation if (abortSignal.aborted) { return abort() // Stop execution } return { result } }, }) ``` ## 関連項目 - [Workflow の概要](https://mastra.zisheng.pro/ja/docs/workflows/overview) - [Workflow.createRun()](https://mastra.zisheng.pro/ja/reference/workflows/workflow-methods/create-run)