From 59afd8ce3b771840bc021bb576b9738c5bb9db4d Mon Sep 17 00:00:00 2001 From: Noi Date: Sun, 30 May 2021 12:00:49 -0700 Subject: [PATCH] Move server count reporting to be handled per-shard --- .../ExternalStatisticsReporting.cs | 58 +++++++++++++++++++ BackgroundServices/ShardBackgroundWorker.cs | 3 +- ShardManager.cs | 35 ----------- 3 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 BackgroundServices/ExternalStatisticsReporting.cs diff --git a/BackgroundServices/ExternalStatisticsReporting.cs b/BackgroundServices/ExternalStatisticsReporting.cs new file mode 100644 index 0000000..6e2fc0c --- /dev/null +++ b/BackgroundServices/ExternalStatisticsReporting.cs @@ -0,0 +1,58 @@ +using System; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace BirthdayBot.BackgroundServices +{ + /// + /// Reports user count statistics to external services on a shard by shard basis. + /// + class ExternalStatisticsReporting : BackgroundService + { + const int ProcessInterval = 600 / ShardBackgroundWorker.Interval; // Process every ~5 minutes + private int _tickCount = 0; + + private static readonly HttpClient _httpClient = new HttpClient(); + + public ExternalStatisticsReporting(ShardInstance instance) : base(instance) { } + + public override async Task OnTick(CancellationToken token) + { + if (++_tickCount % ProcessInterval != 0) return; + + var botId = ShardInstance.DiscordClient.CurrentUser.Id; + if (botId == 0) return; + + await SendDiscordBots(ShardInstance.DiscordClient.Guilds.Count, botId, token); + } + + private async Task SendDiscordBots(int userCount, ulong botId, CancellationToken token) + { + var dbotsToken = ShardInstance.Config.DBotsToken; + if (dbotsToken != null) + { + try + { + const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats"; + const string Body = "{{ \"guildCount\": {0}, \"shardCount\": {1}, \"shardId\": {2} }}"; + var uri = new Uri(string.Format(dBotsApiUrl, botId)); + + var post = new HttpRequestMessage(HttpMethod.Post, uri); + post.Headers.Add("Authorization", dbotsToken); + post.Content = new StringContent(string.Format(Body, + userCount, ShardInstance.Config.ShardTotal, ShardInstance.ShardId), + Encoding.UTF8, "application/json"); + + await _httpClient.SendAsync(post, token); + Log("Discord Bots: Update successful."); + } + catch (Exception ex) + { + Log("Discord Bots: Exception encountered during update: " + ex.Message); + } + } + } + } +} diff --git a/BackgroundServices/ShardBackgroundWorker.cs b/BackgroundServices/ShardBackgroundWorker.cs index 1c36589..1c2c31e 100644 --- a/BackgroundServices/ShardBackgroundWorker.cs +++ b/BackgroundServices/ShardBackgroundWorker.cs @@ -39,7 +39,8 @@ namespace BirthdayBot.BackgroundServices { {UserDownloader}, {BirthdayUpdater}, - {new DataRetention(instance)} + {new DataRetention(instance)}, + {new ExternalStatisticsReporting(instance)} }; _workerTask = Task.Factory.StartNew(WorkerLoop, _workerCanceller.Token); diff --git a/ShardManager.cs b/ShardManager.cs index c12d608..752c4e7 100644 --- a/ShardManager.cs +++ b/ShardManager.cs @@ -5,7 +5,6 @@ using Discord.WebSocket; using System; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -167,8 +166,6 @@ namespace BirthdayBot var guildTotal = guildCounts.Sum(); var guildAverage = guildCounts.Any() ? guildCounts.Average() : 0; Log($"Currently in {guildTotal} guilds. Average shard load: {guildAverage:0.0}."); - if (nullShards.Count == 0 && botId.HasValue) - await SendExternalStatistics(guildTotal, botId.Value, _watchdogCancel.Token).ConfigureAwait(false); // Health report var goodShards = new List(); @@ -243,37 +240,5 @@ namespace BirthdayBot } catch (TaskCanceledException) { } } - - #region Statistical reporting - private static readonly HttpClient _httpClient = new HttpClient(); - - /// - /// Send statistical information to external services. - /// - private async Task SendExternalStatistics(int count, ulong botId, CancellationToken token) - { - var dbotsToken = Config.DBotsToken; - if (dbotsToken != null) - { - try - { - const string dBotsApiUrl = "https://discord.bots.gg/api/v1/bots/{0}/stats"; - const string Body = "{{ \"guildCount\": {0} }}"; - var uri = new Uri(string.Format(dBotsApiUrl, botId)); - - var post = new HttpRequestMessage(HttpMethod.Post, uri); - post.Headers.Add("Authorization", dbotsToken); - post.Content = new StringContent(string.Format(Body, count), Encoding.UTF8, "application/json"); - - await _httpClient.SendAsync(post, token); - Log("Discord Bots: Update successful."); - } - catch (Exception ex) - { - Log("Discord Bots: Exception encountered during update: " + ex.Message); - } - } - } - #endregion } }