Last active
April 19, 2019 12:42
Star
You must be signed in to star a gist
多分わかりやすいDurable Functions【Durable Functionsの簡単なサンプルコード】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Extensions.Logging; | |
namespace FunctionChainingByDurableFunctions | |
{ | |
public static class Function1 | |
{ | |
// クライアント関数 | |
// オーケストレータ関数を起動します。これは単なるHTTPトリガーのAzure Functionsです。 | |
// キュートリガーでもなんでも構いません。大切なのはこのメソッドの中のStartNewAsyncです。 | |
// これでDurable Functionsのキモであるオーケストレーター関数を呼び出します。 | |
// なので、クライアント関数のトリガーは、StartNewAsyncでオーケストレーター関数を起動すれば、 | |
// HTTPでもキューでもBlobでもなんでもOKです。 | |
// また、クライアント関数の引数には、OrchestrationClient属性を付与したDurableOrchestrationClient型の | |
// 引数を必ず定義しなければなりません。 | |
[FunctionName("OrchestrationClient")] | |
public static async Task<HttpResponseMessage> HttpStart( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get")]HttpRequestMessage req, | |
[OrchestrationClient]DurableOrchestrationClient starter, | |
ILogger log) | |
{ | |
// クエリパラメーターnumに指定された数字を解析して取得する。 | |
string queryString = req.RequestUri.Query; | |
NameValueCollection query = HttpUtility.ParseQueryString(queryString, Encoding.UTF8); | |
string num = query["num"]; | |
// オーケストレータ関数を起動する。 | |
// 第1引数には、別途定義したオーケストレーター関数のFunctionName属性で定義した関数名、 | |
// 第2引数には、オーケストレーター関数に渡したいパラメーターを渡す。 | |
string instanceId = await starter.StartNewAsync("Orchestrator", num); | |
return starter.CreateCheckStatusResponse(req, instanceId); | |
} | |
// オーケストレータ関数 | |
// FunctionName属性で定義した関数名には、クライアント関数で記述した | |
// オーケストレーター関数を起動する関数であるStartNewAsync関数の第1引数と | |
// 同じものを指定する。 | |
// オーケストレーター関数の引数には、必ずOrchestrationTrigger属性を付与する。 | |
// 引数のDurableOrchestrationContext型のcontextには実行したDurable Functionsの | |
// インスタンスに関する様々な情報が入っている。例えば、クライアント関数のStartNewAsyncメソッドで | |
// 渡した引数などである。 | |
[FunctionName("Orchestrator")] | |
public static async Task RunOrchestrator( | |
[OrchestrationTrigger] DurableOrchestrationContext context, ILogger log) | |
{ | |
// クライアント関数から渡されたパラメーター(クエリパラメーターnumに指定された数字)を取得する。 | |
int num = context.GetInput<int>(); | |
// 引数に渡した数字に2を足すアクティビティ関数を実行する。 | |
// CallActivityAsyncはアクティビティ関数を起動するためのメソッドです。 | |
// 第1引数には、別途定義したアクティビティ関数のFunctionName属性で定義した関数名、 | |
// 第2引数には、アクティビティ関数に渡したい引数を指定する。 | |
int result1 = await context.CallActivityAsync<int>("Plus2", num); | |
// 引数に渡した数字に2をかけるアクティビティ関数を実行する。 | |
// CallActivityAsyncはアクティビティ関数を起動するためのメソッドです。 | |
// 第1引数には、別途定義したアクティビティ関数のFunctionName属性で定義した関数名、 | |
// 第2引数には、アクティビティ関数に渡したい引数を指定する。 | |
int result2 = await context.CallActivityAsync<int>("MultiplyBy2", result1); | |
// 結果をログに出力する。 | |
log.LogInformation("result:" + result2); | |
} | |
// 引数に渡した数字に2を足すアクティビティ関数 | |
// アクティビティ関数の引数には必ずActivityTrigger属性を定義する。 | |
[FunctionName("Plus2")] | |
public static int Plus2([ActivityTrigger] int num, ILogger log) | |
{ | |
return num + 2; | |
} | |
// 引数に渡した数字に2をかけるアクティビティ関数 | |
// アクティビティ関数の引数には必ずActivityTrigger属性を定義する。 | |
[FunctionName("MultiplyBy2")] | |
public static int MultiplyBy2([ActivityTrigger] int num, ILogger log) | |
{ | |
return num * 2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment