feat: move scheduled tasks to separate project

This commit is contained in:
spiral
2021-10-15 06:27:38 -04:00
parent 362770eef0
commit 3bc451eb4b
13 changed files with 1560 additions and 42 deletions

View File

@@ -260,7 +260,7 @@ namespace PluralKit.Bot
{
// i'm just going to disable this for now we need to find something nicer
// await _errorMessageService.SendErrorMessage(reportChannel.Value,
// sentryEvent.EventId.ToString());
// sentryEvent.EventId.ToString());
}
}
}
@@ -272,9 +272,6 @@ namespace PluralKit.Bot
await UpdateBotStatus();
// Clean up message cache in postgres
await _commandMessageService.CleanupOldMessages();
// Collect some stats, submit them to the metrics backend
await _collector.CollectStats();
await Task.WhenAll(((IMetricsRoot)_metrics).ReportRunner.RunAllAsync());

View File

@@ -85,11 +85,7 @@ namespace PluralKit.Bot
var messagesProxied = _metrics.Snapshot.GetForContext("Bot").Meters.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.MessagesProxied.Name)?.Value;
var commandsRun = _metrics.Snapshot.GetForContext("Bot").Meters.FirstOrDefault(m => m.MultidimensionalName == BotMetrics.CommandsRun.Name)?.Value;
var totalSystems = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SystemCount.Name)?.Value ?? 0;
var totalMembers = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MemberCount.Name)?.Value ?? 0;
var totalGroups = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.GroupCount.Name)?.Value ?? 0;
var totalSwitches = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.SwitchCount.Name)?.Value ?? 0;
var totalMessages = _metrics.Snapshot.GetForContext("Application").Gauges.FirstOrDefault(m => m.MultidimensionalName == CoreMetrics.MessageCount.Name)?.Value ?? 0;
var counts = await _repo.GetStats();
var shardId = ctx.Shard.ShardId;
var shardTotal = ctx.Cluster.Shards.Count;
@@ -113,7 +109,11 @@ namespace PluralKit.Bot
.Field(new("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true))
.Field(new("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true))
.Field(new("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true))
.Field(new("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalGroups:N0} groups, {totalSwitches:N0} switches, {totalMessages:N0} messages"))
.Field(new("Total numbers", $"{counts.SystemCount:N0} systems,"
+ $" {counts.MemberCount:N0} members,"
+ $" {counts.GroupCount:N0} groups,"
+ $" {counts.SwitchCount:N0} switches,"
+ $" {counts.MessageCount:N0} messages"))
.Timestamp(Process.GetCurrentProcess().StartTime.ToString("O"))
.Footer(new($"PluralKit {BuildInfoService.Version} • https://github.com/xSke/PluralKit • Last restarted: ")); ;
await ctx.Rest.EditMessage(msg.ChannelId, msg.Id,

View File

@@ -10,8 +10,6 @@ namespace PluralKit.Bot
{
public class CommandMessageService
{
private static readonly Duration CommandMessageRetention = Duration.FromHours(24);
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly IClock _clock;
@@ -35,16 +33,5 @@ namespace PluralKit.Bot
{
return await _repo.GetCommandMessage(messageId);
}
public async Task CleanupOldMessages()
{
var deleteThresholdInstant = _clock.GetCurrentInstant() - CommandMessageRetention;
var deleteThresholdSnowflake = DiscordUtils.InstantToSnowflake(deleteThresholdInstant);
var deletedRows = await _repo.DeleteCommandMessagesBefore(deleteThresholdSnowflake);
_logger.Information("Pruned {DeletedRows} command messages older than retention {Retention} (older than {DeleteThresholdInstant} / {DeleteThresholdSnowflake})",
deletedRows, CommandMessageRetention, deleteThresholdInstant, deleteThresholdSnowflake);
}
}
}

View File

@@ -20,7 +20,7 @@ namespace PluralKit.Bot
private readonly IDiscordCache _cache;
private readonly CpuStatService _cpu;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly WebhookCacheService _webhookCache;
@@ -28,13 +28,13 @@ namespace PluralKit.Bot
private readonly ILogger _logger;
public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, IDatabase db, IDiscordCache cache)
public PeriodicStatCollector(IMetrics metrics, ILogger logger, WebhookCacheService webhookCache, DbConnectionCountHolder countHolder, CpuStatService cpu, ModelRepository repo, IDiscordCache cache)
{
_metrics = metrics;
_webhookCache = webhookCache;
_countHolder = countHolder;
_cpu = cpu;
_db = db;
_repo = repo;
_cache = cache;
_logger = logger.ForContext<PeriodicStatCollector>();
}
@@ -63,12 +63,15 @@ namespace PluralKit.Bot
_metrics.Measure.Gauge.SetValue(BotMetrics.Channels, channelCount);
// Aggregate DB stats
var counts = await _db.Execute(c => c.QueryFirstAsync<Counts>("select (select count(*) from systems) as systems, (select count(*) from members) as members, (select count(*) from switches) as switches, (select count(*) from messages) as messages, (select count(*) from groups) as groups"));
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.Systems);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, counts.Members);
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.Switches);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.Messages);
_metrics.Measure.Gauge.SetValue(CoreMetrics.GroupCount, counts.Groups);
// just fetching from database here - actual updating of the data is done in PluralKit.ScheduledTasks
// if you're not running ScheduledTasks and want up-to-date counts, uncomment the following line:
// await _repo.UpdateStats();
var counts = await _repo.GetStats();
_metrics.Measure.Gauge.SetValue(CoreMetrics.SystemCount, counts.SystemCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MemberCount, counts.MemberCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.GroupCount, counts.GroupCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.SwitchCount, counts.SwitchCount);
_metrics.Measure.Gauge.SetValue(CoreMetrics.MessageCount, counts.MessageCount);
// Process info
var process = Process.GetCurrentProcess();
@@ -88,14 +91,5 @@ namespace PluralKit.Bot
stopwatch.Stop();
_logger.Debug("Updated metrics in {Time}", stopwatch.ElapsedDuration());
}
public class Counts
{
public int Systems { get; }
public int Members { get; }
public int Switches { get; }
public int Messages { get; }
public int Groups { get; }
}
}
}