Skip to content

Instantly share code, notes, and snippets.

@Cheranga
Last active August 1, 2020 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheranga/3b3ad3bab0d639f86ed7656cd5780324 to your computer and use it in GitHub Desktop.
Save Cheranga/3b3ad3bab0d639f86ed7656cd5780324 to your computer and use it in GitHub Desktop.
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