😀

Azure Durable Functions のタスクハブ名を環境変数から設定してみた

に公開

Azure Durable Functions のタスクハブ名を使ってステート管理をする仕組み上、複数の Azure Functions インスタンス、例えば検証環境とローカルの開発環境が同じストレージアカウントを使用して、かつ同じタスクハブ名を使用すると正常に動作しなくなりました。そこで Azure Functions インスタンスごとに個別のタスクハブ名を使用してステート管理できないかと考え、環境変数からタスクハブ名を設定できるか試してみました。

ストレージアカウントの代わりに Azurite を動かす

zsh
% azurite -s -l ~/.azurite &
[1] 27582

Azurite Blob service is starting at http://127.0.0.1:10000
Azurite Blob service is successfully listening at http://127.0.0.1:10000
Azurite Queue service is starting at http://127.0.0.1:10001
Azurite Queue service is successfully listening at http://127.0.0.1:10001
Azurite Table service is starting at http://127.0.0.1:10002
Azurite Table service is successfully listening at http://127.0.0.1:10002

検証用 Durable Functions を作成

zsh
% func init mnrdf --dotnet

% cd mnrdf

% func new --name DurableExample --template DurableFunctionsOrchestration

Durable Functions のデフォルトのタスクハブ名を確認

zsh
% func start

% curl -s http://localhost:7071/api/DurableExample_HttpStart | jq .
{
  "id": "8ce705c9ac31467aaf60d84eb4e20fbd",
  "statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd?taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd/raiseEvent/{eventName}?taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd/terminate?reason={text}&taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd?taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "restartPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd/restart?taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "suspendPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd/suspend?reason={text}&taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "resumePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/8ce705c9ac31467aaf60d84eb4e20fbd/resume?reason={text}&taskHub=TestHubName&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w=="
}

タスクハブ名は、taskHub=TestHubName が出力されているので、TestHubName です。

host.json にタスクハブ名の設定を入れる

host.json
{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            },
            "enableLiveMetricsFilters": true
        }
    },
    "extensions": {
      "durableTask": {
        "hubName": "%MyTaskHub%"
      }
    }
}

%MyTaskHub%% で挟んでいる文字列が環境変数名として認識するようです。

環境変数を設定して動作確認

zsh
% export MyTaskHub=DurableExample

% func start

% curl -s http://localhost:7071/api/DurableExample_HttpStart | jq .
{
  "id": "aa7e1e292a534f359cbde0c2daf3f427",
  "statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427?taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "sendEventPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427/raiseEvent/{eventName}?taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "terminatePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427/terminate?reason={text}&taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "purgeHistoryDeleteUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427?taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "restartPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427/restart?taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "suspendPostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427/suspend?reason={text}&taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w==",
  "resumePostUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/aa7e1e292a534f359cbde0c2daf3f427/resume?reason={text}&taskHub=DurableExample&connection=Storage&code=7dhvoPWrEtztik5gmkogtHDluqNcuEW1cy0qS9NDJdlrAzFu4M287w=="
}

taskHub=DurableExample と表示されるので、環境変数 MyTaskHub に設定した値 DurableExample をタスクハブ名として設定することができました。

後片付け

zsh
% fg
[1]  + running    azurite -s -l ~/.azurite

% rm -rf ~/.azurite

参考

https://learn.microsoft.com/ja-jp/azure/azure-functions/durable/durable-functions-task-hubs?tabs=csharp#hostjson-functions-20-1

https://learn.microsoft.com/ja-jp/azure/storage/common/storage-use-azurite?tabs=visual-studio%2Cblob-storage

Discussion