Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gsuttie
Created September 17, 2020 18: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 gsuttie/9e24da3d655bf66e490fd9ff3fd8d844 to your computer and use it in GitHub Desktop.
Save gsuttie/9e24da3d655bf66e490fd9ff3fd8d844 to your computer and use it in GitHub Desktop.
[FunctionName("O_PeriodicTask3")]
public static async Task<string> PeriodicTask3([OrchestrationTrigger] IDurableOrchestrationContext context, ILogger log)
{
var waitBetweenTries = TimeSpan.FromSeconds(100); // 3 tries in 5 minutes
var phoneNumbers = await context.CallActivityAsync<string[]>("A_GetNumbersFromStorage", null);
var callTime = context.CurrentUtcDateTime;
try
{
foreach (var phoneNumber in phoneNumbers)
{
CallInfo callinfo = new CallInfo
{
NumberToCall = phoneNumber,
InstanceId = context.InstanceId
};
for (var i = 0; i < 3; i++)
{
using (var timeoutCts = new CancellationTokenSource())
{
if (!context.IsReplaying)
{
log.LogWarning("About to call A_MakeCall3 activity. ");
}
await context.CallActivityAsync("A_MakeCall3", callinfo);
var twilioCallbackTask = context.WaitForExternalEvent<string>("TwilioCallback");
var timeoutTask = context.CreateTimer(context.CurrentUtcDateTime.AddMinutes(1), timeoutCts.Token);
if (twilioCallbackTask == await Task.WhenAny(twilioCallbackTask, timeoutTask))
{
timeoutCts.Cancel();
var twilioResult = twilioCallbackTask.Result;
if (twilioResult == "answered")
{
log.LogWarning($"Call Answered by {phoneNumber} at {DateTime.Now}");
return phoneNumber;
}
}
}
var currTime = context.CurrentUtcDateTime;
if (currTime - callTime < waitBetweenTries)
{
// wait a bit till next try
await context.CreateTimer(callTime.Add(waitBetweenTries), CancellationToken.None);
}
}
}
return null;
}
catch (Exception e)
{
log.LogWarning(e.Message);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment