feat: move scheduled tasks to separate project
This commit is contained in:
15
PluralKit.ScheduledTasks/PluralKit.ScheduledTasks.csproj
Normal file
15
PluralKit.ScheduledTasks/PluralKit.ScheduledTasks.csproj
Normal 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>
|
40
PluralKit.ScheduledTasks/Startup.cs
Normal file
40
PluralKit.ScheduledTasks/Startup.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
75
PluralKit.ScheduledTasks/TaskHandler.cs
Normal file
75
PluralKit.ScheduledTasks/TaskHandler.cs
Normal 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;
|
||||
|
||||
}
|
||||
}
|
1359
PluralKit.ScheduledTasks/packages.lock.json
Normal file
1359
PluralKit.ScheduledTasks/packages.lock.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user