Last active
August 1, 2020 23:13
-
-
Save Cheranga/3b3ad3bab0d639f86ed7656cd5780324 to your computer and use it in GitHub Desktop.
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
public class SendSmsOrchestratorFunction | |
{ | |
[FunctionName(nameof(SendSmsOrchestratorFunction))] | |
public async Task SendSmsAsync([OrchestrationTrigger] IDurableOrchestrationContext context) | |
{ | |
var recipient = context.GetInput<SmsRecipient>(); | |
var status = await context.CallActivityAsync<bool>(nameof(SaveSmsRecipientActivityFunction), recipient); | |
if (!status) | |
{ | |
return; | |
} | |
using (var cts = new CancellationTokenSource()) | |
{ | |
var timeoutTask = context.CreateTimer(context.CurrentUtcDateTime.AddSeconds(30), cts.Token); | |
var waitForSmsSendTask = context.WaitForExternalEvent<SmsEvent>("smssentevent"); | |
var retryOptions = new RetryOptions(TimeSpan.FromSeconds(5), 5); | |
await context.CallActivityWithRetryAsync(nameof(SendSmsActivityFunction), retryOptions, recipient); | |
var winner = await Task.WhenAny(timeoutTask, waitForSmsSendTask); | |
if (winner == waitForSmsSendTask) | |
{ | |
cts.Cancel(); | |
await context.CallActivityAsync(nameof(UpdateSmsRecipientActivityFunction), waitForSmsSendTask.Result); | |
} | |
else | |
{ | |
await context.CallActivityAsync(nameof(UpdateSmsRecipientActivityFunction), new SmsEvent{Id = recipient.Id, Status = RecipientStatus.CouldNotSend}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment