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

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PluralKit.Core\PluralKit.Core.csproj" />
</ItemGroup>
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using Autofac;
using Microsoft.Extensions.Configuration;
using PluralKit.Core;
namespace PluralKit.ScheduledTasks
{
class Startup
{
static async Task Main(string[] args)
{
// Load configuration and run global init stuff
var config = InitUtils.BuildConfiguration(args).Build();
InitUtils.InitStatic();
var services = BuildContainer(config);
services.Resolve<TaskHandler>().Run();
await Task.Delay(-1);
}
private static IContainer BuildContainer(IConfiguration config)
{
var builder = new ContainerBuilder();
builder.RegisterInstance(config);
builder.RegisterModule(new ConfigModule<CoreConfig>());
builder.RegisterModule(new LoggingModule("ScheduledTasks"));
builder.RegisterModule(new MetricsModule());
builder.RegisterModule<DataStoreModule>();
builder.RegisterType<TaskHandler>().AsSelf().SingleInstance();
return builder.Build();
}
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using NodaTime;
using NodaTime.Extensions;
using Serilog;
using PluralKit.Core;
namespace PluralKit.ScheduledTasks
{
public class TaskHandler
{
private static readonly Duration CommandMessageRetention = Duration.FromHours(24);
private Timer _periodicTask;
private readonly ILogger _logger;
private readonly IDatabase _db;
private readonly ModelRepository _repo;
public TaskHandler(ILogger logger, IDatabase db, ModelRepository repo)
{
_logger = logger;
_db = db;
_repo = repo;
}
public void Run()
{
_logger.Information("Starting scheduled task runner...");
var timeNow = SystemClock.Instance.GetCurrentInstant();
var timeTillNextWholeMinute = TimeSpan.FromMilliseconds(60000 - timeNow.ToUnixTimeMilliseconds() % 60000 + 250);
_periodicTask = new Timer(_ =>
{
var __ = UpdatePeriodic();
}, null, timeTillNextWholeMinute, TimeSpan.FromMinutes(1));
}
private async Task UpdatePeriodic()
{
_logger.Information("Running per-minute scheduled tasks.");
var stopwatch = new Stopwatch();
stopwatch.Start();
_logger.Information("Updating database stats...");
await _repo.UpdateStats();
// Clean up message cache in postgres
await CleanupOldMessages();
stopwatch.Stop();
_logger.Information("Ran scheduled tasks in {Time}", stopwatch.ElapsedDuration());
}
private async Task CleanupOldMessages()
{
var deleteThresholdInstant = SystemClock.Instance.GetCurrentInstant() - CommandMessageRetention;
var deleteThresholdSnowflake = 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);
}
// we don't have access to PluralKit.Bot here, so this needs to be vendored
public static ulong InstantToSnowflake(Instant time) =>
(ulong)(time - Instant.FromUtc(2015, 1, 1, 0, 0, 0)).TotalMilliseconds << 22;
}
}

File diff suppressed because it is too large Load Diff