0

I'm facing an issue when fetching voice recordings for a specific date range. I could not reproduce the issue locally. But I continuously get the following error in production

"Twilio.Exceptions.ApiConnectionException: Connection Error: GEThttps://api.twilio.com/2010-04-01/Accounts/AccountID/Recordings.json?DateCreated<=2018-10-27T06:27:49Z ---> System.Threading.Tasks.TaskCanceledException: A task was canceled."

The code we use is mentioned below. This code is run like every 2 minutes

string accountSid = string.Empty;
string authToken = string.Empty;

  if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["TwilioAccountSID"]) && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["TwilioAuthToken"]))
                    {
                        accountSid = ConfigurationManager.AppSettings["TwilioAccountSID"];
                        authToken = ConfigurationManager.AppSettings["TwilioAuthToken"];

                        TwilioClient.Init(accountSid, authToken);
                        var deleteDateRange = DateTime.UtcNow.AddDays(-1);
                        var recordings = RecordingResource.Read(
                                              dateCreatedBefore: deleteDateRange
                                            );

                        if (recordings.Count() > 0)
                        {
                            foreach (var recording in recordings)
                            {
                                RecordingResource.Delete(recording.Sid);
                            }
                        }
                    }
Zaheen Haris
  • 31
  • 1
  • 6
  • Are you using any Tasks or async methods? – Aman B Oct 29 '18 at 10:15
  • Also, the if check before the foreach to delete can be removed, as the loop won't run if there are no elements anyway – Aman B Oct 29 '18 at 10:17
  • Also in this case, `recordings.Any()` will be more efficient than `recordings.Count() > 0`, because count enumerates all elements. – Aman B Oct 29 '18 at 10:19
  • I don't think so. (Not using tasks and Async methods) – Zaheen Haris Oct 29 '18 at 11:44
  • Try changing `var recordings = RecordingResource.Read(dateCreatedBefore: deleteDateRange);` to `var recordings = RecordingResource.Read(dateCreatedBefore: deleteDateRange).ToList();` to force C# to fetch all the recordings before going into the loop. – dprothero Oct 29 '18 at 22:06
  • Another thing you could try is use separate TwilioRestClients for the read and write operations. See this gist: https://gist.github.com/dprothero/20600365507f146b7b845e0e285efd5b – dprothero Oct 29 '18 at 22:14
  • Let me know if either of those solves the issue for you and I'll post an "official" answer. – dprothero Oct 29 '18 at 22:17
  • I dont think converting to .ToList() will make any difference as it has nothing to do with the exception I get. Will try this though. And will definitely try the second solution and lets see it makes any difference. – Zaheen Haris Oct 30 '18 at 05:28
  • Reproduced the issue. Issue occurs when large number of requests are made to twilio simultaneously. Moved this method out of the thread to resolve this ! – Zaheen Haris Oct 30 '18 at 12:23

0 Answers0