Browse Source

* clsDialler.cs: Throttle calls

* clsBotClient.cs: Add e-mail to EMERGENCY
Stuart Johnson 1 year ago
parent
commit
3bfd240544
2 changed files with 37 additions and 3 deletions
  1. 35 2
      TFA-Bot/Dialler/clsDialler.cs
  2. 2 1
      TFA-Bot/DiscordBot/clsBotClient.cs

+ 35 - 2
TFA-Bot/Dialler/clsDialler.cs

@@ -9,6 +9,7 @@ using System.Linq;
 using RestSharp;
 using RestSharp.Authenticators;
 using TFABot.Dialler;
+using System.Threading;
 
 namespace TFABot
 {
@@ -57,7 +58,7 @@ namespace TFABot
                 }
             }
             
-            
+            var dialList = new List<Task>();
             foreach (var nameItem in names.Split(new char []{' '}, StringSplitOptions.RemoveEmptyEntries))
             {
                 var name = nameItem.ToLower();
@@ -70,12 +71,14 @@ namespace TFABot
                     }
                     
                     if (user!=null) 
-                        Dialler.CallAsync(user.DiscordName,user.Tel,ChBotAlert);
+                        dialList.Add(Dialler.CallAsync(user.DiscordName,user.Tel,ChBotAlert));
                     else if (ChBotAlert!=null)
                        ChBotAlert.SendMessageAsync("name not found!");
                 }
             }
             
+            //We had some issues with calling too many numbers at once, so we throttle it to 5 at a time.
+            if (dialList.Count>0) StartAndWaitAllThrottledAsync(dialList,5,61000);
         }
         
         static public void call(MessageCreateEventArgs e)
@@ -83,6 +86,36 @@ namespace TFABot
             var toRing = e.Message.Content.ToLower();
             call(toRing,e.Channel);
         }
+        
+        public static async Task StartAndWaitAllThrottledAsync(IEnumerable<Task> tasksToRun, int maxTasksToRunInParallel, int timeoutInMilliseconds, CancellationToken cancellationToken = new CancellationToken())
+        {
+            // Convert to a list of tasks so that we don't enumerate over it multiple times needlessly.
+            var tasks = tasksToRun.ToList();
+         
+            using (var throttler = new SemaphoreSlim(maxTasksToRunInParallel))
+            {
+                var postTaskTasks = new List<Task>();
+         
+                // Have each task notify the throttler when it completes so that it decrements the number of tasks currently running.
+                tasks.ForEach(t => postTaskTasks.Add(t.ContinueWith(tsk => throttler.Release())));
+         
+                // Start running each task.
+                foreach (var task in tasks)
+                {
+                    // Increment the number of tasks currently running and wait if too many are running.
+                    await throttler.WaitAsync(timeoutInMilliseconds, cancellationToken);
+         
+                    cancellationToken.ThrowIfCancellationRequested();
+                    task.Start();
+                }
+         
+                // Wait for all of the provided tasks to complete.
+                // We wait on the list of "post" tasks instead of the original tasks, otherwise there is a potential race condition where the throttler&#39;s using block is exited before some Tasks have had their "post" action completed, which references the throttler, resulting in an exception due to accessing a disposed object.
+                await Task.WhenAll(postTaskTasks.ToArray());
+            }
+        }
+        
+        
       
     }
 }

+ 2 - 1
TFA-Bot/DiscordBot/clsBotClient.cs

@@ -227,7 +227,7 @@ namespace DiscordBot
                     if (emergency && keyword=="E" || (keyword !="E" && message.Contains(keyword)))
                     {
                         if (!keywordUsed.Contains(keyword)) keywordUsed.Add(keyword);
-                        if (!toCall.Contains(user.Name))  toCall.Add(user.Name);
+                        if (!toCall.Contains(user.Name)) toCall.Add(user.Name);
                     }
                 }
             }
@@ -247,6 +247,7 @@ namespace DiscordBot
             foreach (var userName in toCall)
             {
                 clsDialler.call(userName);
+                clsEmail.email(userName);
             }
             
         }