public static async Task<List<string>> Run( [OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log ) { log.Info("ParallelExec started!"); var tasks = new Task<string>[3]; tasks[0] = context.CallFunctionAsync<string>( "EchoExec", "Hello, I'm Tsuyoshi" ); tasks[1] = context.CallFunctionAsync<string>( "EchoExec", "Hello, I'm Kanio"); tasks[2] = context.CallFunctionAsync<string>( "EchoExec", "Hello, I'm NEO"); await Task.WhenAll(tasks); var outputs = new List<string>(); foreach(Task<string> task in tasks) { outputs.Add(task.Result); } log.Info("ParallelExec Done!"); return outputs; }
public static async Task <long> Run( [OrchestrationTrigger] DurableOrchestrationContext backupContext) { string rootDirectory = backupContext.GetInput <string>(); if (string.IsNullOrEmpty(rootDirectory)) { throw new ArgumentNullException(nameof(rootDirectory), "A root directory must be specified"); } string[] files = await backupContext.CallFunctionAsync <string[]>( "E2_GetFileList", rootDirectory); var tasks = new Task <long> [files.Length]; for (int i = 0; i < files.Length; i++) { tasks[i] = backupContext.CallFunctionAsync <long>( "E2_CopyFileToBlob", files[i]); } await Task.WhenAll(tasks); long totalBytes = tasks.Sum(t => t.Result); return(totalBytes); }
public static async Task <List <BidInfo> > Run( [OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log) { var product = context.GetInput <string>(); var outputs = new List <BidInfo>(); log.Info("calling SaveAsk"); var askInfo = await context.CallFunctionAsync <AskInfo>("SaveAsk", product); log.Info($"AskInfo - Id: {askInfo.AskId}"); using (var timeoutCts = new CancellationTokenSource()) { DateTime expiration = context.CurrentUtcDateTime.AddSeconds(10); await context.CreateTimer(expiration, timeoutCts.Token); log.Info("delay done"); var highestBid = await context.CallFunctionAsync <BidInfo>("GetHighestBid", askInfo.AskId); log.Info($"highestBid - Id: {highestBid.BidId} AskInfo.Id: {askInfo?.AskId}"); outputs.Add(highestBid); // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"] return(outputs); } }
// TODO: It's not currently possible to detect this failure except by examining logs. public static async Task IllegalAwait([OrchestrationTrigger] DurableOrchestrationContext ctx) { await ctx.CallFunctionAsync(nameof(Activities.Hello), "Foo"); // This is the illegal await await Task.Run(() => { }); // This call should throw await ctx.CallFunctionAsync(nameof(Activities.Hello), "Bar"); }
public static async Task <List <string> > Run( [OrchestrationTrigger] DurableOrchestrationContext context) { var outputs = new List <string>(); outputs.Add(await context.CallFunctionAsync <string>("E1_SayHello", "Tokyo")); outputs.Add(await context.CallFunctionAsync <string>("E1_SayHello", "Seattle")); outputs.Add(await context.CallFunctionAsync <string>("E1_SayHello", "London")); // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"] return(outputs); }
public static async Task <List <string> > HelloWorld( [OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log) { var outputs = new List <string>(); outputs.Add(await context.CallFunctionAsync <string>("Say", "Hello")); outputs.Add(await context.CallFunctionAsync <string>("Say", " ")); outputs.Add(await context.CallFunctionAsync <string>("Say", "World!")); log.Info(string.Concat(outputs)); // returns ["Hello", "World"] return(outputs); }
public static async Task SayHelloWithActivity([OrchestrationTrigger] DurableOrchestrationContext ctx) { string input = ctx.GetInput <string>(); string output = await ctx.CallFunctionAsync <string>(nameof(Activities.Hello), input); ctx.SetOutput(output); }
public static async Task <string> Run( [OrchestrationTrigger] DurableOrchestrationContext context) { var request = context.GetInput <TranslatorRequest>(); var output = await context.CallFunctionAsync <string>("DocumentTranslator", request); return(output); }
public static async Task DiskUsage([OrchestrationTrigger] DurableOrchestrationContext ctx) { string directory = ctx.GetInput <string>(); string[] files = await ctx.CallFunctionAsync <string[]>(nameof(Activities.GetFileList), directory); var tasks = new Task <long> [files.Length]; for (int i = 0; i < files.Length; i++) { tasks[i] = ctx.CallFunctionAsync <long>(nameof(Activities.GetFileSize), files[i]); } await Task.WhenAll(tasks); long totalBytes = tasks.Sum(t => t.Result); ctx.SetOutput(totalBytes); }
public static async Task <bool> Run( [OrchestrationTrigger] DurableOrchestrationContext context) { string phoneNumber = context.GetInput <string>(); if (string.IsNullOrEmpty(phoneNumber)) { throw new ArgumentNullException( nameof(phoneNumber), "A phone number input is required."); } int challengeCode = await context.CallFunctionAsync <int>( "E4_SendSmsChallenge", phoneNumber); using (var timeoutCts = new CancellationTokenSource()) { // The user has 90 seconds to respond with the code they received in the SMS message. DateTime expiration = context.CurrentUtcDateTime.AddSeconds(90); Task timeoutTask = context.CreateTimer(expiration, timeoutCts.Token); bool authorized = false; for (int retryCount = 0; retryCount <= 3; retryCount++) { Task <int> challengeResponseTask = context.WaitForExternalEvent <int>("SmsChallengeResponse"); Task winner = await Task.WhenAny(challengeResponseTask, timeoutTask); if (winner == challengeResponseTask) { // We got back a response! Compare it to the challenge code. if (challengeResponseTask.Result == challengeCode) { authorized = true; break; } } else { // Timeout expired break; } } if (!timeoutTask.IsCompleted) { // All pending timers must be complete or canceled before the function exits. timeoutCts.Cancel(); } return(authorized); } }
public static async Task Throw([OrchestrationTrigger] DurableOrchestrationContext ctx) { string message = ctx.GetInput <string>(); if (string.IsNullOrEmpty(message)) { // This throw happens directly in the orchestration. throw new ArgumentNullException(nameof(message)); } // This throw happens in the implementation of an activity. await ctx.CallFunctionAsync(nameof(Activities.Throw), message); }
public static async Task Factorial([OrchestrationTrigger] DurableOrchestrationContext ctx) { int n = ctx.GetInput <int>(); long result = 1; for (int i = 1; i <= n; i++) { result = await ctx.CallFunctionAsync <int>(nameof(Activities.Multiply), new[] { result, i }); } ctx.SetOutput(result); }
public static async Task <ScalingState> ScalingLogic([OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log) { var state = context.GetInput <ScalingState>(); log.Info($"Current state is {state}. Waiting for next value."); var metric = await context.WaitForExternalEvent <Metric>(nameof(Metric)); var history = state.History; log.Info($"Scaling logic: Received {metric.Name}, previous state is {string.Join(", ", history)}"); // 2. Add current metric value, remove old values history.Add(metric.Value); history.RemoveAll(e => e.Time < metric.Value.Time.Subtract(Period)); // 3. Compare the aggregates to thresholds, produce scaling action if needed ScaleAction action = null; if (history.Count >= 5 && context.CurrentUtcDateTime - state.LastScalingActionTime > CooldownPeriod) { var average = (int)history.Average(e => e.Value); var maximum = history.Max(e => e.Value); if (average > ThresholdUp) { log.Info($"Scaling logic: Value {average} is too high, scaling {metric.ResourceName} up..."); action = new ScaleAction(metric.ResourceName, ScaleActionType.Up); } else if (maximum < ThresholdDown) { log.Info($"Scaling logic: Value {maximum} is low, scaling {metric.ResourceName} down..."); action = new ScaleAction(metric.ResourceName, ScaleActionType.Down); } } // 4. If scaling is needed, call Scaler if (action != null) { var result = await context.CallFunctionAsync <int>(nameof(Scaler), action); log.Info($"Scaling logic: Scaled to {result} instances."); state.LastScalingActionTime = context.CurrentUtcDateTime; } context.ContinueAsNew(state); return(state); }
public static async Task TryCatchLoop([OrchestrationTrigger] DurableOrchestrationContext ctx) { int iterations = ctx.GetInput <int>(); int catchCount = 0; for (int i = 0; i < iterations; i++) { try { await ctx.CallFunctionAsync(nameof(Activities.Throw), "Kah-BOOOOOM!!!"); } catch { catchCount++; } } ctx.SetOutput(catchCount); }