From 03e399360bcc52112a798db57672f2661f191541 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 31 Jan 2025 20:40:47 -0400 Subject: [PATCH] Add ability to enable specific ban parsers, remove inactive ones from default configuration --- CentCom.Server/BanSources/VgBanParser.cs | 14 +++------ CentCom.Server/Data/DatabaseUpdater.cs | 36 ++++++++++-------------- CentCom.Server/Program.cs | 5 ++-- CentCom.Server/appsettings.json | 19 ++++--------- 4 files changed, 28 insertions(+), 46 deletions(-) diff --git a/CentCom.Server/BanSources/VgBanParser.cs b/CentCom.Server/BanSources/VgBanParser.cs index 31aa1bc..84b7997 100644 --- a/CentCom.Server/BanSources/VgBanParser.cs +++ b/CentCom.Server/BanSources/VgBanParser.cs @@ -7,15 +7,9 @@ namespace CentCom.Server.BanSources; -public class VgBanParser : BanParser +public class VgBanParser(DatabaseContext dbContext, VgBanService banService, ILogger logger) + : BanParser(dbContext, logger) { - private readonly VgBanService _banService; - - public VgBanParser(DatabaseContext dbContext, VgBanService banService, ILogger logger) : base(dbContext, logger) - { - _banService = banService; - } - protected override Dictionary Sources => new() { { "vgstation", new BanSource @@ -32,13 +26,13 @@ public VgBanParser(DatabaseContext dbContext, VgBanService banService, ILogger> FetchAllBansAsync() { Logger.LogInformation("Fetching all bans for /vg/station..."); - return await _banService.GetBansAsync(); + return await banService.GetBansAsync(); } public override async Task> FetchNewBansAsync() { // Note that the /vg/station website only has a single page for bans, so we always do a full refresh Logger.LogInformation("Fetching new bans for /vg/station..."); - return await _banService.GetBansAsync(); + return await banService.GetBansAsync(); } } \ No newline at end of file diff --git a/CentCom.Server/Data/DatabaseUpdater.cs b/CentCom.Server/Data/DatabaseUpdater.cs index bf159d2..a624437 100644 --- a/CentCom.Server/Data/DatabaseUpdater.cs +++ b/CentCom.Server/Data/DatabaseUpdater.cs @@ -13,7 +13,13 @@ namespace CentCom.Server.Data; [DisallowConcurrentExecution] -public class DatabaseUpdater : IJob +public class DatabaseUpdater( + DatabaseContext dbContext, + ILogger logger, + FlatDataImporter importer, + ISchedulerFactory schedulerFactory, + IConfiguration config) + : IJob { /// /// Types of BanParsers which should not be automatically configured with a refresh schedule @@ -23,32 +29,19 @@ public class DatabaseUpdater : IJob typeof(StandardBanParser) }; - private readonly DatabaseContext _dbContext; - private readonly FlatDataImporter _importer; - private readonly ILogger _logger; - private readonly List _providerConfigs; - private readonly ISchedulerFactory _schedulerFactory; - - public DatabaseUpdater(DatabaseContext dbContext, ILogger logger, FlatDataImporter importer, - ISchedulerFactory schedulerFactory, IConfiguration config) - { - _dbContext = dbContext; - _logger = logger; - _importer = importer; - _schedulerFactory = schedulerFactory; - _providerConfigs = config.GetSection("standardSources").Get>(); - } + private readonly ILogger _logger = logger; + private readonly List _providerConfigs = config.GetSection("standardSources").Get>(); public async Task Execute(IJobExecutionContext context) { _logger.LogInformation("Checking for any pending migrations"); - var appliedMigration = await _dbContext.Migrate(context.CancellationToken); + var appliedMigration = await dbContext.Migrate(context.CancellationToken); if (appliedMigration) _logger.LogInformation("Applied new migration"); // Import any new flat data prior to registering ban parsing jobs _logger.LogInformation("Checking for any updates to flat file data"); - await _importer.RunImports(); + await importer.RunImports(); // Call register jobs after db migration to ensure that the DB is actually created on first run before doing any ops _logger.LogInformation("Registering ban parsing jobs"); @@ -62,15 +55,16 @@ public async Task Execute(IJobExecutionContext context) /// private async Task RegisterJobs() { + var allowedParsers = config.GetSection("enabledParsers").Get>(); var parsers = AppDomain.CurrentDomain.GetAssemblies().Aggregate(new List(), (curr, next) => { curr.AddRange(next.GetTypes() - .Where(x => x.IsSubclassOf(typeof(BanParser)) && !_autoConfigBypass.Contains(x))); + .Where(x => x.IsSubclassOf(typeof(BanParser)) && !_autoConfigBypass.Contains(x) && allowedParsers.Contains(x.Name))); return curr; }); // Get a scheduler instance - var scheduler = await _schedulerFactory.GetScheduler(); + var scheduler = await schedulerFactory.GetScheduler(); foreach (var p in parsers) { @@ -102,7 +96,7 @@ private async Task RegisterJobs() private async Task RegisterStandardJobs() { // Get a scheduler instance - var scheduler = await _schedulerFactory.GetScheduler(); + var scheduler = await schedulerFactory.GetScheduler(); // Iterate through each standard provider to set it up foreach (var provider in _providerConfigs) diff --git a/CentCom.Server/Program.cs b/CentCom.Server/Program.cs index 8712e30..707c554 100644 --- a/CentCom.Server/Program.cs +++ b/CentCom.Server/Program.cs @@ -15,6 +15,7 @@ using Microsoft.Extensions.Hosting; using Quartz; using Serilog; +using Serilog.Filters; namespace CentCom.Server; @@ -27,7 +28,7 @@ static Task Main(string[] args) .Enrich.FromLogContext() .WriteTo.Logger(lc => { - //lc.Filter.ByExcluding(Matching.FromSource("Quartz")); + lc.Filter.ByExcluding(Matching.FromSource("Quartz")); lc.WriteTo.Console( outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] ({SourceContext}) {Message:lj}{NewLine}{Exception}"); @@ -43,7 +44,7 @@ static Task Main(string[] args) Log.Logger.ForContext() .Information("Starting CentCom Server {Version} ({Commit})", AssemblyInformation.Current.Version, AssemblyInformation.Current.Commit[..7]); - + return CreateHostBuilder(args).RunConsoleAsync(); } diff --git a/CentCom.Server/appsettings.json b/CentCom.Server/appsettings.json index a542cdd..5d390fd 100644 --- a/CentCom.Server/appsettings.json +++ b/CentCom.Server/appsettings.json @@ -6,18 +6,11 @@ "sourceConfig": { "allowFulpExpiredSSL": true }, - "standardSources": [ - { - "id": "shiptest", - "display": "Shiptest 13", - "roleplayLevel": "Medium", - "url": "https://bans.shiptest.ga/" - }, - { - "id": "austation", - "display": "AuStation", - "roleplayLevel": "Low", - "url": "https://api.austation.net/" - } + "standardSources": [], + "enabledParsers": [ + "BeeBanParser", + "FulpBanParser", + "TgBanParser", + "YogBanParser" ] } \ No newline at end of file