Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.