node.jsでのアクティビティ関数の動作メモ

# 概要
[Azure Durable Functionsのアクティビティ関数が同期的な関数のときの動作メモ](https://qiita.com/hibohiboo/items/ac21552c881755d0f73c)をTypescriptで実装して確認した。

結果、挙動は変わらないことが分かった。

[ソースコード:確認時](https://github.com/hibohiboo/async-ttrpg/commit/2e645e5f06c7662552723a4b982d0ea479c699de)


確認

```
get http://127.0.0.1:7071/orchestrators/durableOrchestrator
```


<details><div>

```
[2024-11-17T15:57:51.700Z] Executing 'Functions.durableHttpStart' (Reason='This function was programmatically called via the host APIs.', Id=382780ba-b4c1-4aa7-8284-4844b248f315)
[2024-11-17T15:57:51.827Z] Started orchestration with ID = '043831888fdc42489f67afd4a104540c'.
[2024-11-17T15:57:51.843Z] Executed 'Functions.durableHttpStart' (Succeeded, Id=382780ba-b4c1-4aa7-8284-4844b248f315, Duration=154ms)
[2024-11-17T15:57:51.858Z] Executing 'Functions.durableOrchestrator' (Reason='(null)', Id=e793d280-3f81-4ad8-bcff-8cdddb2dc4fe)
[2024-11-17T15:57:51.883Z] Executed 'Functions.durableOrchestrator' (Succeeded, Id=e793d280-3f81-4ad8-bcff-8cdddb2dc4fe, Duration=28ms)
[2024-11-17T15:57:51.903Z] Executing 'Functions.WaitTest' (Reason='(null)', Id=9d16c89c-eb32-41a8-8e52-5d4c722fbf94)
[2024-11-17T15:57:51.906Z] Wait start 0.
[2024-11-17T15:57:52.907Z] Wait end 0.
[2024-11-17T15:57:52.908Z] Executed 'Functions.WaitTest' (Succeeded, Id=9d16c89c-eb32-41a8-8e52-5d4c722fbf94, Duration=1005ms)
[2024-11-17T15:57:52.921Z] Executing 'Functions.durableOrchestrator' (Reason='(null)', Id=0a550db6-a0dd-4b01-85a9-f89b9e524ccf)
[2024-11-17T15:57:52.926Z] Executed 'Functions.durableOrchestrator' (Succeeded, Id=0a550db6-a0dd-4b01-85a9-f89b9e524ccf, Duration=5ms)
[2024-11-17T15:57:52.931Z] Executing 'Functions.WaitTest' (Reason='(null)', Id=06be9c73-8ebe-4e2d-adc4-ce3c46a7e43f)
[2024-11-17T15:57:52.932Z] Wait start 1.
[2024-11-17T15:57:53.936Z] Wait end 1.
[2024-11-17T15:57:53.936Z] Executed 'Functions.WaitTest' (Succeeded, Id=06be9c73-8ebe-4e2d-adc4-ce3c46a7e43f, Duration=1005ms)
[2024-11-17T15:57:53.942Z] Executing 'Functions.durableOrchestrator' (Reason='(null)', Id=59d1c174-9161-496b-9a4d-b0cceace2688)
[2024-11-17T15:57:53.948Z] Executed 'Functions.durableOrchestrator' (Succeeded, Id=59d1c174-9161-496b-9a4d-b0cceace2688, Duration=5ms)
[2024-11-17T15:57:53.953Z] Executing 'Functions.WaitTest' (Reason='(null)', Id=ea41cebf-e115-4465-b37b-30a41da9bca5)
[2024-11-17T15:57:53.954Z] Wait start 2.
[2024-11-17T15:57:54.216Z] Host lock lease acquired by instance ID '00000000000000000000000095FDE2DF'.
[2024-11-17T15:57:54.961Z] Wait end 2.
[2024-11-17T15:57:54.962Z] Executed 'Functions.WaitTest' (Succeeded, Id=ea41cebf-e115-4465-b37b-30a41da9bca5, Duration=1009ms)
[2024-11-17T15:57:54.968Z] Executing 'Functions.durableOrchestrator' (Reason='(null)', Id=1ffc5937-e131-4a43-8375-f47a447f4237)
[2024-11-17T15:57:54.970Z] Executed 'Functions.durableOrchestrator' (Succeeded, Id=1ffc5937-e131-4a43-8375-f47a447f4237, Duration=2ms)
[2024-11-17T15:57:54.974Z] Executing 'Functions.WaitTest' (Reason='(null)', Id=3a075e50-0607-4027-886f-8ff44ce3624f)
[2024-11-17T15:57:54.976Z] Wait start 3.
[2024-11-17T15:57:54.984Z] Executed 'Functions.WaitTest' (Failed, Id=3a075e50-0607-4027-886f-8ff44ce3624f, Duration=9ms)
[2024-11-17T15:57:54.985Z] System.Private.CoreLib: Exception while executing function: Functions.WaitTest. System.Private.CoreLib: Result: Failure
Exception: wait test 3
Stack: Error: wait test 3
    at handler (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:429:27006)
    at ic.<anonymous> (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:101768)
    at Generator.next (<anonymous>)
    at D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:98676
    at new Promise (<anonymous>)
    at o (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:98439)
    at ic.invokeFunction (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:101702)
    at y.<anonymous> (C:\ProgramData\chocolatey\lib\azure-functions-core-tools\tools\workers\node\dist\src\worker-bundle.js:2:39015)
    at Generator.next (<anonymous>)
    at o (C:\ProgramData\chocolatey\lib\azure-functions-core-tools\tools\workers\node\dist\src\worker-bundle.js:2:36104).
[2024-11-17T15:57:54.991Z] 043831888fdc42489f67afd4a104540c: Function 'WaitTest (Activity)' failed with an error. Reason: System.Exception:  wait test 3
[2024-11-17T15:57:54.992Z]  ---> Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException: Result: Failure
Exception: wait test 3
Stack: Error: wait test 3
    at handler (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:429:27006)
    at ic.<anonymous> (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:101768)
    at Generator.next (<anonymous>)
    at D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:98676
    at new Promise (<anonymous>)
    at o (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:98439)
    at ic.invokeFunction (D:\projects\async-ttrpg\apps\api\dist\src\functions\index.js:53:101702)
    at y.<anonymous> (C:\ProgramData\chocolatey\lib\azure-functions-core-tools\tools\workers\node\dist\src\worker-bundle.js:2:39015)
    at Generator.next (<anonymous>)
    at o (C:\ProgramData\chocolatey\lib\azure-functions-core-tools\tools\workers\node\dist\src\worker-bundle.js:2:36104)
[2024-11-17T15:57:54.993Z]    at Microsoft.Azure.WebJobs.Script.Description.WorkerFunctionInvoker.InvokeCore(Object[] parameters, FunctionInvocationContext context) in /_/src/WebJobs.Script/Description/Workers/WorkerFunctionInvoker.cs:line 101
[2024-11-17T15:57:54.993Z]    at Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters) in /_/src/WebJobs.Script/Description/FunctionInvokerBase.cs:line 82
[2024-11-17T15:57:54.994Z]    at Microsoft.Azure.WebJobs.Script.Description.FunctionGenerator.Coerce[T](Task`1 src) in /_/src/WebJobs.Script/Description/FunctionGenerator.cs:line 225      
[2024-11-17T15:57:54.994Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 53
[2024-11-17T15:57:54.995Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeWithTimeoutAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean throwOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 581
[2024-11-17T15:57:54.995Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 527
[2024-11-17T15:57:54.996Z]    at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken cancellationToken) in D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 306
[2024-11-17T15:57:54.996Z]    --- End of inner exception stack trace ---. IsReplay: False. State: Failed. RuntimeStatus: Failed. HubName: TestHubName. AppName: . SlotName: . ExtensionVersion: 2.13.2. SequenceNumber: 20. TaskEventId: 3
[2024-11-17T15:57:55.009Z] Executing 'Functions.durableOrchestrator' (Reason='(null)', Id=b5802749-a576-49c3-b7db-917c3da968d1)
[2024-11-17T15:57:55.011Z] Error in orchestration: 3
[2024-11-17T15:57:55.017Z] Executed 'Functions.durableOrchestrator' (Succeeded, Id=b5802749-a576-49c3-b7db-917c3da968d1, Duration=7ms)
[2024-11-17T15:57:55.021Z] Executing 'Functions.WaitTest' (Reason='(null)', Id=f67cb13b-4e05-4f13-8f57-c53be78d9727)
[2024-11-17T15:57:55.023Z] Wait start 4.
[2024-11-17T15:57:56.034Z] Wait end 4.
[2024-11-17T15:57:56.035Z] Executed 'Functions.WaitTest' (Succeeded, Id=f67cb13b-4e05-4f13-8f57-c53be78d9727, Duration=1013ms)
[2024-11-17T15:57:56.041Z] Executing 'Functions.durableOrchestrator' (Reason='(null)', Id=6c42a518-4160-4d3d-aa63-219d96d23c63)
[2024-11-17T15:57:56.043Z] Error in orchestration: 3
[2024-11-17T15:57:56.046Z] Executed 'Functions.durableOrchestrator' (Succeeded, Id=6c42a518-4160-4d3d-aa63-219d96d23c63, Duration=4ms)
```

</div></details>

# 再試行の動作を試してみる

```
get http://127.0.0.1:7071/orchestrators/durableOrchestratorActivityErrorHandling
```

実行ログ（抜粋)

```diff
- [2024-11-17T23:46:34.374Z] Executing 'Functions.durableHttpStart' (Reason='This function was programmatically called via the host APIs.', Id=cddae0b9-a33b-42d1-b752-6851048bd431)
- [2024-11-17T23:46:34.498Z] Started orchestration with ID = '1bb92721b1df4531a956876a8ab88ed2'.
- [2024-11-17T23:46:34.513Z] Executed 'Functions.durableHttpStart' (Succeeded, Id=cddae0b9-a33b-42d1-b752-6851048bd431, Duration=150ms)
- [2024-11-17T23:46:34.527Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=9a873683-6646-4985-a89d-493351dcbf5c)
- [2024-11-17T23:46:34.555Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Succeeded, Id=9a873683-6646-4985-a89d-493351dcbf5c, Duration=31ms)
+ [2024-11-17T23:46:34.571Z] Executing 'Functions.flakyFunction' (Reason='(null)', Id=4c4dab08-1bde-4f1f-8048-5ae75a052565)
- [2024-11-17T23:46:35.657Z] System.Private.CoreLib: Exception while executing function: Functions.flakyFunction. System.Private.CoreLib: Result: Failure Exception: error test
- [2024-11-17T23:46:35.658Z] 1bb92721b1df4531a956876a8ab88ed2: Function 'flakyFunction (Activity)' failed with an error. Reason: System.Exception:  error test
- [2024-11-17T23:46:35.670Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=c74a6b1d-ec9b-45b9-9246-453b4885671b)
- [2024-11-17T23:46:35.674Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Succeeded, Id=c74a6b1d-ec9b-45b9-9246-453b4885671b, Duration=3ms)
- [2024-11-17T23:46:39.014Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=01a3ea1b-1012-4e2b-8c9d-f0f2a11e94f4)
- [2024-11-17T23:46:39.017Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Succeeded, Id=01a3ea1b-1012-4e2b-8c9d-f0f2a11e94f4, Duration=3ms)
+ [2024-11-17T23:46:39.021Z] Executing 'Functions.flakyFunction' (Reason='(null)', Id=c6ed6227-7778-4e10-aa50-85ed270bd8ff)
- [2024-11-17T23:46:35.657Z] Executed 'Functions.flakyFunction' (Failed, Id=8afe1209-5619-478c-b476-1ac3d32c47af, Duration=3ms)
- [2024-11-17T23:46:35.657Z] System.Private.CoreLib: Exception while executing function: Functions.flakyFunction. System.Private.CoreLib: Result: Failure
- [2024-11-17T23:46:35.658Z] 1bb92721b1df4531a956876a8ab88ed2: Function 'flakyFunction (Activity)' failed with an error. Reason: System.Exception:  error test
- [2024-11-17T23:46:35.670Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=c74a6b1d-ec9b-45b9-9246-453b4885671b)
- [2024-11-17T23:46:35.674Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Succeeded, Id=c74a6b1d-ec9b-45b9-9246-453b4885671b, Duration=3ms)
- [2024-11-17T23:46:39.014Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=01a3ea1b-1012-4e2b-8c9d-f0f2a11e94f4)
- [2024-11-17T23:46:39.017Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Succeeded, Id=01a3ea1b-1012-4e2b-8c9d-f0f2a11e94f4, Duration=3ms)
+ [2024-11-17T23:46:39.021Z] Executing 'Functions.flakyFunction' (Reason='(null)', Id=c6ed6227-7778-4e10-aa50-85ed270bd8ff)
- [2024-11-17T23:46:39.028Z] Executed 'Functions.flakyFunction' (Failed, Id=c6ed6227-7778-4e10-aa50-85ed270bd8ff, Duration=3ms)
- [2024-11-17T23:46:39.028Z] System.Private.CoreLib: Exception while executing function: Functions.flakyFunction. System.Private.CoreLib: Result: Failure
- [2024-11-17T23:46:39.042Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=0b2fe2bc-c111-4777-aaca-f4e8e0e0360d)
- [2024-11-17T23:46:39.044Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Succeeded, Id=0b2fe2bc-c111-4777-aaca-f4e8e0e0360d, Duration=2ms)
- [2024-11-17T23:46:39.051Z] Executing 'Functions.durableOrchestratorActivityErrorHandling' (Reason='(null)', Id=81b311e1-a52b-4689-93f5-ac3dee504969)
- [2024-11-17T23:46:39.062Z] Executed 'Functions.durableOrchestratorActivityErrorHandling' (Failed, Id=81b311e1-a52b-4689-93f5-ac3dee504969, Duration=10ms)

```

最大試行回数より前に３回のリトライに失敗して終わっていることを確認できた。
[ソースコード:確認時](https://github.com/hibohiboo/async-ttrpg/commit/a53453d310366753736cd2e191a98df0a2105c45)

# 参考

[オーケストラレータの制約](https://learn.microsoft.com/ja-jp/azure/azure-functions/durable/durable-functions-code-constraints?tabs=javascript)
[Durable Functionsのリトライ機能について](https://tech-lab.sios.jp/archives/27415)