public static async Task CreateLongRunningTimer( this DurableOrchestrationContextBase target, DateTime fireAt, CancellationToken token) { if (fireAt < target.CurrentUtcDateTime.AddDays(7)) { await target.CreateTimer(fireAt, token); } else { var diff = fireAt - target.CurrentUtcDateTime; while (diff > TimeSpan.Zero) { if (diff > TimeSpan.FromDays(7)) { await target.CreateTimer(target.CurrentUtcDateTime.AddDays(7), token); diff -= TimeSpan.FromDays(7); } else { await target.CreateTimer(target.CurrentUtcDateTime.Add(diff), token); diff = TimeSpan.Zero; } } } }
public static async Task <IEnumerable <(int, long)> > Run( [OrchestrationTrigger] DurableOrchestrationContextBase context, ILogger log) { var input = context.GetInput <OrchestratorDto>(); int take = input.EventData.Take; DateTime endTime = context.CurrentUtcDateTime.AddSeconds(take); int next = input.EventData.Skip; List <(int, long)> takenSamples = new List <(int, long)>(); while (context.CurrentUtcDateTime < endTime) { var current = next; long time; (next, time) = await context.CallActivityAsync <(int, long)>("SendFlightDataSignalRActivity", new SendFlightDataSignalRActivityInput { index = next, count = 20, allplanes = true }); takenSamples.Add((current, time)); var nextCheckpoint = context.CurrentUtcDateTime.AddMilliseconds(1050); if (!context.IsReplaying) { log.LogInformation($"Next check for at {nextCheckpoint}."); } await context.CreateTimer(nextCheckpoint, CancellationToken.None); } log.LogInformation($"Monitor expiring."); return(takenSamples); }
public static async Task <List <string> > Run( [OrchestrationTrigger] DurableOrchestrationContextBase context) { var outputs = new List <string>(); outputs.Add(await context.CallActivityAsync <string>("E1_SayHello", "Tokyo")); context.SetCustomStatus("Tokyo"); DateTime deadline = context.CurrentUtcDateTime.Add(TimeSpan.FromSeconds(3)); await context.CreateTimer(deadline, CancellationToken.None); outputs.Add(await context.CallActivityAsync <string>("E1_SayHello", "Seattle")); context.SetCustomStatus("Tokyo, Seattle"); await context.CreateTimer(deadline, CancellationToken.None); outputs.Add(await context.CallActivityAsync <string>("E1_SayHello", "London")); context.SetCustomStatus("Tokyo, Seattle, London"); // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"] return(outputs); }
public static async Task Func([OrchestrationTrigger] DurableOrchestrationContextBase context) { var data = await context.CallActivityAsync <string[]>(GetDataActivity.Name, null); await context.CreateTimer(context.CurrentUtcDateTime.AddSeconds(60), CancellationToken.None); var tasks = data.Select(x => context.CallActivityAsync <bool>(ProcessActivity.Name, x)); var results = await Task.WhenAll(tasks); if (results.Any(x => !x)) { await context.CallActivityAsync(ErrorNotifyActivity.Name, null); } }
public static async Task <string> GetApprovalOrchestrator([OrchestrationTrigger] DurableOrchestrationContextBase ctx, ILogger log) { var approvalConfig = ctx.GetInput <ApprovalConfig>(); string result; var expireAt = ctx.CurrentUtcDateTime.AddMinutes(approvalConfig.TimeoutMinutes); for (var n = 0; n < approvalConfig.ApproverCount; n++) { // todo: send a message to each approver if (!ctx.IsReplaying) { log.LogInformation($"Requesting approval from Approver {n + 1}"); } } var cts = new CancellationTokenSource(); var timerTask = ctx.CreateTimer(expireAt, cts.Token); var approvers = new HashSet <string>(); while (true) { var externalEventTask = ctx.WaitForExternalEvent <ApprovalResult>(ApprovalResultEventName); var completed = await Task.WhenAny(timerTask, externalEventTask); if (completed == timerTask) { result = $"Timed out with {approvers.Count} approvals so far"; if (!ctx.IsReplaying) { log.LogWarning(result); } break; // end orchestration } else if (completed == externalEventTask) { var approver = externalEventTask.Result.Approver; if (externalEventTask.Result.Approved) { approvers.Add(approver); if (!ctx.IsReplaying) { log.LogInformation($"Approval received from {approver}"); } if (approvers.Count >= approvalConfig.RequiredApprovals) { result = $"Approved ({approvers.Count} approvals received)"; if (!ctx.IsReplaying) { log.LogInformation(result); } break; } } else { result = $"Rejected by {approver}"; if (!ctx.IsReplaying) { log.LogWarning(result); } break; } } else { throw new InvalidOperationException("Unexpected result from Task.WhenAny"); } } cts.Cancel(); return(result); }
public static async Task DurableFunctionWithTimer( [OrchestrationTrigger] DurableOrchestrationContextBase context) { var input = context.GetInput <DurableFunctionWithTimerInput>(); await context.CreateTimer(context.CurrentUtcDateTime.Add(input.Timer), CancellationToken.None); }